Skip to content

Commit 9a6d252

Browse files
author
zhrgithub
committed
加入解码与编码业务逻辑
1 parent db458e4 commit 9a6d252

File tree

6 files changed

+83
-87
lines changed

6 files changed

+83
-87
lines changed

JdkLearn/src/main/java/com/learnjava/io/netty/timeServer/TimeClient.java

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,31 @@
1818
* @date 2022/3/7 21:07
1919
*/
2020
public class TimeClient {
21-
public static void main(String[] args) throws Exception {
22-
String host = "127.0.0.1";
23-
int port = 8080;
24-
EventLoopGroup workerGroup = new NioEventLoopGroup();
21+
public static void main(String[] args) throws Exception {
22+
String host = "127.0.0.1";
23+
int port = 8080;
24+
EventLoopGroup workerGroup = new NioEventLoopGroup();
2525

26-
try {
27-
Bootstrap b = new Bootstrap(); // (1)Bootstrap与ServerBootstrap非服务器通道类似,但它是客户端的管道引导。
28-
b.group(workerGroup); // (2)只设置一个EventLoopGroup,它将同时用作boss组和worker组。即使,BOSS、worker不用于客户端。
29-
b.channel(NioSocketChannel.class); // (3)用于创建客户端管道
30-
b.option(ChannelOption.SO_KEEPALIVE, true); // (4)这里没有用childOption(),因为客户端没有parent
31-
b.handler(new ChannelInitializer<SocketChannel>() {
32-
@Override
33-
public void initChannel(SocketChannel ch) throws Exception {
34-
ch.pipeline().addLast(new TimeDecoderThree(),new TimeClientHandler());
35-
}
36-
});
26+
try {
27+
Bootstrap b = new Bootstrap(); // (1)Bootstrap与ServerBootstrap非服务器通道类似,但它是客户端的管道引导。
28+
b.group(workerGroup); // (2)只设置一个EventLoopGroup,它将同时用作boss组和worker组。即使,BOSS、worker不用于客户端。
29+
b.channel(NioSocketChannel.class); // (3)用于创建客户端管道
30+
b.option(ChannelOption.SO_KEEPALIVE, true); // (4)这里没有用childOption(),因为客户端没有parent
31+
b.handler(
32+
new ChannelInitializer<SocketChannel>() {
33+
@Override
34+
public void initChannel(SocketChannel ch) throws Exception {
35+
ch.pipeline().addLast(new TimeDecoderThree(), new TimeClientHandler());
36+
}
37+
});
3738

38-
// Start the client.
39-
ChannelFuture f = b.connect(host, port).sync(); // (5)通过connect方法绑定终端地址并连接
39+
// Start the client.
40+
ChannelFuture f = b.connect(host, port).sync(); // (5)通过connect方法绑定终端地址并连接
4041

41-
// Wait until the connection is closed.
42-
f.channel().closeFuture().sync();
43-
} finally {
44-
workerGroup.shutdownGracefully();
45-
}
42+
// Wait until the connection is closed.
43+
f.channel().closeFuture().sync();
44+
} finally {
45+
workerGroup.shutdownGracefully();
4646
}
47-
47+
}
4848
}

JdkLearn/src/main/java/com/learnjava/io/netty/timeServer/channel/TimeClientHandler.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,24 @@ public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
3131

3232
@Override
3333
public void channelRead(ChannelHandlerContext ctx, Object msg) {
34-
// ByteBuf m = (ByteBuf) msg; // (1)在 TCP/IP 中,Netty 将从对等端发送的数据读取到ByteBuf.
35-
//
36-
// buf.writeBytes(m); // (2)
37-
// m.release();
38-
//
39-
// if (buf.readableBytes() >= 4) { // (3)
40-
// long currentTimeMillis = (buf.readUnsignedInt() - 2208988800L) * 1000L;
41-
// System.out.println(new Date(currentTimeMillis));
42-
// ctx.close();
43-
// }
44-
45-
// try {
46-
// long currentTimeMillis = (m.readUnsignedInt() - 2208988800L) * 1000L;
47-
// System.out.println(new Date(currentTimeMillis));
48-
// ctx.close();
49-
// } finally {
50-
// m.release();
51-
// }
34+
// ByteBuf m = (ByteBuf) msg; // (1)在 TCP/IP 中,Netty 将从对等端发送的数据读取到ByteBuf.
35+
//
36+
// buf.writeBytes(m); // (2)
37+
// m.release();
38+
//
39+
// if (buf.readableBytes() >= 4) { // (3)
40+
// long currentTimeMillis = (buf.readUnsignedInt() - 2208988800L) * 1000L;
41+
// System.out.println(new Date(currentTimeMillis));
42+
// ctx.close();
43+
// }
44+
45+
// try {
46+
// long currentTimeMillis = (m.readUnsignedInt() - 2208988800L) * 1000L;
47+
// System.out.println(new Date(currentTimeMillis));
48+
// ctx.close();
49+
// } finally {
50+
// m.release();
51+
// }
5252

5353
UnixTime m = (UnixTime) msg;
5454
System.out.println(m);

JdkLearn/src/main/java/com/learnjava/io/netty/timeServer/channel/TimeServerHandler.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,25 @@ public class TimeServerHandler extends ChannelInboundHandlerAdapter {
1717

1818
@Override
1919
public void channelActive(final ChannelHandlerContext ctx) { // (1)建立连接并准备好生成流量,写一个表示当前时间的 32 位整数。
20-
// final ByteBuf time = ctx.alloc().buffer(4); // (2)开辟4个字节的缓存
21-
// time.writeInt((int) (System.currentTimeMillis() / 1000L + 2208988800L));
22-
//
23-
// final ChannelFuture f =
24-
// ctx.writeAndFlush(time); // (3)写入管道并刷新,netty不需要ByteBuffer.flip(),因为netty对不同的操作类型有不同的指针
25-
// f.addListener(
26-
// new ChannelFutureListener() {
27-
//
28-
// @Override
29-
// public void operationComplete(ChannelFuture future) {
30-
//
31-
// assert f == future;
32-
// System.out.println("操作成功");
33-
// ctx.close();
34-
// }
35-
// }); // (4)添加发送信息的监听事件,如果f与future相等表示发送成功
20+
// final ByteBuf time = ctx.alloc().buffer(4); // (2)开辟4个字节的缓存
21+
// time.writeInt((int) (System.currentTimeMillis() / 1000L + 2208988800L));
22+
//
23+
// final ChannelFuture f =
24+
// ctx.writeAndFlush(time); // (3)写入管道并刷新,netty不需要ByteBuffer.flip(),因为netty对不同的操作类型有不同的指针
25+
// f.addListener(
26+
// new ChannelFutureListener() {
27+
//
28+
// @Override
29+
// public void operationComplete(ChannelFuture future) {
30+
//
31+
// assert f == future;
32+
// System.out.println("操作成功");
33+
// ctx.close();
34+
// }
35+
// }); // (4)添加发送信息的监听事件,如果f与future相等表示发送成功
3636

37-
38-
ChannelFuture f = ctx.writeAndFlush(new UnixTime());
39-
f.addListener(ChannelFutureListener.CLOSE);
37+
ChannelFuture f = ctx.writeAndFlush(new UnixTime());
38+
f.addListener(ChannelFutureListener.CLOSE);
4039
}
4140

4241
@Override

JdkLearn/src/main/java/com/learnjava/io/netty/timeServer/pojo/UnixTime.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,22 @@
77
* @date 2022/3/8 12:29
88
*/
99
public class UnixTime {
10-
private final long value;
10+
private final long value;
1111

12-
public UnixTime() {
13-
this(System.currentTimeMillis() / 1000L + 2208988800L);
14-
}
12+
public UnixTime() {
13+
this(System.currentTimeMillis() / 1000L + 2208988800L);
14+
}
1515

16-
public UnixTime(long value) {
17-
this.value = value;
18-
}
16+
public UnixTime(long value) {
17+
this.value = value;
18+
}
1919

20-
public long value() {
21-
return value;
22-
}
23-
24-
@Override
25-
public String toString() {
26-
return new Date((value() - 2208988800L) * 1000L).toString();
27-
}
20+
public long value() {
21+
return value;
22+
}
2823

24+
@Override
25+
public String toString() {
26+
return new Date((value() - 2208988800L) * 1000L).toString();
27+
}
2928
}

JdkLearn/src/main/java/com/learnjava/io/netty/timeServer/util/TimeDecoderThree.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,12 @@
1313
*/
1414
public class TimeDecoderThree extends ReplayingDecoder {
1515

16-
@Override
17-
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
18-
if (in.readableBytes() < 4) {
19-
return;
20-
}
21-
22-
out.add(new UnixTime(in.readUnsignedInt()));
16+
@Override
17+
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
18+
if (in.readableBytes() < 4) {
19+
return;
2320
}
2421

22+
out.add(new UnixTime(in.readUnsignedInt()));
23+
}
2524
}

JdkLearn/src/main/java/com/learnjava/io/netty/timeServer/util/TimeEncoder.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
*/
1212
public class TimeEncoder extends MessageToByteEncoder<UnixTime> {
1313

14-
@Override
15-
protected void encode(ChannelHandlerContext ctx, UnixTime msg, ByteBuf out) throws Exception {
16-
out.writeInt((int)msg.value());
17-
}
18-
14+
@Override
15+
protected void encode(ChannelHandlerContext ctx, UnixTime msg, ByteBuf out) throws Exception {
16+
out.writeInt((int) msg.value());
17+
}
1918
}

0 commit comments

Comments
 (0)