Example #1
0
        private TcpServer(
            EndPoint endPoint,
            IThreadPool threadPool,
            Func <TcpPeer> peerFactory,
            TcpServerSettings settings)
        {
            try
            {
                EndPoint = endPoint;
                Settings = settings ?? TcpServerSettings.Default;
                var acceptArgs = new SocketAsyncEventArgs();
                acceptArgs.Completed += OnAcceptCompleted;
                _connections          = new ConcurrentDictionary <long, TcpServerConnection>();
                _actorSystem          = new ActorSystem(EndPoint.ToString(), threadPool);
                _actorSystem.RegisterFactoryOf(peerFactory);
                _tcpServerConnectionNumber = 0;
                _socket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                _socket.Bind(endPoint);
                _socket.Listen(Settings.Backlog);
                _receiveBufferPool = new FixedArrayPool <byte>(Settings.ReceiveBufferSize, 10000);
                StartAccept(_socket, acceptArgs);
            }
            catch
            {
                if (_socket != null)
                {
                    _socket.Close();
                    _socket.Dispose();
                }

                throw;
            }
        }
Example #2
0
 internal static TcpServerConnection Accept(
     TcpServer server,
     long number,
     Socket socket,
     ActorRef <TcpPeer> peer,
     FixedArrayPool <byte> buffers)
 {
     return(new TcpServerConnection(server, number, socket, peer, buffers));
 }
Example #3
0
        private TcpServerConnection(
            TcpServer server,
            long number,
            Socket socket,
            ActorRef <TcpPeer> peer,
            FixedArrayPool <byte> buffers)
        {
            _server    = server;
            _number    = number;
            _socket    = socket;
            _peer      = peer;
            _buffers   = buffers;
            _connected = true;

            _receive            = new SocketAsyncEventArgs();
            _receive.Completed += OnReceiveCompleted;

            StartReceive(socket, _receive);
        }