ASYNC - Prime context to write a message body
| 482 | |
| 483 | // ASYNC - Prime context to write a message body |
| 484 | void WriteBody() |
| 485 | { |
| 486 | // If this function is called, a header has just been sent, and that header |
| 487 | // indicated a body existed for this message. Fill a transmission buffer |
| 488 | // with the body data, and send it! |
| 489 | asio::async_write(m_socket, asio::buffer(m_qMessagesOut.front().body.data(), m_qMessagesOut.front().body.size()), |
| 490 | [this](std::error_code ec, std::size_t length) |
| 491 | { |
| 492 | if (!ec) |
| 493 | { |
| 494 | // Sending was successful, so we are done with the message |
| 495 | // and remove it from the queue |
| 496 | m_qMessagesOut.pop_front(); |
| 497 | |
| 498 | // If the queue still has messages in it, then issue the task to |
| 499 | // send the next messages' header. |
| 500 | if (!m_qMessagesOut.empty()) |
| 501 | { |
| 502 | WriteHeader(); |
| 503 | } |
| 504 | } |
| 505 | else |
| 506 | { |
| 507 | // Sending failed, see WriteHeader() equivalent for description :P |
| 508 | std::cout << "[" << id << "] Write Body Fail.\n"; |
| 509 | m_socket.close(); |
| 510 | } |
| 511 | }); |
| 512 | } |
| 513 | |
| 514 | // ASYNC - Prime context ready to read a message header |
| 515 | void ReadHeader() |