RpcServer.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include <boost/asio.hpp>
  2. #include <boost/bind.hpp>
  3. #include <boost/make_shared.hpp>
  4. #include <google/protobuf/service.h>
  5. #include <google/protobuf/descriptor.h>
  6. #include "Base/Marcos.h"
  7. #include "Rpc/Typedef.h"
  8. #include "Rpc/RpcServer.h"
  9. #include "Rpc/RpcSession.h"
  10. #include "Rpc/ResponseHandler.h"
  11. #include "Rpc/MethodInfo.h"
  12. namespace Egametang {
  13. RpcServer::RpcServer(boost::asio::io_service& service, int port):
  14. ioService(service), acceptor(ioService),
  15. threadPool(1), sessions(),
  16. methods()
  17. {
  18. boost::asio::ip::address address;
  19. address.from_string("127.0.0.1");
  20. boost::asio::ip::tcp::endpoint endpoint(address, port);
  21. acceptor.open(endpoint.protocol());
  22. acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
  23. acceptor.bind(endpoint);
  24. acceptor.listen();
  25. auto newSession = boost::make_shared<RpcSession>(ioService, *this);
  26. acceptor.async_accept(newSession->Socket(),
  27. boost::bind(&RpcServer::OnAsyncAccept, this,
  28. newSession, boost::asio::placeholders::error));
  29. }
  30. RpcServer::~RpcServer()
  31. {
  32. threadPool.wait();
  33. acceptor.close();
  34. }
  35. void RpcServer::OnAsyncAccept(RpcSessionPtr session, const boost::system::error_code& err)
  36. {
  37. if (err)
  38. {
  39. return;
  40. }
  41. session->Start();
  42. sessions.insert(session);
  43. auto newSession = boost::make_shared<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::RunService(RpcSessionPtr session, RpcMetaPtr meta,
  55. StringPtr message, MessageHandler messageHandler)
  56. {
  57. MethodInfoPtr methodInfo = methods[meta->method];
  58. auto responseHandler =
  59. boost::make_shared<ResponseHandler>(methodInfo, meta->id, messageHandler);
  60. responseHandler->Request()->ParseFromString(*message);
  61. google::protobuf::Closure* done = google::protobuf::NewCallback(
  62. this, &RpcServer::OnCallMethod,
  63. session, responseHandler);
  64. threadPool.schedule(
  65. boost::bind(&google::protobuf::Service::CallMethod, methodInfo->service,
  66. responseHandler->Method(), (google::protobuf::RpcController*)(NULL),
  67. responseHandler->Request(), responseHandler->Response(),
  68. done));
  69. }
  70. void RpcServer::Register(ProtobufServicePtr service)
  71. {
  72. boost::hash<std::string> stringHash;
  73. const google::protobuf::ServiceDescriptor* serviceDescriptor = service->GetDescriptor();
  74. for (int i = 0; i < serviceDescriptor->method_count(); ++i)
  75. {
  76. const google::protobuf::MethodDescriptor* methodDescriptor =
  77. serviceDescriptor->method(i);
  78. std::size_t methodHash = stringHash(methodDescriptor->full_name());
  79. auto methodInfo = boost::make_shared<MethodInfo>(service, methodDescriptor);
  80. methods[methodHash] = methodInfo;
  81. }
  82. }
  83. void RpcServer::Remove(RpcSessionPtr session)
  84. {
  85. sessions.erase(session);
  86. }
  87. } // namespace Egametang