RpcChannel.h 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. #ifndef NET_RPC_CHANNEL_H
  2. #define NET_RPC_CHANNEL_H
  3. #include <google/protobuf/service.h>
  4. #include <boost/unordered_map.hpp>
  5. #include <boost/asio.hpp>
  6. #include "Base/Base.h"
  7. #include "Net/RpcCommunicator.h"
  8. namespace Hainan {
  9. class RpcHandler;
  10. class RpcChannel: public google::protobuf::RpcChannel
  11. {
  12. typedef boost::unordered_map<int32, RpcHandlerPtr> RpcCallbackMap;
  13. private:
  14. int32 id;
  15. RpcCallbackMap handlers;
  16. RpcCommunicator communicator;
  17. boost::asio::io_service io_service;
  18. boost::asio::ip::tcp::socket socket;
  19. void SendRequestHandler(int32 id, RpcHandlerPtr handler,
  20. const boost::system::error_code& err);
  21. void SendRequest(const RpcRequest& request, RpcHandlerPtr handler);
  22. void RecvResponse();
  23. public:
  24. RpcChannel(std::string& host, int port);
  25. ~RpcChannel();
  26. virtual void CallMethod(
  27. const google::protobuf::MethodDescriptor* method,
  28. google::protobuf::RpcController* controller,
  29. const google::protobuf::Message* request,
  30. google::protobuf::Message* response,
  31. google::protobuf::Closure* done);
  32. };
  33. } // namespace Hainan
  34. #endif // NET_RPC_CHANNEL_H