/// <summary> /// Retrieve a player by their ID. /// </summary> private TcpProtocol GetClient(int id) { TcpProtocol p = null; clientsDictionary.TryGetValue(id, out p); return(p); }
/// <summary> /// Add a new player entry. /// </summary> private TcpProtocol AddClient(Socket socket) { TcpProtocol client = new TcpProtocol(); client.StartReceiving(socket); clients.Add(client); return(client); }
/// <summary> /// Remove the specified player. /// </summary> private void RemoveClient(TcpProtocol client) { if (client != null) { client.Release(); clients.Remove(client); if (client.Id != 0) { if (clientsDictionary.Remove(client.Id)) { } client.Id = 0; } } }
/// <summary> /// Receive and process a single incoming packet. /// Returns 'true' if a packet was received, 'false' otherwise. /// </summary> private bool ProcessClientPacket(Buffer buffer, TcpProtocol client) { #if DEBUG Debug.Log("[Listener][TcpProtocol:ProcessClientPacket()] - Processing. Status: " + client.Status); #endif // If the player has not yet been verified, the first packet must be an ID request if (client.Status == ConnectionStatus.Verifying) { if (client.VerifyRequestID(buffer, true)) { #if DEBUG Debug.Log("[Listener][TcpProtocol:ProcessClientPacket()] - Client verified. Id: " + client.Id); #endif clientsDictionary.Add(client.Id, client); if (OnClientConnect != null) { OnClientConnect.Invoke(client.Id, client); } return(true); } RemoveClient(client); return(false); } if (client.Status == ConnectionStatus.Connected) { Debug.Log("[Listener][TcpProtocol:ProcessClientPacket()] - Packet received."); if (OnListenerPacketReceived != null) { OnListenerPacketReceived.Invoke(buffer, client); } } #if DEBUG Debug.Log("[Listener][TcpProtocol:ProcessClientPacket()] - Processed. Status: " + client.Status); #endif return(true); }
private bool ProcessClientPackets() { bool received = false; lock (listenerLockObj) { Buffer buffer; time = DateTime.UtcNow.Ticks / 10000; // Stop the listener if the port is 0 (MakePrivate() was called) if (listenerPort == 0) { if (listener != null) { listener.Stop(); listener = null; } } else { // Add all pending connections while (listener != null && listener.Pending()) { AddClient(listener.AcceptSocket()); } } #if DEBUG Debug.Log("[Listener] - Running ThreadProcessClientPackets. Clients: " + clients.size); #endif // Process player connections next for (int i = 0; i < clients.size;) { TcpProtocol client = clients[i]; // Process up to 100 packets at a time for (int b = 0; b < 100 && client.ReceivePacket(out buffer); ++b) { if (buffer.size > 0) { if (multiThreaded) { try { if (ProcessClientPacket(buffer, client)) { received = true; } } catch (Exception ex) { Debug.LogException(ex); Error("(Listener ThreadFunction Process) " + ex.Message + "\n" + ex.StackTrace); RemoveClient(client); } } else { if (ProcessClientPacket(buffer, client)) { received = true; } } } buffer.Recycle(); } // Time out -- disconnect this player // if (client.Status == ConnectionStatus.Connected) // { // // If the player doesn't send any packets in a while, disconnect him // if (client.TimeoutTime > 0 && client.LastReceivedTime + client.TimeoutTime < time) // { //#if DEBUG // UnityEngine.Debug.LogWarning("[TcpProtocol:StopListener()] - Client " + client.IpAddress + " has timed out"); //#endif // RemoveClient(client); // continue; // } // } // else if (client.LastReceivedTime + 2000 < time) // { //#if DEBUG // UnityEngine.Debug.LogWarning("[TcpProtocol:StopListener()] - Client " + client.IpAddress + " has timed out"); //#endif // RemoveClient(client); // continue; // } ++i; } } return(received); }
protected virtual void OnListenerPacketReceived(Buffer buffer, TcpProtocol source) { }
protected virtual void OnClientConnect(int id, TcpProtocol client) { }