private Func <DotNettyData, DotNettyData> CreateRecieveServiceRequestHandler(RecieveServiceRequestDelegate recieveServiceRequestDelegate) { if (recieveServiceRequestDelegate == null) { return(null); } return(new Func <DotNettyData, DotNettyData>((message) => { return recieveServiceRequestDelegate(message); })); }
ServerBootstrap SetupBootstrap(RecieveServiceRequestDelegate recieveServiceRequest) { var serviceRecieveHandler = CreateRecieveServiceRequestHandler(recieveServiceRequest); return(new ServerBootstrap() .Group(this.parentEventLoopGroup, this.eventLoopGroup) .Channel <TcpServerSocketChannel>() .Option(ChannelOption.SoBacklog, ListenBacklogSize) .ChildOption(ChannelOption.TcpNodelay, true) .ChildOption(ChannelOption.SoKeepalive, true) .ChildOption(ChannelOption.Allocator, PooledByteBufferAllocator.Default) .Handler(new LoggingHandler("CommServerErrror", LogLevel.ERROR)) .ChildHandler(new ActionChannelInitializer <ISocketChannel>(channel => { IChannelPipeline pipeline = channel.Pipeline; //pipeline.AddLast("IdleStateHandler", new IdleStateHandler(0, 0, 30)); pipeline.AddLast("framing-enc", new LengthFieldPrepender(4)); pipeline.AddLast("framing-dec", new LengthFieldBasedFrameDecoder(Int32.MaxValue, 0, 4, 0, 4)); pipeline.AddLast("comm-ecode", new CommEncoder()); pipeline.AddLast("comm-decode", new CommDecoder()); pipeline.AddLast("comm-handler", new ServerServiceHandler(serviceRecieveHandler)); //pipeline.AddLast("IdleStateHearBeatReqHandler", new IdleStateHearBeatReqHandler()); }))); }
public async Task RunAsync(int threadCount, CancellationToken cancellationToken, RecieveServiceRequestDelegate recieveServiceRequest) { Contract.Requires(threadCount > 0); try { LogsManager.Info("通用服务开始启动!"); this.parentEventLoopGroup = new MultithreadEventLoopGroup(1); this.eventLoopGroup = new MultithreadEventLoopGroup(threadCount); ServerBootstrap bootstrap = this.SetupBootstrap(recieveServiceRequest); this.serverChannel = await bootstrap.BindAsync(port); cancellationToken.Register(this.CloseAsync); LogsManager.Info("通用服务成功启动!"); } catch (Exception ex) { LogsManager.Error("通用服务启动失败"); this.CloseAsync(); } }
private Func <byte[], IDictionary <string, byte[]>, LoginState, Task <ResponseData> > CreateRecieveServiceRequestHandler(RecieveServiceRequestDelegate recieveServiceRequestDelegate) { if (recieveServiceRequestDelegate == null) { return(null); } return(new Func <byte[], IDictionary <string, byte[]>, LoginState, Task <ResponseData> >((message, attachments, loginState) => { return recieveServiceRequestDelegate(message, attachments, loginState); })); }