forked from pyb1993/JavaRedis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessageEncoder.java
More file actions
65 lines (54 loc) · 2.11 KB
/
MessageEncoder.java
File metadata and controls
65 lines (54 loc) · 2.11 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
package RedisCommand;
import CommandDispatcher.CommandDispatcher;
import MessageOutput.MessageOutput;
import RedisDataBase.AbstractPooledObject;
import RedisDataBase.RedisString;
import com.alibaba.fastjson.JSON;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageEncoder;
import io.netty.util.CharsetUtil;
import java.nio.ByteBuffer;
import java.util.List;
/*
* 这个类并不涉及状态,所以其实可以共享
* 用来将输出的结果进行编码
* */
public class MessageEncoder extends MessageToMessageEncoder<MessageOutput> {
@Override
protected void encode(ChannelHandlerContext ctx, MessageOutput msg, List<Object> out) throws Exception {
ByteBuf buf = PooledByteBufAllocator.DEFAULT.directBuffer();
writeStr(buf, msg.getRequestId());
writeStr(buf, msg.getType());
Object payload = msg.getPayload();
if(CommandDispatcher.newProtocal(msg.getType())){
// 使用自定义协议直接传递,让客户端自己去解析
if(payload instanceof AbstractPooledObject){
writeStr(buf, (RedisString) payload);
}else{
writeStr(buf, (String) payload);
}
}else{
// 使用json作为对象传递
writeStr(buf, JSON.toJSONString(payload));
}
out.add(buf);
// 在这里可以开始进行释放 requestId可以立刻释放的,type是static,不能释放
msg.getRequestId().release();
// payload能否释放要根据命令来,所以需要在各个handler里面进行释放,这里不能统一释放
}
private void writeStr(ByteBuf buf, String s) {
int len = s.getBytes(CharsetUtil.UTF_8).length;
buf.writeInt(len);
buf.writeBytes(s.getBytes(CharsetUtil.UTF_8));
}
// 注意这里不能全部写入
private void writeStr(ByteBuf buf, RedisString s) {
int len = s.size();
buf.writeInt(len);
if(len > 0) {
buf.writeBytes(s.bytes, 0, s.size());
}
}
}