Exemplo n.º 1
0
        private void Run()
        {
            while (running)
            {
                try
                {
                    INetworkTransport transport = listener.AcceptClient(0);
                    logger.Info("New connection from : " + transport.RemoteEndPoint);

                    ClientConnection channel = ClientConnection.CreateClientConnection(transport, Envelope);
                    lock (clientLock)
                    {
                        clients.Add(channel);
                    }

                    OnChannelConnected(this, new ClientEventArgs(channel));
                    channel.ClientClosed += OnClientClosed;
                }
                catch (SocketException)
                {
                    // This is here because AcceptTcpClient throws an exception when we tell it
                    // stop listening.
                    logger.Debug("ChannelListener shutdown.  No longer accepting connections");
                }
                catch (IOException ex)
                {
                    logger.Warn(ex.Message, ex);
                    if (ex.InnerException != null)
                    {
                        logger.Warn("Inner Exception: " + ex.InnerException.Message, ex.InnerException);
                    }
                }
                catch (Exception ex)
                {
                    // Catch everything to make sure server remains running
                    logger.Fatal("Exception catchall: " + ex.Message, ex);
                    if (ex.InnerException != null)
                    {
                        logger.Fatal("Inner Exception: " + ex.InnerException.Message, ex.InnerException);
                    }
                    running = false;
                    listener.Stop();
                }
            }
        }
Exemplo n.º 2
0
        private void ServerMain()
        {
            listener.Start();

            while (!IsStopped)
            {
                try
                {
                    INetworkTransport client = listener.AcceptClient();

                    if (client != null)
                    {
                        ClientConnection channel = ClientConnection.CreateClientConnection(
                            new ProtoBuffEnvelope(registry),
                            client);

                        channel.ClientId = Guid.NewGuid();

                        Logger.Info("New connection from : " + client.RemoteEndPoint);

                        lock (clientSync)
                        {
                            _connectionList.Add(channel.ClientId, channel);
                        }

                        OnClientConnected(channel.ClientId, channel, client.RemoteEndPoint.ToString());

                        channel.ClientClosed    += OnClientDisconnected;
                        channel.MessageReceived += OnMessageReceived;
                    }
                }
                catch (SocketException)
                {
                    // This is here because AcceptTcpClient throws an exception when we tell it
                    // stop listening.
                    Logger.Debug("SocketServer shutdown.  No longer accepting connections");
                }
            }
        }