示例#1
0
        public void Listen(IPEndPoint tcpListener, IPAddress udpAddress = null, int[] udpListenerPorts = null)
        {
            ThrowIfDisposed();

            var log = Configuration.Logger?
                      .ForContext("TcpEndPoint", tcpListener)
                      .ForContext("UdpAddress", udpAddress)
                      .ForContext("UdpPorts", udpListenerPorts);

            log?.Information("Starting - tcp={TcpEndPoint} udp={UdpAddress} udp-port={UdpPorts}");

            try
            {
                _listenerChannel = new ServerBootstrap()
                                   .Group(_socketListenerThreads, _socketWorkerThreads)
                                   .Channel <TcpServerSocketChannel>()
                                   .Handler(new ActionChannelInitializer <IServerSocketChannel>(ch => { }))
                                   .ChildHandler(new ActionChannelInitializer <ISocketChannel>(ch =>
                {
                    var userMessageHandler = new SimpleMessageHandler();
                    foreach (var handler in Configuration.MessageHandlers)
                    {
                        userMessageHandler.Add(handler);
                    }

                    ch.Pipeline
                    .AddLast(new SessionHandler(this))
                    .AddLast(new ProudFrameDecoder((int)Configuration.MessageMaxLength))
                    .AddLast(new ProudFrameEncoder())
                    .AddLast(new RecvContextDecoder())
                    .AddLast(new CoreMessageDecoder())
                    .AddLast(new CoreMessageEncoder())
                    .AddLast("coreHandler", new SimpleMessageHandler()
                             .Add(new CoreHandler(this)))
                    .AddLast(new SendContextEncoder())
                    .AddLast(new MessageDecoder(Configuration.MessageFactories))
                    .AddLast(new MessageEncoder(Configuration.MessageFactories))

                    // SimpleMessageHandler discards all handled messages
                    // So internal messages(if handled) wont reach the user messagehandler
                    .AddLast(new SimpleMessageHandler()
                             .Add(new ServerHandler()))
                    .AddLast(userMessageHandler)
                    .AddLast(new ErrorHandler(this));
                }))
                                   .ChildOption(ChannelOption.TcpNodelay, !Configuration.EnableNagleAlgorithm)
                                   .ChildOption(ChannelOption.SoRcvbuf, 51200)
                                   .ChildOption(ChannelOption.SoSndbuf, 8192)
                                   .ChildOption(ChannelOption.AllowHalfClosure, false)
                                   .ChildAttribute(ChannelAttributes.Session, default(ProudSession))
                                   .ChildAttribute(ChannelAttributes.Server, this)
                                   .BindAsync(tcpListener).WaitEx();

                if (udpListenerPorts != null)
                {
                    UdpSocketManager.Listen(udpAddress, tcpListener.Address, udpListenerPorts, _socketWorkerThreads);
                }
            }
            catch (Exception ex)
            {
                log?.Error(ex, "Unable to start server - tcp={TcpEndPoint} udp={UdpAddress} udp-port={UdpPorts}");
                ShutdownThreads();
                ex.Rethrow();
            }

            IsRunning = true;
            OnStarted();
            RetryUdpOrHolepunchIfRequired(this, null);
        }
示例#2
0
        public void Listen(IPEndPoint tcpListener, IPAddress udpAddress = null, int[] udpListenerPorts = null, IEventLoopGroup listenerEventLoopGroup = null, IEventLoopGroup workerEventLoopGroup = null)
        {
            ThrowIfDisposed();

            _listenerEventLoopGroup = listenerEventLoopGroup ?? new MultithreadEventLoopGroup(1);
            _workerEventLoopGroup   = workerEventLoopGroup ?? new MultithreadEventLoopGroup();
            try
            {
                _listenerChannel = new ServerBootstrap()
                                   .Group(_listenerEventLoopGroup, _workerEventLoopGroup)
                                   .Channel <TcpServerSocketChannel>()
                                   .Handler(new ActionChannelInitializer <IServerSocketChannel>(ch => { }))
                                   .ChildHandler(new ActionChannelInitializer <ISocketChannel>(ch =>
                {
                    var userMessageHandler = new SimpleMessageHandler();
                    foreach (var handler in Configuration.MessageHandlers)
                    {
                        userMessageHandler.Add(handler);
                    }

                    ch.Pipeline
                    .AddLast(new SessionHandler(this))

                    .AddLast(new ProudFrameDecoder((int)Configuration.MessageMaxLength))
                    .AddLast(new ProudFrameEncoder())

                    .AddLast(new CoreMessageDecoder())
                    .AddLast(new CoreMessageEncoder())

                    .AddLast("coreHandler", new SimpleMessageHandler()
                             .Add(new CoreHandler(this)))

                    .AddLast(new SendContextEncoder())
                    .AddLast(new MessageDecoder(Configuration.MessageFactories))
                    .AddLast(new MessageEncoder(Configuration.MessageFactories))

                    // SimpleMessageHandler discards all handled messages
                    // So internal messages(if handled) wont reach the user messagehandler
                    .AddLast(new SimpleMessageHandler()
                             .Add(new ServerHandler()))

                    .AddLast(userMessageHandler)
                    .AddLast(new ErrorHandler(this));
                }))
                                   .ChildOption(ChannelOption.TcpNodelay, !Configuration.EnableNagleAlgorithm)
                                   .ChildAttribute(ChannelAttributes.Session, default(ProudSession))
                                   .ChildAttribute(ChannelAttributes.Server, this)
                                   .BindAsync(tcpListener).WaitEx();

                if (udpListenerPorts != null)
                {
                    UdpSocketManager.Listen(udpAddress, tcpListener.Address, udpListenerPorts, _workerEventLoopGroup);
                }
            }
            catch (Exception ex)
            {
                _listenerEventLoopGroup.ShutdownGracefullyAsync();
                _listenerEventLoopGroup = null;
                _workerEventLoopGroup.ShutdownGracefullyAsync();
                _workerEventLoopGroup = null;
                ex.Rethrow();
            }

            IsRunning = true;
            OnStarted();
        }