RpcServer.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include <boost/asio.hpp>
  2. #include <boost/foreach.hpp>
  3. #include <boost/bind.hpp>
  4. #include <google/protobuf/service.h>
  5. #include <google/protobuf/descriptor.h>
  6. #include <glog/logging.h>
  7. #include "Base/Marcos.h"
  8. #include "Rpc/RpcTypedef.h"
  9. #include "Rpc/RpcServer.h"
  10. #include "Rpc/RpcSession.h"
  11. #include "Rpc/ResponseHandler.h"
  12. #include "Rpc/MethodInfo.h"
  13. namespace Egametang {
  14. RpcServer::RpcServer(boost::asio::io_service& service, int port):
  15. io_service(service), acceptor(io_service),
  16. thread_pool(), sessions(),
  17. methods()
  18. {
  19. boost::asio::ip::address address;
  20. address.from_string("127.0.0.1");
  21. boost::asio::ip::tcp::endpoint endpoint(address, port);
  22. acceptor.open(endpoint.protocol());
  23. acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
  24. acceptor.bind(endpoint);
  25. acceptor.listen();
  26. RpcSessionPtr new_session(new RpcSession(io_service, *this));
  27. acceptor.async_accept(new_session->Socket(),
  28. boost::bind(&RpcServer::OnAsyncAccept, this,
  29. new_session, boost::asio::placeholders::error));
  30. }
  31. RpcServer::~RpcServer()
  32. {
  33. }
  34. void RpcServer::OnAsyncAccept(RpcSessionPtr session, const boost::system::error_code& err)
  35. {
  36. if (err)
  37. {
  38. LOG(ERROR) << "accept fail: " << err.message();
  39. return;
  40. }
  41. session->Start();
  42. sessions.insert(session);
  43. RpcSessionPtr new_session(new RpcSession(io_service, *this));
  44. acceptor.async_accept(new_session->Socket(),
  45. boost::bind(&RpcServer::OnAsyncAccept, this,
  46. new_session, boost::asio::placeholders::error));
  47. }
  48. void RpcServer::OnCallMethod(RpcSessionPtr session, ResponseHandlerPtr response_handler)
  49. {
  50. // 调度到网络线程
  51. session->Socket().get_io_service().post(
  52. boost::bind(&ResponseHandler::Run, response_handler));
  53. }
  54. void RpcServer::HandleStop()
  55. {
  56. acceptor.close();
  57. sessions.clear();
  58. }
  59. void RpcServer::Stop()
  60. {
  61. thread_pool.Wait();
  62. // 调度到io_service线程,防止两个线程竞争
  63. io_service.post(boost::bind(&RpcServer::HandleStop, shared_from_this()));
  64. }
  65. void RpcServer::RunService(RpcSessionPtr session, RpcMetaPtr meta,
  66. StringPtr message, MessageHandler message_handler)
  67. {
  68. MethodInfoPtr method_info = methods[meta->method];
  69. ResponseHandlerPtr response_handler(
  70. new ResponseHandler(method_info, meta->id, message_handler));
  71. response_handler->Request()->ParseFromString(*message);
  72. google::protobuf::Closure* done = google::protobuf::NewCallback(
  73. this, &RpcServer::OnCallMethod,
  74. session, response_handler);
  75. thread_pool.Schedule(
  76. boost::bind(&google::protobuf::Service::CallMethod, method_info->service,
  77. response_handler->Method(), (google::protobuf::RpcController*)(NULL),
  78. response_handler->Request(), response_handler->Response(),
  79. done));
  80. }
  81. void RpcServer::Register(RpcServicePtr service)
  82. {
  83. boost::hash<std::string> string_hash;
  84. const google::protobuf::ServiceDescriptor* service_descriptor = service->GetDescriptor();
  85. for (int i = 0; i < service_descriptor->method_count(); ++i)
  86. {
  87. const google::protobuf::MethodDescriptor* method_descriptor =
  88. service_descriptor->method(i);
  89. std::size_t method_hash = string_hash(method_descriptor->full_name());
  90. MethodInfoPtr method_info(new MethodInfo(service, method_descriptor));
  91. CHECK(methods.find(method_hash) == methods.end());
  92. methods[method_hash] = method_info;
  93. }
  94. }
  95. void RpcServer::Remove(RpcSessionPtr& session)
  96. {
  97. sessions.erase(session);
  98. }
  99. } // namespace Egametang