我设计了以下协议。
Header的长度 |
标头 |
内容 |
定界符(END标志) |
0x00000101 |
Hello |
JSON |
### End ### |
客户端发送一条消息,其中包含“标头的长度”,“标头”,“内容”,“内容”和“分界符”。应该注意的是,客户不会一次发送完整的消息,“竞争”字段将被多次发送。当客户端终点发送内容字段时,它将连接“ ### End ###”。服务器的作业是获取“标题”字段和“内容”字段。
业务逻辑是处理“标题”和“内容”。因此,我需要从Bytebuf获得这些。我想到的第一件事是使用解码器扩展要获得“标头”。当我获得第一个解码框架(即“标头”)时,我将通过将解码器替换为另一个来更改点线。最后,我将使用新处理程序来处理剩下的消息。
我试图根据
I designed the following protocol.
Header’s Length |
Header |
Content |
Delimiter(end flag) |
0x00000101 |
HELLO |
json |
###end### |
The Client sends a message which contains “Header’s Length”,”Header”,”content”and “Delimiter”. It should be noted that the client will not send a complete message at once, the “Contend”field will be sent multiple times.when the client finish sending the Content field it will attach “###end###” at the end.The Server’s job is to get the “Header”field and the “Content” field.
The Business logic is to handle the “Header” and “Content”. So I need to get these from the bytebuf. The first thing that comes to my mind is using a decoder extends LengthFieldBasedFrameDecoder to get “Header”.When I get the first decoded frame ,namely the “Header”, i will change the pineline by replacing the decoder to another.In the end,I'll use the new handler to deal with leftover message.
I tried to overwrite LengthFieldBasedFrameDecoder's decode() method according to this answer. But it seems to be an infeasible method. When the decoder is removed from the pipeline,client is still sending messages.so what happen to these message? Will these messages be dropped?
发布评论
评论(1)
您的协议格式比在解码器中的简单构建要复杂得多。这意味着您必须编写自己的解码器。
查看您的格式,我们可以识别以下状态:
在使用时实现这样的状态开关不太困难
replayingdecoder
。例如:
编写此类编码后,请确保对其进行测试,包括有部分数据,或者当同一消息中有多个帧时,例如:
Your protocol format is a bit more complicated than just a simple build in decoder. This means you have to write your own decoder.
Looking a your format, we can identity the following states:
Implementing a state switch like this is not too difficulty when using a
ReplayingDecoder
.For example:
After writing such an encoding make sure to test it, including with the cases there is partial data, or when there are multiple frames in the same message, for example: