Creating A Server Authoritative Game System Using Unity’s NEW Transport Layer | Part 2

AI Applied
4 min readJul 24, 2020

--

Understanding Network Messages

Messages get sent via the network connection as an array of bytes. We can create classes and have variables inside them that we want to have delivered across the network but this means that these classes need to be converted in a certain way into an array of bytes that can actually be sent as packets over the network.

In this section I’ll walk you through how to create a compatible network object that can be shared across the network between the client and the server. This is crucial to understanding any other step that we take when we actually build up from the lower level transport layer.

The Structure of A Message

The following code snippet is from the network protocol class of the project, from lines 44 to 51 and line 10.

The Message Class

The Message Type

Message Class

To transmit a message under the network you need to have two things, the first is the message class. This class defines what information will be held in the message. An instance of this class will be converted into an array of bytes and then transmitted across the network. Therefore, in our case every message class will need to be System.Serializable as indicated by the attribute at the head of this class. This will allow us to use The .Net Binary Stream Reader and Writer to serialize these classes. You needn’t trouble yourself with this serialization and deserialization right now, nevertheless it is of essence that you understand that a class does need to have this attribute.

Message Type

The message type is only useful to the receiver of the message, it allows him to figure out which type of message we are sending over the network. Are we sending him a chat message? Updating our client’s position on his machine? etc

Every message also has a Constructor, which automatically allows the message to assign itself it’s own type. This is such that there is less boilerplate code when you’re actually sending the message.

Creating Our Own Message

We will now see all these things in action. We shall send a message from the server to the client, and then have that message show up in the game.

Creating The Message Class

Open the NetworkProtocal.cs file and add the following class at the bottom.

This creates a new class, makes it a serializable object, and adds a constructor that automatically assignes the class type to SerevrNotification.

Creating The Message Type

As of now the code above causes an error, to fix this you need to scroll to the top of the network protocol file and modify the network protocol class to look as follows.

By adding this line public static byte SerevrNotification = 11; you are creating a new type of message, the message class we created earlier will then assign itself this type.

This is the only thing that goes into creating every new network message. Now let’s take a look at actually using it.

Sending The Network Message

I have built a level of obstruction into this networking system that allows us to send and receive Network messages quite easily, we will use this for now. Later on we shall actually look into how to build this kind of system and how messages are internally filtered to figure out what type they are.

The server has certain classes that help it when it comes to handling and sending network messages. We shall use the class that’s in charge of handling movement of cards on the board to send the client a notification whenever the server realizes that he has moved a card.

To do this head over to the Server_CardMessagesHandler class and modify its OnCardMoved function to look as follows;

By adding “//ADDE DPORTION” code, you are telling the server to construct a new message and send it to the client that has just moved a card. As simple as that, we have now sent a message to the client. We now need to prepare the client to receive this message.

Message Handlers

Open the GameClient class and modify the RegisterMessageHandlerClasses to look as follows

We have now registered to receive the server notification message, however we now get an error since we need a function to actually handle this message. To fix this add the following function to the game client class.

As you can see this class receives the message, and is also told from what connection this message came. This is not too important on the client, but is rather essential on the server.

I will not discuss the code that goes into the ShowPlayerMessage functionality, since this is not network code, if you do read it though, it’s rather simple and straightforward.

You can now play the game, and see the message pop up. As you can see no message pops up for the rejected client move.

In The Next Post

This post has shown you what is required of a network message. We have used a nice system for handling the message, but let’s face it; the system could be better.

In tomorrow’s post I will take a look at how to create message handler classes. This adds a bit of distribution to the code and prevents the client and server classes from being clustered with unnecessary functionality.

--

--

No responses yet