io.netty.handler.codec.LengthFieldBasedFrameDecoder Java Examples
The following examples show how to use
io.netty.handler.codec.LengthFieldBasedFrameDecoder.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NodeServerBootstrap.java From GoPush with GNU General Public License v2.0 | 6 votes |
@PostConstruct public void start() throws Exception { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workGroup) .channelFactory(NioServerSocketChannel::new) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { ChannelPipeline pipeline = socketChannel.pipeline(); pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); pipeline.addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8)); pipeline.addLast("frameEncoder", new LengthFieldPrepender(4)); pipeline.addLast("stringEncoder", new StringEncoder(CharsetUtil.UTF_8)); pipeline.addLast("idleStateHandler", new IdleStateHandler(300, 0, 0)); pipeline.addLast("handler", nodeChannelInBoundHandler); } }) .option(ChannelOption.TCP_NODELAY, true) .childOption(ChannelOption.SO_REUSEADDR, true) .option(ChannelOption.SO_SNDBUF, 2048) .option(ChannelOption.SO_RCVBUF, 1024); bootstrap.bind(goPushNodeServerConfig.getNodePort()).sync(); log.info("Node server start successful! listening port: {}", goPushNodeServerConfig.getNodePort()); }
Example #2
Source File: NettyRpcServerChannelInitializer.java From tx-lcn with Apache License 2.0 | 6 votes |
@Override protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast(new LengthFieldPrepender(4, false)); ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); ch.pipeline().addLast(new IdleStateHandler(managerProperties.getCheckTime(), managerProperties.getCheckTime(), managerProperties.getCheckTime(), TimeUnit.MILLISECONDS)); ch.pipeline().addLast(new ObjectSerializerEncoder()); ch.pipeline().addLast(new ObjectSerializerDecoder()); ch.pipeline().addLast(rpcCmdDecoder); ch.pipeline().addLast(new RpcCmdEncoder()); ch.pipeline().addLast(socketManagerInitHandler); ch.pipeline().addLast(rpcAnswerHandler); }
Example #3
Source File: ChanelInitializerHandler.java From netty-pubsub with MIT License | 6 votes |
@Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new IdleStateHandler(0,0,35)); pipeline.addLast(new IdleStateTrigger()); //��ư��ĸ�ʽ 1�ֽڹ̶���ͷ 1�ֽڹ����� 1�ֽڣ��ж��Ƿ����topic�ֶΣ� 4�ֽڹ̶������ֶ� 12�ֽڹ̶�topic���DZ��룩 ʣ���ֽ����� pipeline.addLast(new LengthFieldBasedFrameDecoder(2048, 3, 4, 0, 0)); pipeline.addLast(new MessageToPoDecoder()); //�����֤�Ĵ����� //pipeline.addLast("auth",new AuthenticationHandler()); //���Э�鴦���� pipeline.addLast( "message-process", new MessageProcessHandler()); pipeline.addLast(new MessageEncoder()); //pipeline.addLast("auth",new AuthenticationHandler()); //pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8)); }
Example #4
Source File: PulsarChannelInitializer.java From pulsar with Apache License 2.0 | 6 votes |
@Override public void initChannel(SocketChannel ch) throws Exception { if (tlsEnabled) { if (tlsEnabledWithKeyStore) { ch.pipeline().addLast(TLS_HANDLER, new SslHandler(nettySSLContextAutoRefreshBuilder.get().createSSLEngine())); } else { SslHandler handler = StringUtils.isNotBlank(sniHostName) ? sslContextSupplier.get().newHandler(ch.alloc(), sniHostName, sniHostPort) : sslContextSupplier.get().newHandler(ch.alloc()); ch.pipeline().addLast(TLS_HANDLER, handler); } ch.pipeline().addLast("ByteBufPairEncoder", ByteBufPair.COPYING_ENCODER); } else { ch.pipeline().addLast("ByteBufPairEncoder", ByteBufPair.ENCODER); } ch.pipeline() .addLast("frameDecoder", new LengthFieldBasedFrameDecoder( Commands.DEFAULT_MAX_MESSAGE_SIZE + Commands.MESSAGE_SIZE_FRAME_PADDING, 0, 4, 0, 4)); ch.pipeline().addLast("handler", clientCnxSupplier.get()); }
Example #5
Source File: NettyRpcClientChannelInitializer.java From tx-lcn with Apache License 2.0 | 6 votes |
@Override protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast(new LengthFieldPrepender(4, false)); ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); ch.pipeline().addLast(new ObjectSerializerEncoder()); ch.pipeline().addLast(new ObjectSerializerDecoder()); ch.pipeline().addLast(rpcCmdDecoder); ch.pipeline().addLast(new RpcCmdEncoder()); ch.pipeline().addLast(nettyClientRetryHandler); ch.pipeline().addLast(socketManagerInitHandler); ch.pipeline().addLast(rpcAnswerHandler); }
Example #6
Source File: EppModule.java From nomulus with Apache License 2.0 | 6 votes |
/** * {@link Provides} the list of providers of {@link ChannelHandler}s that are used for the EPP * Protocol. */ @Provides @EppProtocol static ImmutableList<Provider<? extends ChannelHandler>> provideEppHandlerProviders( @EppProtocol Provider<SslClientInitializer<NioSocketChannel>> sslClientInitializerProvider, Provider<LengthFieldBasedFrameDecoder> lengthFieldBasedFrameDecoderProvider, Provider<LengthFieldPrepender> lengthFieldPrependerProvider, Provider<EppMessageHandler> eppMessageHandlerProvider, Provider<EppActionHandler> eppActionHandlerProvider) { return ImmutableList.of( sslClientInitializerProvider, lengthFieldBasedFrameDecoderProvider, lengthFieldPrependerProvider, eppMessageHandlerProvider, eppActionHandlerProvider); }
Example #7
Source File: Client.java From push with Apache License 2.0 | 6 votes |
public void run() { workerGroup = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); // b.option(ChannelOption.SO_KEEPALIVE, true); b.handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(65536, 0, 4, 0, 4)); pipeline.addLast("frameEncoder", new LengthFieldPrepender(4)); pipeline.addLast("decoder", new MsgPackDecode()); pipeline.addLast("encoder", new MsgPackEncode()); pipeline.addLast(ClientConfiguration.clientHandler()); } }); channel = b.connect(clientProperties.getServerHost(), clientProperties.getServerPort()).sync().channel(); status = Status.START; channel.closeFuture().sync(); } catch (Exception e) { e.printStackTrace(); } status = Status.STOP; }
Example #8
Source File: TCPChannelInitializerHandler.java From NettyChat with Apache License 2.0 | 6 votes |
@Override protected void initChannel(Channel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); // netty提供的自定义长度解码器,解决TCP拆包/粘包问题 pipeline.addLast("frameEncoder", new LengthFieldPrepender(2)); pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(65535, 0, 2, 0, 2)); // 增加protobuf编解码支持 pipeline.addLast(new ProtobufEncoder()); pipeline.addLast(new ProtobufDecoder(MessageProtobuf.Msg.getDefaultInstance())); // 握手认证消息响应处理handler pipeline.addLast(LoginAuthRespHandler.class.getSimpleName(), new LoginAuthRespHandler(imsClient)); // 心跳消息响应处理handler pipeline.addLast(HeartbeatRespHandler.class.getSimpleName(), new HeartbeatRespHandler(imsClient)); // 接收消息处理handler pipeline.addLast(TCPReadHandler.class.getSimpleName(), new TCPReadHandler(imsClient)); }
Example #9
Source File: GpbTcpServer.java From Okra with Apache License 2.0 | 6 votes |
@Override protected ChannelHandler newChannelInitializer() { return new ChannelInitializer<NioSocketChannel>() { @Override protected void initChannel(NioSocketChannel ch) throws Exception { ChannelPipeline cp = ch.pipeline(); cp.addLast("frame", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2)); cp.addLast("prepender", FRAME_PREPENDER); cp.addLast("decoder", GPB_DECODER_HANDLER); cp.addLast("encoder", GPB_ENCODER_HANDLER); // handler cp.addLast("handler", serverHandler); // cp.addLast("handler", new ServerHandler()); } }; }
Example #10
Source File: LengthFieldBasedFrameDecoderTest.java From netty4.0.27Learn with Apache License 2.0 | 6 votes |
@Test public void testFailSlowTooLongFrameRecovery() throws Exception { EmbeddedChannel ch = new EmbeddedChannel( new LengthFieldBasedFrameDecoder(5, 0, 4, 0, 4, false)); for (int i = 0; i < 2; i ++) { assertFalse(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0, 0, 2 }))); try { assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0 }))); fail(DecoderException.class.getSimpleName() + " must be raised."); } catch (TooLongFrameException e) { // Expected } ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0, 0, 1, 'A' })); ByteBuf buf = releaseLater((ByteBuf) ch.readInbound()); assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1)); buf.release(); } }
Example #11
Source File: NettyTransportClient.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 6 votes |
private Bootstrap initClientBootstrap() { Bootstrap b = new Bootstrap(); eventLoopGroup = new NioEventLoopGroup(); b.group(eventLoopGroup) .channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, ClusterClientConfigManager.getConnectTimeout()) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { clientHandler = new TokenClientHandler(currentState, disconnectCallback); ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new LengthFieldBasedFrameDecoder(1024, 0, 2, 0, 2)); pipeline.addLast(new NettyResponseDecoder()); pipeline.addLast(new LengthFieldPrepender(2)); pipeline.addLast(new NettyRequestEncoder()); pipeline.addLast(clientHandler); } }); return b; }
Example #12
Source File: MockBrokerService.java From pulsar with Apache License 2.0 | 6 votes |
public void startMockBrokerService() throws Exception { ThreadFactory threadFactory = new ThreadFactoryBuilder().setNameFormat("mock-pulsar-%s").build(); final int numThreads = 2; final int MaxMessageSize = 5 * 1024 * 1024; try { workerGroup = EventLoopUtil.newEventLoopGroup(numThreads, threadFactory); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(workerGroup, workerGroup); bootstrap.channel(EventLoopUtil.getServerSocketChannelClass(workerGroup)); bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("frameDecoder", new LengthFieldBasedFrameDecoder(MaxMessageSize, 0, 4, 0, 4)); ch.pipeline().addLast("handler", new MockServerCnx()); } }); // Bind and start to accept incoming connections. listenChannel = bootstrap.bind(0).sync().channel(); } catch (Exception e) { throw e; } }
Example #13
Source File: LengthFieldBasedFrameDecoderTest.java From netty4.0.27Learn with Apache License 2.0 | 6 votes |
@Test public void testFailFastTooLongFrameRecovery() throws Exception { EmbeddedChannel ch = new EmbeddedChannel( new LengthFieldBasedFrameDecoder(5, 0, 4, 0, 4)); for (int i = 0; i < 2; i ++) { try { assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0, 0, 2 }))); fail(DecoderException.class.getSimpleName() + " must be raised."); } catch (TooLongFrameException e) { // Expected } ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0, 0, 0, 0, 1, 'A' })); ByteBuf buf = releaseLater((ByteBuf) ch.readInbound()); assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1)); buf.release(); } }
Example #14
Source File: TcpServer.java From krpc with MIT License | 6 votes |
protected static void run() throws Exception { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup); b.channel(NioServerSocketChannel.class); b.childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); pipeline.addLast("frameEncoder", new LengthFieldPrepender(4)); pipeline.addLast("decoder", new ByteArrayDecoder()); pipeline.addLast("encoder", new ByteArrayEncoder()); // pipeline.addLast("decoder", new StringDecoder(CharsetUtil.UTF_8)); // pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8)); pipeline.addLast(new TcpServerHandler()); } }); // b.bind(IP, PORT).sync(); ChannelFuture f = b.bind(PORT).sync(); // (7) f.channel().closeFuture().sync(); System.out.println("TCP服务器已启动"); }
Example #15
Source File: TCPClient.java From krpc with MIT License | 6 votes |
/** * 初始化Bootstrap * * @return */ public static final Bootstrap getBootstrap() { EventLoopGroup group = new NioEventLoopGroup(); Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class); b.handler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); pipeline.addLast("frameEncoder", new LengthFieldPrepender(4)); pipeline.addLast("decoder", new ByteArrayDecoder()); pipeline.addLast("encoder", new ByteArrayEncoder()); pipeline.addLast("handler", new TcpClientHandler()); } }); return b; }
Example #16
Source File: NetworkServiceImpl.java From ServerCore with Apache License 2.0 | 6 votes |
@Override protected void initChannel(Channel ch) { ChannelPipeline pip = ch.pipeline(); int maxLength = 1048576; int lengthFieldLength = 4; int ignoreLength = -4; int offset = 0; pip.addLast(new LengthFieldBasedFrameDecoder(maxLength, offset, lengthFieldLength, ignoreLength, lengthFieldLength)); pip.addLast(new MessageDecoder(builder.getImessageandhandler())); pip.addLast(new LengthFieldPrepender(4, true)); pip.addLast(new MessageEncoder(builder.getImessageandhandler())); pip.addLast(new MessageExecutor(builder.getConsumer(), builder.getListener())); for (ChannelHandler handler : builder.getExtraHandlers()) { pip.addLast(handler); } }
Example #17
Source File: NettyTransportClient.java From Sentinel with Apache License 2.0 | 6 votes |
private Bootstrap initClientBootstrap() { Bootstrap b = new Bootstrap(); eventLoopGroup = new NioEventLoopGroup(); b.group(eventLoopGroup) .channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, ClusterClientConfigManager.getConnectTimeout()) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { clientHandler = new TokenClientHandler(currentState, disconnectCallback); ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new LengthFieldBasedFrameDecoder(1024, 0, 2, 0, 2)); pipeline.addLast(new NettyResponseDecoder()); pipeline.addLast(new LengthFieldPrepender(2)); pipeline.addLast(new NettyRequestEncoder()); pipeline.addLast(clientHandler); } }); return b; }
Example #18
Source File: ClientInitializer.java From Summer with Apache License 2.0 | 6 votes |
@Override protected void initChannel(SocketChannel sc) { ChannelPipeline pipeline = sc.pipeline(); ClientConfig config = clientContext.getConfig(); if (ServerConst.SERVER_PROTOCOL_STRING_LINE.equals(config.getProtocol())) { pipeline.addLast(new StringPasswordLineDecoder(config.getMsgLength(), config.getCharset(), config.getPassword())); pipeline.addLast(new StringPasswordLineEncoder(config.getCharset(), config.getPassword())); } else if (ServerConst.SERVER_PROTOCOL_LENGTH_FIELD.equals(config.getProtocol())) { pipeline.addLast(new LengthFieldBasedFrameDecoder(config.getMsgLength(), 0, 4, 0, 4)); pipeline.addLast(new LengthFieldPrepender(4)); pipeline.addLast(new StringPasswordDecoder(config.getCharset(), config.getPassword())); pipeline.addLast(new StringPasswordEncoder(config.getCharset(), config.getPassword())); } else { throw new NotFoundProtocolException(config.getProtocol()); } pipeline.addLast(new ClientStringHandler(clientContext)); }
Example #19
Source File: LengthFieldBasedFrameDecoderTest.java From netty-4.1.22 with Apache License 2.0 | 6 votes |
@Test public void testFailFastTooLongFrameRecovery() throws Exception { EmbeddedChannel ch = new EmbeddedChannel( new LengthFieldBasedFrameDecoder(5, 0, 4, 0, 4)); for (int i = 0; i < 2; i ++) { try { assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0, 0, 2 }))); fail(DecoderException.class.getSimpleName() + " must be raised."); } catch (TooLongFrameException e) { // Expected } ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0, 0, 0, 0, 1, 'A' })); ByteBuf buf = ch.readInbound(); assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1)); buf.release(); } }
Example #20
Source File: LengthFieldBasedFrameDecoderTest.java From netty-4.1.22 with Apache License 2.0 | 6 votes |
@Test public void testFailSlowTooLongFrameRecovery() throws Exception { EmbeddedChannel ch = new EmbeddedChannel( new LengthFieldBasedFrameDecoder(5, 0, 4, 0, 4, false)); for (int i = 0; i < 2; i ++) { assertFalse(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0, 0, 2 }))); try { assertTrue(ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0 }))); fail(DecoderException.class.getSimpleName() + " must be raised."); } catch (TooLongFrameException e) { // Expected } ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 0, 0, 0, 1, 'A' })); ByteBuf buf = ch.readInbound(); assertEquals("A", buf.toString(CharsetUtil.ISO_8859_1)); buf.release(); } }
Example #21
Source File: ServiceChannelInitializer.java From pulsar with Apache License 2.0 | 6 votes |
@Override protected void initChannel(SocketChannel ch) throws Exception { if (sslCtxRefresher != null && this.enableTls) { if (this.tlsEnabledWithKeyStore) { ch.pipeline().addLast(TLS_HANDLER, new SslHandler(nettySSLContextAutoRefreshBuilder.get().createSSLEngine())); } else{ SslContext sslContext = sslCtxRefresher.get(); if (sslContext != null) { ch.pipeline().addLast(TLS_HANDLER, sslContext.newHandler(ch.alloc())); } } } ch.pipeline().addLast("frameDecoder", new LengthFieldBasedFrameDecoder( Commands.DEFAULT_MAX_MESSAGE_SIZE + Commands.MESSAGE_SIZE_FRAME_PADDING, 0, 4, 0, 4)); ch.pipeline().addLast("handler", new ServerConnection(discoveryService)); }
Example #22
Source File: NettyRpcClient.java From litchi with Apache License 2.0 | 6 votes |
public NettyRpcClient(Litchi litchi, String nodeType, String nodeId, String host, int port) { this(nodeType, nodeId, host, port); bootstrap.group(bossGroup).channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(DEFAULT_MAX_FRAME_LEN, 0, 4, 0, 0)); ch.pipeline().addLast(new RpcDecoder()); ch.pipeline().addLast(new RpcEncoder()); ch.pipeline().addLast(new GameEventHandler(litchi)); ch.pipeline().addLast(new RpcCallbackHandler(litchi, futureContext)); ch.pipeline().addLast(new ResponseHandler(litchi)); } }) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.SO_KEEPALIVE, true); }
Example #23
Source File: NettyChatServerInitializer.java From sctalk with Apache License 2.0 | 5 votes |
@Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("framer", new LengthFieldBasedFrameDecoder(400 * 1024, 0, 4, -4, 0)); pipeline.addLast("decoder", new PacketDecoder()); pipeline.addLast("encoder", new PacketEncoder()); pipeline.addLast(new LoggingHandler(LogLevel.DEBUG)); pipeline.addLast("handler", new MessageServerHandler(handlerManager)); }
Example #24
Source File: LionServerChannelInitializer.java From lionrpc with Apache License 2.0 | 5 votes |
protected void initChannel(SocketChannel socketChannel) throws Exception { socketChannel.pipeline() .addLast(new LengthFieldBasedFrameDecoder(65536,0,4,0,0)) .addLast(new MessageDecoder(RequestMessage.class)) .addLast(new MessageEncoder(ResponseMessage.class)) .addLast(new LionServerChannelInboundHanndler(serverHandlerMap,workerCenter)); }
Example #25
Source File: Node.java From GoPush with GNU General Public License v2.0 | 5 votes |
private ChannelInitializer channelInitializer() { return new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel socketChannel) throws Exception { ChannelPipeline pipeline = socketChannel.pipeline(); pipeline.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); pipeline.addLast("stringDecoder", new StringDecoder(CharsetUtil.UTF_8)); pipeline.addLast("frameEncoder", new LengthFieldPrepender(4)); pipeline.addLast("stringEncoder", new StringEncoder(CharsetUtil.UTF_8)); pipeline.addLast("idleStateHandler", new IdleStateHandler(300, 0, 0)); pipeline.addLast("handler", nodeChannelInBoundHandler()); } }; }
Example #26
Source File: MgsServer.java From push with Apache License 2.0 | 5 votes |
@Override public void run() { ServerBootstrap b = new ServerBootstrap();// 引导辅助程序 bossGroup = new NioEventLoopGroup(BIZGROUPSIZE); workerGroup = new NioEventLoopGroup(BIZTHREADSIZE); try { b.group(bossGroup, workerGroup); b.channel(NioServerSocketChannel.class);// 设置nio类型的channel b.childHandler(new ChannelInitializer<SocketChannel>() {// 有连接到达时会创建一个channel protected void initChannel(SocketChannel ch) throws Exception { logger.debug("客户端:{} 初始化", ch.remoteAddress()); // pipeline管理channel中的Handler,在channel队列中添加一个handler来处理业务 ch.pipeline().addLast("frameDecoder", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); ch.pipeline().addLast("frameEncoder", new LengthFieldPrepender(4)); ch.pipeline().addLast("decoder", msgPackDecode); ch.pipeline().addLast("encoder", msgPackEncode); ch.pipeline().addLast(serverHandler); } }); b.option(ChannelOption.SO_BACKLOG, 128); b.childOption(ChannelOption.SO_KEEPALIVE, true); logger.info("server start : {}", port); status = "run"; ChannelFuture f = b.bind(port).sync();// 配置完成,开始绑定server,通过调用sync同步方法阻塞直到绑定成功 channel = f.channel(); f.channel().closeFuture().sync();// 应用程序会一直等待,直到channel关闭 } catch (Exception e) { status = "error"; e.printStackTrace(); } }
Example #27
Source File: ThreadServerSocket.java From werewolf_server with Apache License 2.0 | 5 votes |
public void startSocket() throws InterruptedException { EventLoopGroup boss = new NioEventLoopGroup(); EventLoopGroup worker = new NioEventLoopGroup(); try { ServerBootstrap boot = new ServerBootstrap(); boot.group(boss,worker); boot.channel(NioServerSocketChannel.class); boot.childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(1024*1024,0,4,-4,0,false)); ch.pipeline().addLast(new ByteToPacketCodec()); //ch.pipeline().addLast(new LoginChannelHandler(listener)); ch.pipeline().addLast(new PacketChannelHandler(listener)); } }); boot.option(ChannelOption.SO_BACKLOG,128); boot.childOption(ChannelOption.SO_KEEPALIVE,true); channelFuture = boot.bind(port).sync(); System.out.println("服务器"+port+"开启成功..."); channelFuture.channel().closeFuture().sync(); }finally { boss.shutdownGracefully().sync(); worker.shutdownGracefully().sync(); channelFuture = null; System.out.println("服务器关闭成功..."); } }
Example #28
Source File: BenchmarkServer.java From Okra with Apache License 2.0 | 5 votes |
@Override protected ChannelHandler newChannelInitializer() { return new ChannelInitializer<NioSocketChannel>() { @Override protected void initChannel(NioSocketChannel ch) throws Exception { ChannelPipeline cp = ch.pipeline(); cp.addLast("frame", new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 2, 0, 2)); cp.addLast("prepender", FRAME_PREPENDER); // Any other useful handler cp.addLast("strDecoder", STRING_DECODER); cp.addLast("strEncoder", STRING_ENCODER); cp.addLast("handler", HANDLER); } }; }
Example #29
Source File: NettyServerServiceImpl.java From sds with Apache License 2.0 | 5 votes |
@Override public synchronized void start() { bossGroup = new NioEventLoopGroup(); // (1) workerGroup = new NioEventLoopGroup(); try { b = new ServerBootstrap(); // (2) b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) // (3) .childHandler(new ChannelInitializer<SocketChannel>() { // (4) @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ByteArrayDecoder()); ch.pipeline().addLast(new ByteArrayEncoder()); ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); ch.pipeline().addLast(new IdleStateHandler(heartTime, heartTime, heartTime, TimeUnit.SECONDS)); ch.pipeline().addLast(new DeliveryHandler(deliveryService)); } }) .option(ChannelOption.SO_BACKLOG, 128) // (5) .childOption(ChannelOption.SO_KEEPALIVE, true); // (6) // Bind and start to accept incoming connections. b.bind(settingService.getDeliveryPort()); logger.info("socket: "+settingService.getDeliveryPort()+" starting...."); // Wait until the server socket is closed. // In this example, this does not happen, but you can do that to gracefully } catch (Exception e) { e.printStackTrace(); } }
Example #30
Source File: ConnectionTest.java From sctalk with Apache License 2.0 | 5 votes |
@Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("framer", new LengthFieldBasedFrameDecoder(400 * 1024, 0, 4, -4, 0)); pipeline.addLast("decoder", new ClientPacketDecoder()); pipeline.addLast("encoder", new PacketEncoder()); pipeline.addLast(new LoggingHandler(LogLevel.WARN)); pipeline.addLast("handler", new ClientMessageClientHandler(name)); }