boost asio async_read_until:如何从自定义 MatchCondition 引发错误?
我正在尝试优化具有混合模式协议的应用层协议(用于元信息、握手、确认等的线路协议和用于数据的二进制协议)。在客户端等待 ACK 的状态下,我
async_read_until(socket, buffer, untill_crlf_maxbytes_1024_match_condition, handler);
按照 async_read_until 记录的 此处。
这里的想法是读取直到 CR+LF,但只等待读取 1024 个字节,这样如果发生任何奇怪的事情并且套接字收到一些垃圾,它就不会继续读取。
我的问题是,在这种情况下从 untill_crlf_maxbytes_1024_match_condition
引发错误是个好主意吗?在这种情况下我该如何提出错误?如果不是最好的选择是什么?
I am trying to optimize an application layer protocol that has a mixed mode protocol ( line protocol for meta info, handshake, acknowledgement, etc. and binary for data ). In a state where the client is waiting for an ACK, I do
async_read_until(socket, buffer, untill_crlf_maxbytes_1024_match_condition, handler);
Where untill_crlf_maxbytes_1024_match_condition
is implemented acxcording to async_read_until documented here.
Here the idea is to read untill CR+LF but wait for only 1024 bytes to be read, so that if anything spooky goes on and if the socket gets some junk, it would not stay reading.
My question is, is it a good idea to raise an error in such a scenario from untill_crlf_maxbytes_1024_match_condition
? How do I raise an error in that scenario? If not whats the best alternative?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
引发错误,您的意思是抛出异常吗?如果是这样,这是一个坏主意,因为 boost asio 的大多数函数都有两个版本:抛出异常和返回错误代码。错误代码对于 boost asio 的异步特性非常有用。
对于所有情况(包括错误),您都可以从
匹配条件
返回true
。只需检查处理程序
中的错误By raise error, do you mean throw exception? If so, it's a bad idea because most functions of boost asio have two versions: throwing exceptions and returning error codes. Error codes are very useful for asynchronous nature of boost asio.
You can return
true
from yourmatch condition
for all cases, including errors. Just check errors in youhandler