/// <summary> /// Constructor /// </summary> /// <param name="protocol">Protocol</param> /// <param name="tcpListener">TCP listener</param> private ServerListener(EProtocol protocol, TcpListener tcpListener) { Protocol = protocol; TCPListener = tcpListener; listenerThread = new Thread((that) => { if (that is ServerListener) { ServerListener listener = (ServerListener)that; while (listener.TCPListener != null) { lock (listener.TCPListener) { if (listener.TCPListener.Pending()) { TcpClient tcp_client = listener.TCPListener.AcceptTcpClient(); if (tcp_client != null) { bool allow = (Whitelist.Count <= 0); string address = ((IPEndPoint)(tcp_client.Client.RemoteEndPoint)).Address.ToString(); if (!allow) { allow = Whitelist.Contains(address); } if (allow) { allow = !(Bans.Contains(address)); } if (allow) { ServerConnection server_connection = new ServerConnection(tcp_client, protocol); lock (listener.serverConnections) { listener.serverConnections.Add(server_connection); } OnClientConnectionAccepted?.Invoke(server_connection); } else { OnClientConnectionDenied?.Invoke(tcp_client); tcp_client.Dispose(); } } } } lock (listener.serverConnections) { List <Tuple <int, ServerConnection> > dispose_server_connections = null; for (int i = 0; i < listener.serverConnections.Count; i++) { ServerConnection server_connection = listener.serverConnections[i]; if ((!(server_connection.CanReceive)) && (!(server_connection.CanSend))) { if (dispose_server_connections == null) { dispose_server_connections = new List <Tuple <int, ServerConnection> >(); } dispose_server_connections.Add(new Tuple <int, ServerConnection>(i, server_connection)); } } dispose_server_connections.Reverse(); foreach (Tuple <int, ServerConnection> server_connection in dispose_server_connections) { listener.serverConnections.RemoveAt(server_connection.Item1); } } Thread.Sleep(20); } } }); listenerThread.Start(this); }
/// <summary> /// Constructor /// </summary> /// <param name="serverListener">Server listener</param> /// <param name="maxUsers">Maximal amount of users</param> internal ServerLobby(ServerListener serverListener, uint maxUsers) { this.serverListener = serverListener; users = new Pool <IUser>(0, maxUsers); serverListener.OnClientConnectionAccepted += ClientConnectionAcceptedEvent; }