Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd2ffdd7

Browse files
committed
2 parentsa1ed1ea +d856feb commitd2ffdd7

File tree

8 files changed

+234
-0
lines changed

8 files changed

+234
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
packagecom.waylau.netty.demo.pojo;
2+
3+
importio.netty.buffer.ByteBuf;
4+
importio.netty.channel.ChannelHandlerContext;
5+
importio.netty.handler.codec.MessageToByteEncoder;
6+
7+
/**
8+
* @author wangjun
9+
* @date 2020-08-08
10+
*/
11+
publicclassMessageToByteTimeEncoderextendsMessageToByteEncoder<UnixTime> {
12+
13+
@Override
14+
protectedvoidencode(ChannelHandlerContextctx,UnixTimemsg,ByteBufout) {
15+
out.writeInt((int)msg.value());
16+
}
17+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
packagecom.waylau.netty.demo.pojo;
2+
3+
importio.netty.bootstrap.Bootstrap;
4+
importio.netty.channel.ChannelFuture;
5+
importio.netty.channel.ChannelInitializer;
6+
importio.netty.channel.ChannelOption;
7+
importio.netty.channel.EventLoopGroup;
8+
importio.netty.channel.nio.NioEventLoopGroup;
9+
importio.netty.channel.socket.SocketChannel;
10+
importio.netty.channel.socket.nio.NioSocketChannel;
11+
12+
publicclassTimeClient {
13+
14+
publicstaticvoidmain(String[]args)throwsException {
15+
16+
Stringhost ="127.0.0.1";// args[0];
17+
intport =8080;//Integer.parseInt(args[1]);
18+
EventLoopGroupworkerGroup =newNioEventLoopGroup();
19+
20+
try {
21+
Bootstrapb =newBootstrap();// (1)
22+
b.group(workerGroup);// (2)
23+
b.channel(NioSocketChannel.class);// (3)
24+
b.option(ChannelOption.SO_KEEPALIVE,true);// (4)
25+
b.handler(newChannelInitializer<SocketChannel>() {
26+
@Override
27+
publicvoidinitChannel(SocketChannelch)throwsException {
28+
ch.pipeline().addLast(newTimeDecoder());
29+
ch.pipeline().addLast(newTimeClientHandler());
30+
}
31+
});
32+
33+
// 启动客户端
34+
ChannelFuturef =b.connect(host,port).sync();// (5)
35+
36+
// 等待连接关闭
37+
f.channel().closeFuture().sync();
38+
}finally {
39+
workerGroup.shutdownGracefully();
40+
}
41+
}
42+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
packagecom.waylau.netty.demo.pojo;
2+
3+
importio.netty.channel.ChannelHandlerContext;
4+
importio.netty.channel.ChannelInboundHandlerAdapter;
5+
6+
/**
7+
* @author wj89757
8+
* @date 2020-08-08
9+
*/
10+
publicclassTimeClientHandlerextendsChannelInboundHandlerAdapter{
11+
12+
@Override
13+
publicvoidchannelRead(ChannelHandlerContextctx,Objectmsg) {
14+
UnixTimem = (UnixTime)msg;
15+
System.out.println(m);
16+
ctx.close();
17+
}
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
packagecom.waylau.netty.demo.pojo;
2+
3+
importio.netty.buffer.ByteBuf;
4+
importio.netty.channel.ChannelHandlerContext;
5+
importio.netty.handler.codec.ByteToMessageDecoder;
6+
7+
importjava.util.List;
8+
9+
/**
10+
* TimeDecoder 处理数据拆分的问题
11+
* @author wj89757
12+
* @date 2020-08-08
13+
*/
14+
publicclassTimeDecoderextendsByteToMessageDecoder {
15+
16+
@Override
17+
protectedvoiddecode(ChannelHandlerContextchannelHandlerContext,ByteBufbyteBuf,List<Object>list)throwsException {
18+
if (byteBuf.readableBytes() <4) {
19+
return;
20+
}
21+
list.add(newUnixTime(byteBuf.readUnsignedInt()));
22+
}
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
packagecom.waylau.netty.demo.pojo;
2+
3+
importio.netty.buffer.ByteBuf;
4+
importio.netty.channel.ChannelHandlerContext;
5+
importio.netty.channel.ChannelOutboundHandlerAdapter;
6+
importio.netty.channel.ChannelPromise;
7+
8+
/**
9+
* @author wj89757
10+
* @date 2020-08-08
11+
*/
12+
publicclassTimeEncoderextendsChannelOutboundHandlerAdapter {
13+
/**
14+
* 第一,通过 ChannelPromise,当编码后的数据被写到了通道上 Netty 可以通过这个对象标记是成功还是失败。
15+
* 第二, 我们不需要调用 cxt.flush()。因为处理器已经单独分离出了一个方法 void flush(ChannelHandlerContext cxt),
16+
* 如果像自己实现 flush() 方法内容可以自行覆盖这个方法。
17+
*/
18+
@Override
19+
publicvoidwrite(ChannelHandlerContextctx,Objectmsg,ChannelPromisepromise) {
20+
UnixTimem = (UnixTime)msg;
21+
ByteBufencoded =ctx.alloc().buffer(4);
22+
encoded.writeInt((int)m.value());
23+
ctx.write(encoded,promise);// (1)
24+
}
25+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
packagecom.waylau.netty.demo.pojo;
2+
3+
importio.netty.bootstrap.ServerBootstrap;
4+
importio.netty.channel.ChannelFuture;
5+
importio.netty.channel.ChannelInitializer;
6+
importio.netty.channel.ChannelOption;
7+
importio.netty.channel.EventLoopGroup;
8+
importio.netty.channel.nio.NioEventLoopGroup;
9+
importio.netty.channel.socket.SocketChannel;
10+
importio.netty.channel.socket.nio.NioServerSocketChannel;
11+
12+
/**
13+
* 时间服务器
14+
*/
15+
publicclassTimeServer {
16+
17+
privateintport;
18+
19+
publicTimeServer(intport) {
20+
this.port =port;
21+
}
22+
23+
publicvoidrun()throwsException {
24+
EventLoopGroupbossGroup =newNioEventLoopGroup();// (1)
25+
EventLoopGroupworkerGroup =newNioEventLoopGroup();
26+
try {
27+
ServerBootstrapb =newServerBootstrap();// (2)
28+
b.group(bossGroup,workerGroup)
29+
.channel(NioServerSocketChannel.class)// (3)
30+
.childHandler(newChannelInitializer<SocketChannel>() {// (4)
31+
@Override
32+
publicvoidinitChannel(SocketChannelch)throwsException {
33+
ch.pipeline().addLast(newTimeEncoder());
34+
ch.pipeline().addLast(newTimeServerHandler());
35+
}
36+
})
37+
.option(ChannelOption.SO_BACKLOG,128)// (5)
38+
.childOption(ChannelOption.SO_KEEPALIVE,true);// (6)
39+
40+
// 绑定端口,开始接收进来的连接
41+
ChannelFuturef =b.bind(port).sync();// (7)
42+
43+
// 等待服务器 socket 关闭 。
44+
// 在这个例子中,这不会发生,但你可以优雅地关闭你的服务器。
45+
f.channel().closeFuture().sync();
46+
}finally {
47+
workerGroup.shutdownGracefully();
48+
bossGroup.shutdownGracefully();
49+
}
50+
}
51+
52+
publicstaticvoidmain(String[]args)throwsException {
53+
intport;
54+
if (args.length >0) {
55+
port =Integer.parseInt(args[0]);
56+
}else {
57+
port =8080;
58+
}
59+
newTimeServer(port).run();
60+
}
61+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
packagecom.waylau.netty.demo.pojo;
2+
3+
importio.netty.buffer.ByteBuf;
4+
importio.netty.channel.ChannelFuture;
5+
importio.netty.channel.ChannelFutureListener;
6+
importio.netty.channel.ChannelHandlerContext;
7+
importio.netty.channel.ChannelInboundHandlerAdapter;
8+
9+
/**
10+
* @author wj89757
11+
* @date 2020-08-08
12+
*/
13+
publicclassTimeServerHandlerextendsChannelInboundHandlerAdapter {
14+
15+
@Override
16+
publicvoidchannelActive(ChannelHandlerContextctx)throwsException {
17+
ChannelFuturef =ctx.writeAndFlush(newUnixTime());
18+
f.addListener(ChannelFutureListener.CLOSE);
19+
}
20+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
packagecom.waylau.netty.demo.pojo;
2+
3+
importjava.util.Date;
4+
5+
/**
6+
* @author wj89757
7+
* @date 2020-08-08
8+
*/
9+
publicclassUnixTime {
10+
privatefinallongvalue;
11+
12+
publicUnixTime() {
13+
this(System.currentTimeMillis() /1000L +2208988800L);
14+
}
15+
16+
publicUnixTime(longvalue) {
17+
this.value =value;
18+
}
19+
20+
publiclongvalue() {
21+
returnvalue;
22+
}
23+
24+
@Override
25+
publicStringtoString() {
26+
returnnewDate((value() -2208988800L) *1000L).toString();
27+
}
28+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp