RpcServer.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #ifndef RPC_RPCSERVER_H
  2. #define RPC_RPCSERVER_H
  3. #include <functional>
  4. #include <unordered_set>
  5. #include <unordered_map>
  6. #include <boost/asio.hpp>
  7. #include <boost/enable_shared_from_this.hpp>
  8. #include <boost/threadpool.hpp>
  9. #include <google/protobuf/service.h>
  10. #include "Base/Marcos.h"
  11. #include "Rpc/Typedef.h"
  12. namespace Egametang {
  13. typedef std::unordered_set<RpcSessionPtr> RpcSessionSet;
  14. typedef std::unordered_map<std::size_t, MethodInfoPtr> MethodMap;
  15. class RpcServer: public boost::noncopyable, public boost::enable_shared_from_this<RpcServer>
  16. {
  17. private:
  18. friend class RpcServerTest;
  19. boost::asio::io_service& ioService;
  20. boost::asio::ip::tcp::acceptor acceptor;
  21. boost::threadpool::fifo_pool threadPool;
  22. RpcSessionSet sessions;
  23. MethodMap methods;
  24. void OnAsyncAccept(RpcSessionPtr session, const boost::system::error_code& err);
  25. void OnCallMethod(RpcSessionPtr session, ResponseHandlerPtr responseHandler);
  26. public:
  27. RpcServer(boost::asio::io_service& service, int port);
  28. virtual ~RpcServer();
  29. virtual void RunService(
  30. RpcSessionPtr session, const RpcMetaPtr meta,
  31. const StringPtr message, MessageHandler messageHandler);
  32. virtual void Register(ProtobufServicePtr service);
  33. virtual void Remove(RpcSessionPtr session);
  34. };
  35. } // namespace Egametang
  36. #endif // RPC_RPCSERVER_H