RpcServer.cc 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <functional>
  2. #include <memory>
  3. #include <boost/asio.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 = std::make_shared<RpcSession>(ioService, *this);
  26. acceptor.async_accept(newSession->Socket(),
  27. std::bind(&RpcServer::OnAsyncAccept, this,
  28. newSession, std::placeholders::_1));
  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 = std::make_shared<RpcSession>(ioService, *this);
  44. acceptor.async_accept(newSession->Socket(),
  45. std::bind(&RpcServer::OnAsyncAccept, this,
  46. newSession, std::placeholders::_1));
  47. }
  48. void RpcServer::OnCallMethod(RpcSessionPtr session, ResponseHandlerPtr responseHandler)
  49. {
  50. // 调度到网络线
  51. session->Socket().get_io_service().post(
  52. std::bind(&ResponseHandler::Run, responseHandler));
  53. }
  54. void RpcServer::RunService(
  55. RpcSessionPtr session, const RpcMetaPtr meta,
  56. const StringPtr message, MessageHandler messageHandler)
  57. {
  58. MethodInfoPtr methodInfo = methods[meta->method];
  59. auto responseHandler =
  60. std::make_shared<ResponseHandler>(meta, message, methodInfo, messageHandler);
  61. google::protobuf::Closure* done = google::protobuf::NewCallback(
  62. this, &RpcServer::OnCallMethod, session, responseHandler);
  63. threadPool.schedule(
  64. std::bind(&google::protobuf::Service::CallMethod, methodInfo->GetService(),
  65. &responseHandler->Method(), (google::protobuf::RpcController*)(nullptr),
  66. responseHandler->Request(), responseHandler->Response(),
  67. done));
  68. }
  69. void RpcServer::Register(ProtobufServicePtr service)
  70. {
  71. std::hash<std::string> stringHash;
  72. const google::protobuf::ServiceDescriptor* serviceDescriptor = service->GetDescriptor();
  73. for (int i = 0; i < serviceDescriptor->method_count(); ++i)
  74. {
  75. const google::protobuf::MethodDescriptor* methodDescriptor =
  76. serviceDescriptor->method(i);
  77. std::size_t methodHash = stringHash(methodDescriptor->full_name());
  78. auto methodInfo = std::make_shared<MethodInfo>(service, methodDescriptor);
  79. methods[methodHash] = methodInfo;
  80. }
  81. }
  82. void RpcServer::Remove(RpcSessionPtr session)
  83. {
  84. sessions.erase(session);
  85. }
  86. } // namespace Egametang