-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathNettyServer.java
More file actions
69 lines (53 loc) · 2.03 KB
/
NettyServer.java
File metadata and controls
69 lines (53 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package top.guoziyang.mydb.backend.server;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
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 top.guoziyang.mydb.backend.tbm.TableManager;
import top.guoziyang.mydb.transport.DecoderHandler;
import top.guoziyang.mydb.transport.EncoderHandler;
/**
* netty服务端
* 作者:RioAngele
* 时间:2023.5.23
*/
public class NettyServer {
int PORT ;
TableManager tbm;
public NettyServer(int PORT, TableManager tbm){
this.PORT=PORT;
this.tbm=tbm;
}
public void start() {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 128)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
p.addLast(new EncoderHandler())
.addLast(new DecoderHandler())
.addLast(new NettyServerHandler(tbm));
}
});
ChannelFuture f = b.bind(PORT).sync();
f.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("fail to start!!!");
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}