Skip to content

Commit

Permalink
ByteToMessageDecoder解码器
Browse files Browse the repository at this point in the history
  • Loading branch information
1263919792@qq.com committed Jul 23, 2019
1 parent 33b15e3 commit 3eb9e3a
Show file tree
Hide file tree
Showing 9 changed files with 354 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/main/java/netty_demo/decode_demo/EchoClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package netty_demo.decode_demo;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

public class EchoClient {

private String host;
private int port;

public EchoClient(String host, int port) {
this.host = host;
this.port = port;
}

private void start() throws Exception {
EventLoopGroup group = new NioEventLoopGroup();

try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(host, port))
.handler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new EchoClientHandler());
}
});

ChannelFuture channelFuture = b.connect().sync();
channelFuture.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}

public static void main(String[] args) throws Exception {

String host = "127.0.0.1";
int port = 8080;

if (args != null && args.length > 0) {
host = args[0];
port = Integer.valueOf(args[1]);
}

new EchoClient(host, port).start();
}


}
33 changes: 33 additions & 0 deletions src/main/java/netty_demo/decode_demo/EchoClientHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package netty_demo.decode_demo;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
int i;

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
ByteBuf buf = Unpooled.buffer();
for (int i = 0; i < 9; i++) {
buf.writeInt(i);
}
ctx.writeAndFlush(buf);
}

protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
System.out.println("channelRead0 receive : " + ++i);
while (msg.isReadable()) {
System.out.println("The Client receive : " + msg.readByte());
}
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
57 changes: 57 additions & 0 deletions src/main/java/netty_demo/decode_demo/EchoServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package netty_demo.decode_demo;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.net.InetSocketAddress;

public class EchoServer {

private int port;

public EchoServer(int port) {
this.port = port;
}

public static void main(String[] args) throws Exception {
int port = 8080;
if (args != null && args.length > 0) {
port = Integer.valueOf(args[0]);
}

new EchoServer(port).start();
}

private void start() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
NioEventLoopGroup workerGroup = new NioEventLoopGroup();

try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(port))
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ToIntegerDecoder());
ch.pipeline().addLast(new EchoServerHandler());
}
});

ChannelFuture channelFuture = serverBootstrap.bind().sync();
System.out.println("The Echo Server start in : " + port);

channelFuture.channel().closeFuture().sync();

} finally {
bossGroup.shutdownGracefully().sync();
workerGroup.shutdownGracefully().sync();
}
}
}
14 changes: 14 additions & 0 deletions src/main/java/netty_demo/decode_demo/EchoServerHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package netty_demo.decode_demo;

import io.netty.buffer.Unpooled;
import io.netty.channel.*;

public class EchoServerHandler extends SimpleChannelInboundHandler<Integer> {
int i;

@Override
protected void channelRead0(ChannelHandlerContext ctx, Integer msg) throws Exception {
System.out.println("server receive mag : " + msg + ", channelRead0方法被调用 : " + ++i + "次");
ctx.writeAndFlush(Unpooled.copiedBuffer(new byte[]{(byte) ((int) i)}));
}
}
17 changes: 17 additions & 0 deletions src/main/java/netty_demo/decode_demo/ToIntegerDecoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package netty_demo.decode_demo;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;

import java.util.List;

public class ToIntegerDecoder extends ByteToMessageDecoder {
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
if (in.readableBytes() >= 4) {
out.add(in.readInt());
// Thread.sleep(1000);
}
}
}
58 changes: 58 additions & 0 deletions src/main/java/netty_demo/testnetty3/EchoClient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package netty_demo.testnetty3;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

public class EchoClient {

private String host;
private int port;

public EchoClient(String host, int port) {
this.host = host;
this.port = port;
}

private void start() throws Exception {
EventLoopGroup group = new NioEventLoopGroup();

try {
Bootstrap b = new Bootstrap();
b.group(group)
.channel(NioSocketChannel.class)
.remoteAddress(new InetSocketAddress(host, port))
.handler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new EchoClientHandler());
}
});

ChannelFuture channelFuture = b.connect().sync();
channelFuture.channel().closeFuture().sync();
} finally {
group.shutdownGracefully();
}
}

public static void main(String[] args) throws Exception {

String host = "127.0.0.1";
int port = 8080;

if (args != null && args.length > 0) {
host = args[0];
port = Integer.valueOf(args[1]);
}

new EchoClient(host, port).start();
}


}
30 changes: 30 additions & 0 deletions src/main/java/netty_demo/testnetty3/EchoClientHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package netty_demo.testnetty3;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

import java.nio.ByteBuffer;

public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {

@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
for (int i = 0; i < 20; i++) {
ctx.writeAndFlush(Unpooled.copiedBuffer(new byte[]{(byte)i}));
Thread.sleep(1000);
}
}

protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
System.out.println("The Client receive : " + msg.toString(CharsetUtil.UTF_8));
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
54 changes: 54 additions & 0 deletions src/main/java/netty_demo/testnetty3/EchoServer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package netty_demo.testnetty3;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.net.InetSocketAddress;

public class EchoServer {

private int port;

public EchoServer(int port) {
this.port = port;
}

public static void main(String[] args) throws Exception {
int port = 8080;
if (args != null && args.length > 0) {
port = Integer.valueOf(args[0]);
}

new EchoServer(port).start();
}

private void start() throws Exception {
EventLoopGroup group = new NioEventLoopGroup();

try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(group)
.channel(NioServerSocketChannel.class)
.localAddress(new InetSocketAddress(port))
.childHandler(new ChannelInitializer<SocketChannel>() {

protected void initChannel(SocketChannel socketChannel) throws Exception {
socketChannel.pipeline().addLast(new EchoServerHandler());
}
});

ChannelFuture channelFuture = serverBootstrap.bind().sync();
System.out.println("The Echo Server start in : " + port);

channelFuture.channel().closeFuture().sync();

} finally {
group.shutdownGracefully().sync();
}
}
}
33 changes: 33 additions & 0 deletions src/main/java/netty_demo/testnetty3/EchoServerHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package netty_demo.testnetty3;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.util.CharsetUtil;

public class EchoServerHandler extends ChannelInboundHandlerAdapter {

int i;

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

if (msg instanceof ByteBuf) {
ByteBuf in = (ByteBuf) msg;
System.out.println("channelRead0方法被调用 : " + ++i);
if (in.readableBytes() >= 10) {
System.out.println("server receive msg :");
while (in.isReadable()) {
System.out.println(in.readByte());
}
}
}
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
throws Exception {
cause.printStackTrace();
ctx.close();
}
}

0 comments on commit 3eb9e3a

Please sign in to comment.