2

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?

ϹοδεMεδιϲ
  • 2,790
  • 3
  • 33
  • 54

1 Answers1

3

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

ϹοδεMεδιϲ
  • 2,790
  • 3
  • 33
  • 54
Andriy Tylychko
  • 15,967
  • 6
  • 64
  • 112
  • Thanks AndyT, but that as I understand from the linked documentation, the bool return value only suggests whether the async_read can stop or not. It doesnt really tell the handler that there was an error; By error I mean the fact that a CR+LF was not found in 1024 bytes read so far. – ϹοδεMεδιϲ Jan 31 '11 at 16:14
  • @CodeMedic I think your handler could infer an error based on `bytes_transferred` placeholder being larger than 1024 bytes. – Sam Miller Jan 31 '11 at 16:20
  • @CodeMedic, @Sam Miller: or any other convenient way, up to setting a global flag etc. boost::asio just doesn't provide you such facility – Andriy Tylychko Jan 31 '11 at 16:26
  • @CodeMedic: somebody downvoted this answer, I hope it's not you (no other critical comments here) because I tried to help and I believe I provided correct answer – Andriy Tylychko Jan 31 '11 at 16:29
  • @Andy FWIW I upvoted you since this seems like the best answer. – Sam Miller Jan 31 '11 at 16:36
  • @AndyT, I think there is no easy way to do it other than checking `bytes_transferred`. So a combination of restricting read length from `untill_crlf_maxbytes_1024_match_condition` and `bytes_transferred` check from handler is the solution. Thanks Sam & Andy! – ϹοδεMεδιϲ Jan 31 '11 at 19:34