ASYNC - Prime context to write a message header
| 436 | private: |
| 437 | // ASYNC - Prime context to write a message header |
| 438 | void WriteHeader() |
| 439 | { |
| 440 | // If this function is called, we know the outgoing message queue must have |
| 441 | // at least one message to send. So allocate a transmission buffer to hold |
| 442 | // the message, and issue the work - asio, send these bytes |
| 443 | asio::async_write(m_socket, asio::buffer(&m_qMessagesOut.front().header, sizeof(message_header<T>)), |
| 444 | [this](std::error_code ec, std::size_t length) |
| 445 | { |
| 446 | // asio has now sent the bytes - if there was a problem |
| 447 | // an error would be available... |
| 448 | if (!ec) |
| 449 | { |
| 450 | // ... no error, so check if the message header just sent also |
| 451 | // has a message body... |
| 452 | if (m_qMessagesOut.front().body.size() > 0) |
| 453 | { |
| 454 | // ...it does, so issue the task to write the body bytes |
| 455 | WriteBody(); |
| 456 | } |
| 457 | else |
| 458 | { |
| 459 | // ...it didnt, so we are done with this message. Remove it from |
| 460 | // the outgoing message queue |
| 461 | m_qMessagesOut.pop_front(); |
| 462 | |
| 463 | // If the queue is not empty, there are more messages to send, so |
| 464 | // make this happen by issuing the task to send the next header. |
| 465 | if (!m_qMessagesOut.empty()) |
| 466 | { |
| 467 | WriteHeader(); |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | else |
| 472 | { |
| 473 | // ...asio failed to write the message, we could analyse why but |
| 474 | // for now simply assume the connection has died by closing the |
| 475 | // socket. When a future attempt to write to this client fails due |
| 476 | // to the closed socket, it will be tidied up. |
| 477 | std::cout << "[" << id << "] Write Header Fail.\n"; |
| 478 | m_socket.close(); |
| 479 | } |
| 480 | }); |
| 481 | } |
| 482 | |
| 483 | // ASYNC - Prime context to write a message body |
| 484 | void WriteBody() |