예제 #1
0
        public void Listen <H>(Int16 port,
                               Int32 RCV_BUFF      = 32 * 1024, Int32 SND_BUFF = 32 * 1024,
                               Int32 READ_TIME_OUT = 30, Int32 WRITE_TIME_OUT  = 30, Int32 RW_TIME_OUT = 30)
            where H : ByteToMessageDecoder, new()
        {
            var bootstrap = new ServerBootstrap();

            bootstrap
            .Group(threads)
            .Channel <TcpServerSocketChannel>()
            .Option(ChannelOption.SoBacklog, BackLog)
            .Option(ChannelOption.SoRcvbuf, RCV_BUFF)
            .Option(ChannelOption.SoSndbuf, SND_BUFF)
            .Option(ChannelOption.TcpNodelay, true)
            .Option(ChannelOption.SoReuseaddr, true)
            .Option(ChannelOption.SoKeepalive, true)
            .Option(ChannelOption.Allocator, PooledByteBufferAllocator.Default)

            .ChildAttribute(PLAYER_ID, new RefValue <Int64>(0))
            .ChildAttribute(IP_ADDR, new IPEndPoint(IPAddress.Any, 0))
            .ChildAttribute(OPEN_ID, "")
            .ChildAttribute(SERVER_ID, new RefValue <Int64>(0))
            .ChildAttribute(ACTIVE_TIME, new RefValue <Int64>(0))
            .ChildAttribute(SESSION_ID, new RefValue <Int64>(0))

            .ChildHandler(new ActionChannelInitializer <ISocketChannel>(channel =>
            {
                if (channel.RemoteAddress is IPEndPoint)
                {
                    channel.GetAttribute(IP_ADDR).Set(channel.RemoteAddress as IPEndPoint);
                }
                channel.GetAttribute(ACTIVE_TIME).Set(Platform.GetSeconds());
                Singleton <ConnManager> .Instance.AddNewConntion(channel);
                channel.GetAttribute(SESSION_ID).Get().Value = ConnectionSessionID++;

                IChannelPipeline pipeline = channel.Pipeline;
                pipeline.AddLast("TimeOut", new IdleStateHandler(READ_TIME_OUT, WRITE_TIME_OUT, RW_TIME_OUT));
                pipeline.AddLast(TypeInstance <H> .New());

                LoggerProvider.Logger.Trace("NewSession SessionID:{0} IpAddr:{1}", channel.GetSessionID(), channel.GetIpAddr());
            }));

            Task.WaitAll(bootstrap.BindAsync(port));
            LoggerProvider.Logger.Trace("Listen Port:{0} Type:{1}", port);
            ports.Add(bootstrap);
        }