RpcServer.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. ioService(service), acceptor(ioService),
  16. threadPool(), 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(ioService, *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 newSession(new RpcSession(ioService, *this));
  44. acceptor.async_accept(newSession->Socket(),
  45. boost::bind(&RpcServer::OnAsyncAccept, this,
  46. newSession, boost::asio::placeholders::error));
  47. }
  48. void RpcServer::OnCallMethod(RpcSessionPtr session, ResponseHandlerPtr responseHandler)
  49. {
  50. // 调度到网络线
  51. session->Socket().get_io_service().post(
  52. boost::bind(&ResponseHandler::Run, responseHandler));
  53. }
  54. void RpcServer::Stop()
  55. {
  56. threadPool.Wait();
  57. acceptor.close();
  58. sessions.clear();
  59. }
  60. void RpcServer::RunService(RpcSessionPtr session, RpcMetaPtr meta,
  61. StringPtr message, MessageHandler messageHandler)
  62. {
  63. MethodInfoPtr methodInfo = methods[meta->method];
  64. ResponseHandlerPtr responseHandler(
  65. new ResponseHandler(methodInfo, meta->id, messageHandler));
  66. responseHandler->Request()->ParseFromString(*message);
  67. google::protobuf::Closure* done = google::protobuf::NewCallback(
  68. this, &RpcServer::OnCallMethod,
  69. session, responseHandler);
  70. threadPool.Schedule(
  71. boost::bind(&google::protobuf::Service::CallMethod, methodInfo->service,
  72. responseHandler->Method(), (google::protobuf::RpcController*)(NULL),
  73. responseHandler->Request(), responseHandler->Response(),
  74. done));
  75. }
  76. void RpcServer::Register(RpcServicePtr service)
  77. {
  78. boost::hash<std::string> stringHash;
  79. const google::protobuf::ServiceDescriptor* serviceDescriptor = service->GetDescriptor();
  80. for (int i = 0; i < serviceDescriptor->method_count(); ++i)
  81. {
  82. const google::protobuf::MethodDescriptor* methodDescriptor =
  83. serviceDescriptor->method(i);
  84. std::size_t methodHash = stringHash(methodDescriptor->full_name());
  85. MethodInfoPtr methodInfo(new MethodInfo(service, methodDescriptor));
  86. CHECK(methods.find(methodHash) == methods.end());
  87. methods[methodHash] = methodInfo;
  88. }
  89. }
  90. void RpcServer::Remove(RpcSessionPtr& session)
  91. {
  92. sessions.erase(session);
  93. }
  94. } // namespace Egametang