📓

MasterServerConnection and GameServerListing

public class GameServerListing
{

    public static int currentGameID = 1;

    public int gameID = 0;
    public string name;
    public bool natDataReady = false;
    public IPEndPoint internalAddr;
    public IPEndPoint externalAddr;
    //used basically as a token to update the games data on the master server
    public int serverGamePassword;
    //TODO string password
    public int players = 0;
    public int maxPlayers = 10;//defualt, but change it as game says to update it

    //who created the game
    public NetPeer hostPeer;

    public GameServerListing()
    {
        //empty, useful for reading data from server
    }
    public GameServerListing(string name, NetPeer hostPeer)
    {
        this.name = name;
        this.hostPeer = hostPeer;
        gameID = currentGameID;
        natDataReady = false;
        System.Random r = new System.Random();
        serverGamePassword = r.Next();

        currentGameID++;
        Debug.Log("New game listing: " + name + " ID: " + gameID);
    }
    public void SetNatData(IPEndPoint internalAddr, IPEndPoint externalAddr)
    {
        Debug.Log("----------------------------------------------");
        Debug.Log("Set Nat Data:");
        Debug.Log("InternalAddr.Address: " + internalAddr.Address);
        Debug.Log("InternalAddr.AddressFamily: " + internalAddr.AddressFamily);
        Debug.Log("InternalAddr.Port: " + internalAddr.Port);
        Debug.Log("externalAddr.Address: " + externalAddr.Address);
        Debug.Log("externalAddr.AddressFamily: " + externalAddr.AddressFamily);
        Debug.Log("externalAddr.Port: " + externalAddr.Port);
        this.internalAddr = internalAddr;
        this.externalAddr = externalAddr;
        natDataReady = true;
    }
}


public class MasterServerConnection : GameConnection, INatPunchListener
{
    public List<NetPeer> netPeers = new List<NetPeer>();
    public List<GameServerListing> gameServerListings = new List<GameServerListing>();
    //public MasterServerPacketProcessor masterServerPacketProcessor = new MasterServerPacketProcessor();
    public int maxGamesToList = 5;
//...

I should really pull GameServerListing out into its own file, but for the moment its here.

A GameServerListing holds data on a game clients can join, listed on a Master Server. This includes name, ID, number of players, serverGamePassword(which is a token randomly set by the Master Server) and information on how to connect to this game.

The MasterServerConnection is a connection used for helping clients join servers and helping in the NAT Hole PunchThrough procedure. A MasterServerConnection keeps track of gameServerListings which connected clients can request information about. A Server can join the Master Server and request to have its details listed too. If a Server is running a game and new players are joining or some are leaving, Servers can tell the Master Server about it and it will update the appropriate game listing.

A Master Server should be hosted (if for public use) somewhere you can port forward any ports you intend for it to listen on so anyone can join with the correct IP and port combo.

Methods

public MasterServerConnection(int port)

A constructor which takes a port to start listening in on. It then sets up a bunch of events to fire when listening in for network related events. This is where most of the functionality comes in for this class and you are welcome to change this as you need for your own game.

Currently covers events such as peers connecting, disconnecting and receiving packets.

public void OnNatIntroductionRequest(IPEndPoint localEndPoint, IPEndPoint remoteEndPoint, string token)

A method to help aid in NAT Punch Through, facilitating the introductions stage where we pass client and server data to the other

public void OnNatIntroductionSuccess(IPEndPoint targetEndPoint, NatAddressType type, string token)

Currently doesn’t do anything, but this method holds the moment when introduction attempts are done between client and server for NAT Punch Through and its now up to them to carry out the rest of the procedure.