-
Notifications
You must be signed in to change notification settings - Fork 279
Expand file tree
/
Copy pathNettyServerHandler.java
More file actions
45 lines (40 loc) · 1.27 KB
/
NettyServerHandler.java
File metadata and controls
45 lines (40 loc) · 1.27 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
package top.guoziyang.mydb.backend.server;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import top.guoziyang.mydb.backend.tbm.TableManager;
import top.guoziyang.mydb.transport.Encoder;
import top.guoziyang.mydb.transport.Package;
import top.guoziyang.mydb.transport.Packager;
/**
* netty服务端Handler
* 作者:RioAngele
* 时间:2023.5.23
*/
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
private TableManager tbm;
public NettyServerHandler(TableManager tbm) {
this.tbm = tbm;
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
Package pck=(Package) msg;
Executor exe = new Executor(tbm);
byte[] result = null;
Exception e = null;
try {
result = exe.execute(pck.getData());
} catch (Exception e1) {
e = e1;
e.printStackTrace();
exe.close();
}
Package pkg=new Package(result,e);
ctx.writeAndFlush(pkg);
}
}