Client - nixfanboy/WrathNet GitHub Wiki

The Client

In a Server->Client infrastructure, the Client connects to a Server that is already listening for Clients on a certain address and port. The Client connects with a specified address and port on the protocol assigned in the Constructor.

Design

The Client class is a surface class that contains a Manager class that is dependent on the Protocol being used. The Class in charge of protocol and data management would be the ClientManager class, with direct subclasses ClientTcpManager, ClientUdpManager, and ClientRudpManager.

Multi-threading

The WrathNet Engine uses two background threads for each client, one listening for new data coming in (recvThread)(in order to retrieve and organize data as quickly as possible, without missing any data) and another for executing the code specified in the ClientListener according to the onReceive() method overridden by the developer (execThread). This thread is also responsible for detecting compressed and/or encrypted data and decompressing and/or decrypting it.

The Client Listener

The ClientListener Interface is used by the developer to specify what behaviors are desired when an event occurs with the Client. The onReceive(Client, Packet) method is called after a Packet is received from specified Client and all of the data is decompressed and decrypted as appropriate. Due to the interface code being run on its own thread (which is shared with decompression and decryption operations), it is safe to call methods on the Client in the onReceive method, such as send(byte[]).

The onConnect(Client) method indicates when a Client successfully connects to a Server. This method is called after all related threads are running and the ConnectionState of the Client is appropriately set to ConnectionState.CONNECTED. It is safe to call methods to the Client, such as send(byte[]), from here.

The onDisconnect(Client) method indicates when a Client successfully (and intentionally) disconnects from a Server. This method is called after all of the resources to the previous connection has been unbound and cleared. Therefore, it is not safe to call methods on the Client that depend on an active connection, such as send(byte[]). However, it is still safe to call methods that do not require an active connection, or require no active connection, such as connect(String, int).