Ejemplo n.º 1
0
 public void TestSetup()
 {
     serverInstance = new ServerInstance(DEFAULT_PORT, 2);
     socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     serverInstance.ClientConnected += (sender, args) => session = (Session) args.Session;
     serverInstance.Start();
     socket.Connect(DEFAULT_HOST, DEFAULT_PORT);
     while (session == null)
     {
         Thread.Sleep(10);
     }
     Assert.IsNotNull(session);
 }
Ejemplo n.º 2
0
        private void SocketAcceptCallback(IAsyncResult ar)
        {
            var socket = ar.AsyncState as Socket;
            if (socket != null)
            {
                try
                {
                    var clientSocket = socket.EndAccept(ar);
                    if (Sessions.Count == MaxSessions)
                    {
                        clientSocket.Close();
                    }
                    else
                    {
                        var session = new Session(clientSocket);
                        session.Closed += OnSessionClosed;

                        Sessions.Add(session);
                        InvokeClientConnected(session);
                        socket.BeginAccept(SocketAcceptCallback, socket);
                    }
                }
                catch (ObjectDisposedException)
                {
                    log.Debug("Listening socket was closed");
                    InvokeStopped();
                }
                catch (Exception ex)
                {
                    log.Debug("Exception occured while accepting a new client connection:",ex);
                    InvokeStopped();
                }
            }
        }