Beispiel #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;
            }
        }
Beispiel #2
0
 public static TcpServer Start <T>(
     EndPoint endPoint,
     IThreadPool threadPool,
     Func <T> peerFactory,
     TcpServerSettings settings = null)
     where T : TcpPeer
 {
     return(new TcpServer(endPoint, threadPool, peerFactory, settings));
 }
Beispiel #3
0
 public static TcpServer Start <T>(
     string ip,
     int port,
     IThreadPool threadPool,
     Func <T> peerFactory,
     TcpServerSettings settings = null)
     where T : TcpPeer
 {
     return(Start(IPAddress.Parse(ip), port, threadPool, peerFactory, settings));
 }
Beispiel #4
0
        public static TcpServer Start <T>(
            IPAddress ip,
            int port,
            IThreadPool threadPool,
            Func <T> peerFactory,
            TcpServerSettings settings = null)
            where T : TcpPeer
        {
            var endPoint = new IPEndPoint(ip, port);

            return(new TcpServer(endPoint, threadPool, peerFactory, settings));
        }