RpcServer.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #include <boost/asio.hpp>
  2. #include <boost/foreach.hpp>
  3. #include <boost/bind.hpp>
  4. #include <boost/make_shared.hpp>
  5. #include <google/protobuf/service.h>
  6. #include <google/protobuf/descriptor.h>
  7. #include "Base/Marcos.h"
  8. #include "Rpc/Typedef.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(1), 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 = boost::make_shared<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. return;
  39. }
  40. session->Start();
  41. sessions.insert(session);
  42. RpcSessionPtr newSession = boost::make_shared<RpcSession>(ioService, *this);
  43. acceptor.async_accept(newSession->Socket(),
  44. boost::bind(&RpcServer::OnAsyncAccept, this,
  45. newSession, boost::asio::placeholders::error));
  46. }
  47. void RpcServer::OnCallMethod(RpcSessionPtr session, ResponseHandlerPtr responseHandler)
  48. {
  49. // 调度到网络线
  50. session->Socket().get_io_service().post(
  51. boost::bind(&ResponseHandler::Run, responseHandler));
  52. }
  53. void RpcServer::Stop()
  54. {
  55. threadPool.wait();
  56. acceptor.close();
  57. sessions.clear();
  58. }
  59. void RpcServer::RunService(RpcSessionPtr session, RpcMetaPtr meta,
  60. StringPtr message, MessageHandler messageHandler)
  61. {
  62. MethodInfoPtr methodInfo = methods[meta->method];
  63. ResponseHandlerPtr responseHandler =
  64. boost::make_shared<ResponseHandler>(methodInfo, meta->id, messageHandler);
  65. responseHandler->Request()->ParseFromString(*message);
  66. google::protobuf::Closure* done = google::protobuf::NewCallback(
  67. this, &RpcServer::OnCallMethod,
  68. session, responseHandler);
  69. threadPool.schedule(
  70. boost::bind(&google::protobuf::Service::CallMethod, methodInfo->service,
  71. responseHandler->Method(), (google::protobuf::RpcController*)(NULL),
  72. responseHandler->Request(), responseHandler->Response(),
  73. done));
  74. }
  75. void RpcServer::Register(ProtobufServicePtr service)
  76. {
  77. boost::hash<std::string> stringHash;
  78. const google::protobuf::ServiceDescriptor* serviceDescriptor = service->GetDescriptor();
  79. for (int i = 0; i < serviceDescriptor->method_count(); ++i)
  80. {
  81. const google::protobuf::MethodDescriptor* methodDescriptor =
  82. serviceDescriptor->method(i);
  83. std::size_t methodHash = stringHash(methodDescriptor->full_name());
  84. MethodInfoPtr methodInfo = boost::make_shared<MethodInfo>(service, methodDescriptor);
  85. methods[methodHash] = methodInfo;
  86. }
  87. }
  88. void RpcServer::Remove(RpcSessionPtr& session)
  89. {
  90. sessions.erase(session);
  91. }
  92. } // namespace Egametang