Networking Protocol - LightningdeGolem/Chips2 GitHub Wiki
Chips2 works by sending unsigned bytes to and from the client and server. To convert an unsinged byte to an integer, the program adds 128 to the value and vice versa. The network is managed by a series of states as follows:
After the server has accepted the client connection, the client sends its protocol id (PID) as a single unsinged byte. The server checks this byte with its own PID and sends 255 if the protocol ids match. If not, the server sends a 0 and closes the connection. If the connection was a success, the server and client start the next step. The server then sends the Map object by serializing it. It sends a packet as follows:(length of serialized array) {serialized array}
The client recieves this and deserializes it. If this was successful, the server and client move on to the Main Game State.
Now there is no real distinction between the client and the server: there are just two clients sending the same packet structure between them. This code also runs on a seperate thread. It is at this point that the game gui is opened and the game tick is started. The server sends first.This is the packet structure:
(PID) (PlayerX) (PlayerY) (PlayerRotation) (NumberOfBlockUpdates) {BlockUpdateData} (NumberOfGameVarUpdates) {GameVarUpdateData} (Flags)
(BlockX) (BlockY) (BlockID) (LengthOfBlockData) {BlockData}NB: Block ID as defined in the block id class
(GameVarId) (GameVarValue) Currently there is only one flag: the exit flag. This is on if one client wants to exit the game. It is the most significant bit of the flag byte. If this is true, the exit signal is sent to the rest of the game.The clients take turns to send each other the packet asyncronously.