📓

NetworkPacketProcessor

//simple approach means we gonna deal with packets as enumed types for now
public enum PacketType
{
    joinedMasterServer, //you joined the master server
    hostGameOnMasterServer,//tell server you would like to host a server
    updateHostedGamePlayerCount,//tell server how many players are in the game
    requestListOfGames, //ask MS for a list of games to join
    listOfGames, //list of games received from the master server
    serverGameDetails, //info about the game hosting for the server to know (id and name)
    joinGameOnMasterServer, //I would like to join the game now


    clientJoinedGame, //you joined the game (confirmation from server AND your new player ID :D)
    changeScene, //server wants you to change scene
    clientInput, //button press info so server can simulate the shit you just did
    syncPacket, //server telling you what the world looks like on its end
    /*deltaPacket, //if you have lots of entities or lots of entity data, maybe use delta packets */
    
    clientPlayerData, //things like name, skin chosen etc
    serverPlayerDataUpdate, //info on what names, skins and scores people have
    gameWinner, //who won!
    newGameStart, //next round starting

    //TODO add more later
}
public class NetworkPacketProcessor
{
    //only 1 of these mother fuckers active at a time - Singleton (you are client or server)
    public static NetworkPacketProcessor networkPacketProcessor;
    //+1 so we can deal with the master server :/
//...

Info on ClassNetworkPacketProcessor is a base class designed for parsing data received in packets and directing them to other parts of the system as necessary. You’ll see this class extended as ServerPacketProcessor and ClientPacketProcessor.

At the top of the file you will see the PacketType enum. When a packet of data is sent in this system, the first bit of data written to the packet is the PacketType. The NetworkPacketProcessor that receives a packet reads this bit of information first and then it can decide what to do with it. You are welcome to add and change these packet enums to suit your needs.

Methods

public static void ProcessPacket(NetPeer fromPeer, NetPacketReader dataReader)

Grabs the static networkPacketProcessor and calls its ProcessBasicPackets method. the static networkPacketProcessor isa reference and could be pointing to a ServerPacketProcessor, ClientPacketProcessor or nothing.

public virtual void ProcessBasicPacket(NetPeer fromPeer, NetPacketReader dataReader)

This method is used to process simple packets and if it can’t find a match, will call ProcessOtherPacket. Currently the packetTypes this method can handle is ChangeScene, which if it does find that type of packet it can read which Unity Scene to load up

public virtual void ProcessOtherPacket(PacketType packetType, NetPeer fromPeer, NetPacketReader dataReader)

Currently empty, but used by sub classes to read packets specific to their access level as clients and servers are likely to expect different kinds data being sent their way

public static PacketType GetPacketType(NetPacketReader dataReader)

Given a NetPacketReader to access the data of a packet, it reads the very first bit to get the PacketType enum value and returns it

public void ChangeScene(string newScene)

Calls cleanup for the current Unity Scene and then loads the one by the name of newScene