RpcCommunicator.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <boost/bind.hpp>
  2. #include <boost/asio.hpp>
  3. #include <boost/lexical_cast.hpp>
  4. #include "Rpc/RpcCommunicator.h"
  5. namespace Egametang {
  6. RpcCommunicator::RpcCommunicator(boost::asio::io_service& service):
  7. isStopped(false), ioService(service), socket(service)
  8. {
  9. }
  10. RpcCommunicator::~RpcCommunicator()
  11. {
  12. if (isStopped)
  13. {
  14. return;
  15. }
  16. socket.close();
  17. }
  18. boost::asio::ip::tcp::socket& RpcCommunicator::Socket()
  19. {
  20. return socket;
  21. }
  22. void RpcCommunicator::Stop()
  23. {
  24. if (isStopped)
  25. {
  26. return;
  27. }
  28. isStopped = true;
  29. socket.close();
  30. }
  31. void RpcCommunicator::RecvMeta(RpcMetaPtr meta, StringPtr message)
  32. {
  33. boost::asio::async_read(socket,
  34. boost::asio::buffer(reinterpret_cast<char*>(meta.get()), sizeof(*meta)),
  35. boost::bind(&RpcCommunicator::RecvMessage, this,
  36. meta, message, boost::asio::placeholders::error));
  37. }
  38. void RpcCommunicator::RecvMessage(RpcMetaPtr meta, StringPtr message,
  39. const boost::system::error_code& err)
  40. {
  41. if (err)
  42. {
  43. Stop();
  44. return;
  45. }
  46. message->resize(meta->size, 0);
  47. boost::asio::async_read(socket,
  48. boost::asio::buffer(reinterpret_cast<char*>(&message->at(0)), meta->size),
  49. boost::bind(&RpcCommunicator::RecvDone, this,
  50. meta, message, boost::asio::placeholders::error));
  51. }
  52. void RpcCommunicator::RecvDone(RpcMetaPtr meta, StringPtr message,
  53. const boost::system::error_code& err)
  54. {
  55. if (err)
  56. {
  57. Stop();
  58. return;
  59. }
  60. OnRecvMessage(meta, message);
  61. }
  62. void RpcCommunicator::OnRecvMessage(RpcMetaPtr meta, StringPtr message)
  63. {
  64. }
  65. void RpcCommunicator::SendMeta(RpcMetaPtr meta, StringPtr message)
  66. {
  67. boost::asio::async_write(socket,
  68. boost::asio::buffer(reinterpret_cast<char*>(meta.get()), sizeof(*meta)),
  69. boost::bind(&RpcCommunicator::SendMessage, this,
  70. meta, message, boost::asio::placeholders::error));
  71. }
  72. void RpcCommunicator::SendMessage(RpcMetaPtr meta, StringPtr message,
  73. const boost::system::error_code& err)
  74. {
  75. if (err)
  76. {
  77. Stop();
  78. return;
  79. }
  80. boost::asio::async_write(socket, boost::asio::buffer(*message),
  81. boost::bind(&RpcCommunicator::SendDone, this,
  82. meta, message, boost::asio::placeholders::error));
  83. }
  84. void RpcCommunicator::SendDone(RpcMetaPtr meta, StringPtr message,
  85. const boost::system::error_code& err)
  86. {
  87. if (err)
  88. {
  89. Stop();
  90. return;
  91. }
  92. OnSendMessage(meta, message);
  93. }
  94. void RpcCommunicator::OnSendMessage(RpcMetaPtr meta, StringPtr message)
  95. {
  96. }
  97. } // namespace Egametang