Search code examples
c++boostnetwork-programmingboost-asio

boost asio async_read_until: how to raise error from a custom MatchCondition?


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?


Solution

  • 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 your match condition for all cases, including errors. Just check errors in you handler