public async Task BindWebSocketAsync(int port, string websocketPath, IMessageHandlerFactory handlerFactory) { factoryContext[port] = (channel) => { var info = this.channelSessionInfoFactory.NewSessionInfo(handlerFactory); info.ConnectionType = ConnectionType.WebSocket; channel.GetAttribute(ChannelExt.SESSION_INFO).Set(info); var localPort = (channel.LocalAddress as IPEndPoint).Port; info.RemoteAddress = channel.RemoteAddress as IPEndPoint; this.connectionManager.AddConnection(channel); IChannelPipeline pipeline = channel.Pipeline; pipeline.AddLast("TimeOut", new IdleStateHandler(this.config.ReadTimeout, this.config.WriteTimeout, this.config.ReadTimeout)); pipeline.AddLast(new HttpServerCodec()); pipeline.AddLast(new HttpObjectAggregator(65536)); pipeline.AddLast(new WebSocketServerCompressionHandler()); pipeline.AddLast(new WebSocketServerProtocolHandler( websocketPath: websocketPath, subprotocols: null, allowExtensions: true, maxFrameSize: 65536, allowMaskMismatch: true, checkStartsWith: false, dropPongFrames: true, enableUtf8Validator: false)); pipeline.AddLast(new WebSocketServerHttpHandler()); pipeline.AddLast(new WebSocketFrameAggregator(65536)); pipeline.AddLast(handlerFactory.NewHandler()); logger.LogInformation("NewWebSocketSession SessionID:{0} IpAddr:{1}, CodecName:{2}", info.SessionID, info.RemoteAddress?.ToString(), handlerFactory.Codec.CodecName); }; var bootstrap = this.MakeBootStrap(); bootstrap.ChildHandler(new ActionChannelInitializer <IChannel>((channel) => { var port = (channel.LocalAddress as IPEndPoint).Port; var factory = this.factoryContext[port]; factory(channel); })); await bootstrap.BindAsync(port); ports.Add(bootstrap); logger.LogInformation("Listen Port:{0}, {1}, Codec:{2}", port, handlerFactory.NewHandler().GetType(), handlerFactory.Codec.GetType()); }
public async Task BindAsync(int port, IMessageHandlerFactory handlerFactory) { factoryContext[port] = (channel) => { var info = this.channelSessionInfoFactory.NewSessionInfo(handlerFactory); info.ConnectionType = ConnectionType.Socket; channel.GetAttribute(ChannelExt.SESSION_INFO).Set(info); var localPort = (channel.LocalAddress as IPEndPoint).Port; info.RemoteAddress = channel.RemoteAddress as IPEndPoint; this.connectionManager.AddConnection(channel); IChannelPipeline pipeline = channel.Pipeline; pipeline.AddLast("TimeOut", new IdleStateHandler(this.config.ReadTimeout, this.config.WriteTimeout, this.config.ReadTimeout)); pipeline.AddLast(handlerFactory.NewHandler()); logger.LogInformation("NewSession SessionID:{0} IpAddr:{1}, CodecName:{2}", info.SessionID, info.RemoteAddress?.ToString(), handlerFactory.Codec.CodecName); }; var bootstrap = this.MakeBootStrap(); bootstrap.ChildHandler(new ActionChannelInitializer <IChannel>((channel) => { var port = (channel.LocalAddress as IPEndPoint).Port; var factory = this.factoryContext[port]; factory(channel); })); await bootstrap.BindAsync(port); ports.Add(bootstrap); logger.LogInformation("Listen Port:{0}, {1}, Codec:{2}", port, handlerFactory.NewHandler().GetType(), handlerFactory.Codec.GetType()); }
public async Task <IChannel> ConnectAsync(EndPoint address, IMessageHandlerFactory factory) { Bootstrap bootstrap; lock (mutex) { if (!this.bootstraps.TryGetValue(factory, out bootstrap)) { bootstrap = new Bootstrap(); bootstrap .Group(this.group) .Channel <TcpSocketChannel>() .Option(ChannelOption.SoRcvbuf, this.config.RecvWindowSize) .Option(ChannelOption.SoSndbuf, this.config.SendWindowSize) .Option(ChannelOption.Allocator, PooledByteBufferAllocator.Default) .Option(ChannelOption.TcpNodelay, true) .Option(ChannelOption.SoKeepalive, true) .Option(ChannelOption.WriteBufferHighWaterMark, this.config.WriteBufferHighWaterMark) .Option(ChannelOption.WriteBufferLowWaterMark, this.config.WriteBufferLowWaterMark) .Handler(new ActionChannelInitializer <IChannel>(channel => { var info = this.channelSessionInfoFactory.NewSessionInfo(factory); info.ConnectionType = ConnectionType.Socket; channel.GetAttribute(ChannelExt.SESSION_INFO).Set(info); IChannelPipeline pipeline = channel.Pipeline; pipeline.AddLast("TimeOut", new IdleStateHandler(this.config.ReadTimeout, this.config.WriteTimeout, this.config.ReadTimeout)); pipeline.AddLast(factory.NewHandler()); logger.LogInformation("New Client Session, SessionID:{0}", channel.GetSessionInfo().SessionID); })); this.bootstraps.TryAdd(factory, bootstrap); } } var channel = await bootstrap.ConnectAsync(address).ConfigureAwait(false); channel.GetSessionInfo().RemoteAddress = channel.RemoteAddress as IPEndPoint; this.connectionManager.AddConnection(channel); return(channel); }