RpcChannel.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. namespace Egametang {
  8. class RpcHandler;
  9. class RpcChannel: public google::protobuf::RpcChannel
  10. {
  11. private:
  12. typedef boost::unordered_map<int32, RpcHandlerPtr> RpcCallbackMap;
  13. int32 id_;
  14. RpcCallbackMap handlers_;
  15. boost::asio::io_service& io_service_;
  16. boost::asio::ip::tcp::socket socket_;
  17. void AsyncConnectHandler(const boost::system::error_code& err);
  18. // recieve response
  19. void RecvMessegeSize();
  20. void RecvMessage(IntPtr size, const boost::system::error_code& err);
  21. void RecvMessageHandler(StringPtr ss, const boost::system::error_code& err);
  22. // send request
  23. void SendMessageSize(const RpcRequestPtr request, RpcHandlerPtr handler);
  24. void SendMessage(const RpcRequestPtr request,
  25. RpcHandlerPtr handler, const boost::system::error_code& err);
  26. void SendMessageHandler(int32 id, RpcHandlerPtr handler,
  27. const boost::system::error_code& err);
  28. public:
  29. RpcChannel(boost::asio::io_service& service, std::string& host, int port);
  30. ~RpcChannel();
  31. virtual void CallMethod(
  32. const google::protobuf::MethodDescriptor* method,
  33. google::protobuf::RpcController* controller,
  34. const google::protobuf::Message* request,
  35. google::protobuf::Message* response,
  36. google::protobuf::Closure* done);
  37. };
  38. } // namespace Egametang
  39. #endif // NET_RPC_CHANNEL_H