RpcClient.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #ifndef RPC_RPCCLIENT_H
  2. #define RPC_RPCCLIENT_H
  3. #include <unordered_map>
  4. #include <boost/enable_shared_from_this.hpp>
  5. #include <boost/asio.hpp>
  6. #include <boost/thread.hpp>
  7. #include <google/protobuf/service.h>
  8. #include "Base/Typedef.h"
  9. #include "Rpc/Typedef.h"
  10. #include "Rpc/RpcCommunicator.h"
  11. namespace Egametang {
  12. class RpcHandler;
  13. class RpcClient:
  14. public google::protobuf::RpcChannel, public RpcCommunicator,
  15. public boost::enable_shared_from_this<RpcClient>
  16. {
  17. private:
  18. typedef std::unordered_map<std::size_t, RequestHandlerPtr> RequestHandlerMap;
  19. std::size_t id;
  20. RequestHandlerMap requestHandlers;
  21. void OnAsyncConnect(const boost::system::error_code& err);
  22. virtual void OnRecvMessage(RpcMetaPtr meta, StringPtr message);
  23. virtual void OnSendMessage(RpcMetaPtr meta, StringPtr message);
  24. public:
  25. RpcClient(boost::asio::io_service& service, std::string host, int port);
  26. ~RpcClient();
  27. virtual void CallMethod(
  28. const google::protobuf::MethodDescriptor* method,
  29. google::protobuf::RpcController* controller,
  30. const google::protobuf::Message* request,
  31. google::protobuf::Message* response,
  32. google::protobuf::Closure* done);
  33. };
  34. } // namespace Egametang
  35. #endif // RPC_RPCCLIENT_H