/// <summary> /// Spawns a worker thread to handle the client /// </summary> /// <param name="protoClient"></param> private void AcceptClient(ProtoServerClient protoClient) { Logger.Server.DebugLine($"Client connect: {protoClient.Client.Client.RemoteEndPoint}"); // Queue the communication into new thread // When that thread disconnects, remove that client from the client list var newThread = new Thread(o => { try { protoClient.HandleForever(); } catch (Exception) { } finally { RemoveClient(protoClient); } }); protoClient.AcceptorThread = newThread; m_Clients.Add(protoClient); newThread.Start(); }
/// <summary> /// Removes a current connected client from the active list /// </summary> /// <param name="client">The client</param> private void RemoveClient(ProtoServerClient client) { Logger.Server.DebugLine($"Client disconnect: {client.Client.Client.RemoteEndPoint}"); m_Clients.Remove(client); // Close resources just in case client.Client.Close(); client.AcceptorThread.Abort(); }
/// <summary> /// Accept clients from the endpoint and handle their threads /// </summary> private void ServerMainThread() { Logger.Server.DebugLine($"Starting server @ {m_Endpoint}"); while (true) { var client = m_Listener.AcceptTcpClient(); var protoClient = new ProtoServerClient(client); AcceptClient(protoClient); } }