use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder in project baseio by generallycloud.
the class TestLoadEchoClient1 method prepare.
@Override
public void prepare() throws Exception {
eventHandleAdaptor = new ChannelInboundHandlerAdapter() {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
addCount(1024);
}
};
Bootstrap b = new Bootstrap();
b.group(group);
b.channel(NettyUtil.newSocketChannel());
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected 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 StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast("handler", eventHandleAdaptor);
}
});
f = b.connect("localhost", 8300).sync();
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder in project baseio by generallycloud.
the class MyNettyServer method service.
public static void service() throws Exception {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup);
bootstrap.channel(NettyUtil.newServerSocketChannel());
bootstrap.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new IdleStateHandler(5, 10, 20));
pipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
pipeline.addLast(new LengthFieldPrepender(4));
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast(new TcpServerHandler());
}
});
ChannelFuture f = bootstrap.bind(IP, PORT).sync();
f.channel().closeFuture().sync();
System.out.println("TCP服务器已启动");
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder in project baseio by generallycloud.
the class NettyClientThread method main.
public static void main(String[] args) throws Exception {
EventLoopGroup group = NettyUtil.newEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group);
b.channel(NettyUtil.newSocketChannel()).option(ChannelOption.TCP_NODELAY, true);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected 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 StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
pipeline.addLast("handler", new HelloClient());
}
});
System.out.println("################## Test start ####################");
ChannelFuture f = b.connect("127.0.0.1", 5656).sync();
System.out.println(f.isSuccess());
Channel channel = f.channel();
System.out.println("channel is active :" + channel.isActive() + ",channel:" + channel);
int len = 1024 * 64;
StringBuilder s = new StringBuilder(len);
for (int i = 0; i < len; i++) {
s.append(len % 10);
}
final String msg = s.toString();
Util.exec(() -> {
int i = 0;
for (; ; ) {
// String s = "hello Service! ---> :" + i;
ChannelFuture f1 = channel.writeAndFlush(msg);
Util.sleep(1);
System.out.println(f1.isDone() + "--------" + i);
i++;
}
});
Util.sleep(Integer.MAX_VALUE);
f.channel().closeFuture().sync();
} catch (Exception e) {
e.printStackTrace();
} finally {
group.shutdownGracefully();
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder in project openremote by openremote.
the class UDPStringServer method addDecoders.
@Override
protected void addDecoders(DatagramChannel channel) {
// Add delimiter and string decoders to do the work
addDecoder(channel, new DelimiterBasedFrameDecoder(maxFrameLength, stripDelimiter, Unpooled.wrappedBuffer(delimiter.getBytes())));
addDecoder(channel, new StringDecoder());
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.string.StringDecoder in project openremote by openremote.
the class TCPStringServer method addDecoders.
@Override
protected void addDecoders(SocketChannel channel) {
// Add delimiter and string decoders to do the work
addDecoder(channel, new DelimiterBasedFrameDecoder(maxFrameLength, stripDelimiter, Unpooled.wrappedBuffer(delimiter.getBytes())));
addDecoder(channel, new StringDecoder());
}
Aggregations