Beispiel #1
0
            private static void acceptConnection(Socket clientSocket)
            {
                IPEndPoint clientEndpoint = (IPEndPoint)clientSocket.RemoteEndPoint;

                // Add timeouts and set socket options
                //clientSocket.ReceiveTimeout = 5000;
                //clientSocket.SendTimeout = 5000;
                clientSocket.LingerState = new LingerOption(true, 3);
                clientSocket.NoDelay     = true;
                clientSocket.Blocking    = true;

                if (!Node.isAcceptingConnections())
                {
                    Thread.Sleep(100); // wait a bit for check connectivity purposes
                    clientSocket.Send(CoreProtocolMessage.prepareProtocolMessage(ProtocolMessageCode.bye, new byte[1]));
                    clientSocket.Shutdown(SocketShutdown.Both);
                    clientSocket.Disconnect(true);
                    return;
                }

                lastIncomingConnectionTime = DateTime.UtcNow;
                connectable = true;

                // Setup the remote endpoint
                RemoteEndpoint remoteEndpoint = new RemoteEndpoint();

                lock (connectedClients)
                {
                    if (connectedClients.Count + 1 > CoreConfig.maximumServerMasterNodes)
                    {
                        Logging.warn(string.Format("Maximum number of connected clients reached. Disconnecting client: {0}:{1}",
                                                   clientEndpoint.Address.ToString(), clientEndpoint.Port));
                        clientSocket.Send(CoreProtocolMessage.prepareProtocolMessage(ProtocolMessageCode.bye, new byte[1]));
                        clientSocket.Shutdown(SocketShutdown.Both);
                        clientSocket.Disconnect(true);
                        return;
                    }

                    var existing_clients = connectedClients.Where(re => re.remoteIP.Address == clientEndpoint.Address);
                    if (existing_clients.Count() > 0)
                    {
                        Logging.warn(String.Format("Client {0}:{1} already connected as {2}.",
                                                   clientEndpoint.Address.ToString(), clientEndpoint.Port, existing_clients.First().ToString()));
                        clientSocket.Send(CoreProtocolMessage.prepareProtocolMessage(ProtocolMessageCode.bye, new byte[1]));
                        clientSocket.Shutdown(SocketShutdown.Both);
                        clientSocket.Disconnect(true);
                        return;
                    }

                    connectedClients.Add(remoteEndpoint);

                    Logging.info(String.Format("Client connection accepted: {0} | #{1}/{2}", clientEndpoint.ToString(), connectedClients.Count + 1, CoreConfig.maximumServerMasterNodes));

                    remoteEndpoint.start(clientSocket);
                }
            }