예제 #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Server!");

            IPHostEntry  ipHostInfo       = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress    currentIpAddress = ipHostInfo.AddressList[1];
            ISocketProxy socket           = new SocketProxy(currentIpAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            LingerOption lingerOption     = new LingerOption(true, 0);

            socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger, lingerOption);

            Server server = new Server(socket);

            server.StartServer(currentIpAddress);
            Console.Read();
        }
예제 #2
0
        public override bool Handle(byte[] firstPacket, int length, SocketProxy socket, object state)
        {
            if (NotCompatible(firstPacket, length, socket))
            {
                return(false);
            }
            socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
            var handler = TCPHandlerFactory(_controller, _config, this, socket);

            var handlersToClose = Enumerable.Empty <ITCPHandler>();

            lock (Handlers)
            {
                Handlers.Add(handler);
                var now = DateTime.Now;
                if (NeedSweepTimeoutHandlers(now))
                {
                    _lastSweepTime  = now;
                    handlersToClose = GetTimeoutHandlersList(now);
                }
            }
            foreach (var timeoutHandler in handlersToClose)
            {
                Logging.Debug("Closing timed out TCP connection.");
                timeoutHandler.Close();
            }

            /*
             * Start after we put it into Handlers set. Otherwise if it failed in handler.Start()
             * then it will call handler.Close() before we add it into the set.
             * Then the handler will never release until the next Handle call. Sometimes it will
             * cause odd problems (especially during memory profiling).
             */
            handler.Start(firstPacket, length);

            return(true);
        }
예제 #3
0
        internal void SetSocketOption(SocketOption option, int value)
        {
            EnsureNotDisposed();

            HandleProxyResult(_socketProxy.SetSocketOption((int)option, value));
        }