/// <summary> /// Assigns a new NetworkConnection a unique clientID. /// </summary> /// <param name="newConnection"></param> private short assignClientID(NetworkConnection newConnection) { short clientID = totalClientsConnected++; connectedClients.Add(clientID, newConnection); sendMessageToClient(MessageFactory.createClientIDMessage(clientID), clientID, true); OnClientConnected?.Invoke(clientID); //Notify any observers of the new connection return(clientID); }
public void ReadFromClient() { byte[] bytes = new byte[1024]; while (isProcessing) { if (IsConnected) { try { if (Client == null) { continue; } if (!SocketConnected(Client)) { Client = null; FindClient(); break; } Debug.Log("Waiting for data from socket . . ."); int length = Client.Receive(bytes); if (length != 0) { data = Encoding.UTF8.GetString(bytes, 0, length); Debug.Log("Recieved text: " + data); OnMessageRecieved.Invoke(data); } } catch (SocketException socketException) { Debug.Log("ReadFromClient: " + socketException.ToString()); } } } }
/// <summary> /// Process any new network events. /// </summary> private void processNetworkEvents() { DataStreamReader stream; //Used for reading data from data network events if (hasConnections) //There are clients connected { List <short> clientIDs = new List <short>(connectedClients.Keys); for (int i = 0; i < clientIDs.Count; i++) { short clientID = clientIDs[i]; NetworkConnection connection = connectedClients[clientID]; if (!connection.IsCreated) { Assert.IsTrue(true); } //Get network events for the connection NetworkEvent.Type networkEvent; while ((networkEvent = networkDriver.PopEventForConnection(connection, out stream)) != NetworkEvent.Type.Empty) { if (networkEvent == NetworkEvent.Type.Data) //Connection sent data { MessageParser.parse(stream); //Parse data } else if (networkEvent == NetworkEvent.Type.Disconnect) //Connection disconnected { Debug.Log("<color=magenta><b>[Server]</b></color> Client " + clientID + " disconnected from server."); OnClientDisconnected?.Invoke(clientID); //Notify any observers of the disconnection event clientMessageQueues.Remove(clientID); //Remove any queued messages for the connection connection = default; //This ensures the connections will be cleaned up in cleanupConnections() } } } } }
/// <summary> /// Handles the event of the client reconnecting to the server. /// This is used for maintaining associations between client IDs and NetworkConnections even if the client disconnects and reconnects. /// </summary> /// <param name="clientsideID">ID of the client on the client (previously assigned client ID)</param> /// <param name="serversideID">ID of the client on the server (newly assigned client ID)</param> internal void handleClientReconnected(short clientsideID, short serversideID) { if (connectedClients.ContainsKey(clientsideID)) //Client ID is already associated with a connection { if (connectedClients.ContainsKey(serversideID)) //Serverside ID is associated with the active client connection { //Associate active connection with the original clientID connectedClients[clientsideID] = connectedClients[serversideID]; //Transfer any queued messages for new client to old client if (clientMessageQueues.ContainsKey(serversideID)) { initializeClientMessageQueue(clientsideID); //Get messages queued to new client ID Queue <QueuedMessage> queuedMsgs = clientMessageQueues[serversideID]; //Transfer queued messages to the original client ID's message queue while (queuedMsgs.Count > 0) { clientMessageQueues[clientsideID].Enqueue(queuedMsgs.Dequeue()); } } //Remove message queue for the serverside ID clientMessageQueues.Remove(serversideID); //Remove the serverside client ID connection connectedClients.Remove(serversideID); Debug.Log("<color=magenta><b>[Server]</b></color> Client " + clientsideID + " reconnected and identified itself as the new client, " + serversideID); OnClientReconnected?.Invoke(new ClientIdentification(clientsideID, serversideID)); } } }