use of io.netty.handler.codec.string.StringEncoder in project tech by ffyyhh995511.
the class HelloClientInitializer method initChannel.
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
/*
* 这个地方的 必须和服务端对应上。否则无法正常解码和编码
*
* 解码和编码 我将会在下一张为大家详细的讲解。再次暂时不做详细的描述
*
* */
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new StringDecoder());
pipeline.addLast("encoder", new StringEncoder());
// 客户端的逻辑
pipeline.addLast("handler", new HelloClientHandler());
}
use of io.netty.handler.codec.string.StringEncoder in project x-pipe by ctripcorp.
the class ProxyTest method testCloseOnBothSide.
@Test
public void testCloseOnBothSide() throws Exception {
serverBootstrap().childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new StringEncoder());
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
logger.info("[channelRead] msg: {}", msg);
super.channelRead(ctx, msg);
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
logger.info("[channelUnregistered] {}", ChannelUtil.getDesc(ctx.channel()));
super.channelUnregistered(ctx);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
logger.info("[channelInactive] {}", ChannelUtil.getDesc(ctx.channel()));
super.channelInactive(ctx);
}
});
}
}).bind(8009).sync();
ChannelFuture future = clientBootstrap().connect("127.0.0.1", 8992).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
future.channel().writeAndFlush(new SimpleStringParser("Proxy Route proxy://127.0.0.1:8009").format());
}
});
future.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
future.channel().close().sync();
}
});
Thread.sleep(1000 * 3);
}
use of io.netty.handler.codec.string.StringEncoder in project x-pipe by ctripcorp.
the class ProxyTest method testSendFrequently.
@Test
public void testSendFrequently() throws InterruptedException {
AtomicInteger counter = new AtomicInteger(0);
int N = 10000;
serverBootstrap().childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new StringEncoder());
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
Assert.assertTrue(msg instanceof String);
String message = (String) msg;
Assert.assertTrue(message.toLowerCase().contains("ok") || message.toLowerCase().contains("proxy"));
if (message.toLowerCase().contains("ok")) {
String[] strs = message.split("\r\n");
for (String str : strs) {
if (str.toLowerCase().contains("ok")) {
counter.getAndIncrement();
}
}
}
super.channelRead(ctx, msg);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
// Assert.assertEquals(N, counter.get());
logger.info("[complete] N: {} counter: {}, N == counter: {}", N, counter.get(), N == counter.get());
System.out.println("Complete" + counter.get());
super.channelInactive(ctx);
}
});
}
}).bind(8009).sync();
ChannelFuture future = clientBootstrap().connect("127.0.0.1", 8992).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
future.channel().writeAndFlush(new SimpleStringParser("Proxy Route proxy://127.0.0.1:8009").format());
Thread.sleep(1);
for (int i = 0; i < N; i++) {
future.channel().writeAndFlush(new SimpleStringParser("OK").format());
}
}
});
Thread.sleep(1000 * 10);
logger.info("[complete] N: {} counter: {}, N == counter: {}", N, counter.get(), N == counter.get());
}
use of io.netty.handler.codec.string.StringEncoder in project x-pipe by ctripcorp.
the class ProxyTest method testCloseOnBothSide2.
@Test
public void testCloseOnBothSide2() throws Exception {
serverBootstrap().childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new StringEncoder());
ch.pipeline().addLast(new StringDecoder());
ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
logger.info("[channelRead] msg: {}", msg);
ctx.channel().close();
super.channelRead(ctx, msg);
}
@Override
public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
logger.info("[channelUnregistered] {}", ChannelUtil.getDesc(ctx.channel()));
super.channelUnregistered(ctx);
}
@Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
logger.info("[channelInactive] {}", ChannelUtil.getDesc(ctx.channel()));
super.channelInactive(ctx);
}
});
}
}).bind(8009).sync();
ChannelFuture future = clientBootstrap().connect("127.0.0.1", 8992).addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
future.channel().writeAndFlush(new SimpleStringParser("Proxy Route proxy://127.0.0.1:8009").format());
}
});
future.channel().closeFuture().addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
logger.info("[close] close future");
}
});
Thread.sleep(1000 * 3);
}
use of io.netty.handler.codec.string.StringEncoder in project x-pipe by ctripcorp.
the class SecureChatServerInitializer method initChannel.
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
// Add SSL handler first to encrypt and decrypt everything.
// In this example, we use a bogus certificate in the server side
// and accept any invalid certificates in the client side.
// You will need something more complicated to identify both
// and server in the real world.
pipeline.addLast(createSslHandler(initSSLContext()));
// On top of the SSL handler, add the text line codec.
pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
pipeline.addLast(new StringDecoder());
pipeline.addLast(new StringEncoder());
// and then business logic.
pipeline.addLast(new SecureChatServerHandler());
}
Aggregations