GameConnection
public enum ConnectionType
{
unassigned, server, client, masterServer, masterServerClient
}; //add more if you need it
//this is the base class for CLIENT and HOST
//and carries any SHARED code those 2 bastards need
public class GameConnection
{
public static string connection_key = "putYourGamesUniqueKeyHERE";
public int port;
public EventBasedNetListener listener;
public NetManager netManager;
public ConnectionType connectionType = ConnectionType.unassigned;
public NetDataWriter netDataWriter;
public float latency = 0; //in seconds (but likely 0.1 or something)
public int latencyFrames = 0;
public string currentGameScene = "";//use different approach if users can be in multiple scenes
//...
This is the base type for each type of connection the game supports(listed above in ConnectionType enum). Being the base class, it has attributes and methods useful to all types of connections.
To make sure other people using LiteNetLib are unable to join your games with their own programs, ensure to set connection_key to something fairly unique. This is used when making connections between your clients, servers and master servers and passed along to determine if they are allowed to connect.
Here is a quick run down of the other attributes of interest here:
- port is what port number to send and receive packets through. This can be a value up to 65535, but when working with ports its important to note that each program using a port on your computer must be using a different one. If a program is listening or connected to multiple places at once, its likely that program could be using more then 1 port. But basically 1 port per connection, which is why its attached to this class.
- listener is event driven and when a packet comes in can respond immediately by reading it or passing it some other class to read it. It also catches other events such as connections, disconnections and reading player latency values.
- netManager is a LiteNetLib class for actually sending and receiving packets and managing connections we’re wrapping here
- connectionType is one of the above enum values. Mostly used in if statements to help decide authority or how to deal with data.
- netDataWriter helps write packets. Its cheaper to reuse the 1 packet writer over and over rather then create a new one each time, which is why its here as an attribute
- latency and latencyFrames help us keep track of our own latency, but LiteNetLib has helper functions for this anyway
- currentGameScene is a string containing the name of the Unity Scene the game is currently in. When a server changes Scenes, it updates this value and then looks through its clients and if any are on the wrong scene, tells them to change to the current
Methods
public GameConnection()
Constructor which calls the Setup method (below)
public void Setup()
Sets up the listener, netManager and netDataWriter ready for first time use but does not kick off any connections yet
public virtual void Update()
Call this to poll the netManager for any incoming events e.g packets, connections, Nat Punch event etc
public void OpenPort(int port)
Let netManager know what port to start listening on
public void Stop()
Tell netManager to stop listening for events
public void SendChangeSceneMessage(NetPeer peer, string newScene)
Let a specific client know what scene to change to by sending them a message
public void SendChangeSceneMessageToAll(string newScene)
Let all clients know what scene to change to by sending them a message
public virtual void CleanUpBeforeSceneChange()
Nothing in here by default, but can be overridden in children classes to do any clean up necessary, e.g destroying objects, saving game etc