RpcSession.cc 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <boost/bind.hpp>
  2. #include <boost/make_shared.hpp>
  3. #include "Rpc/RpcSession.h"
  4. #include "Rpc/RpcServer.h"
  5. namespace Egametang {
  6. RpcSession::RpcSession(boost::asio::io_service& ioService, RpcServer& server):
  7. RpcCommunicator(ioService), rpcServer(server)
  8. {
  9. }
  10. RpcSession::~RpcSession()
  11. {
  12. }
  13. void RpcSession::OnRecvMessage(RpcMetaPtr meta, StringPtr message)
  14. {
  15. RpcSessionPtr session = shared_from_this();
  16. rpcServer.RunService(session, meta, message,
  17. boost::bind(&RpcSession::SendMeta, session, _1, _2));
  18. // 可以循环利用
  19. RecvMeta(meta, message);
  20. }
  21. void RpcSession::OnSendMessage(RpcMetaPtr meta, StringPtr message)
  22. {
  23. }
  24. void RpcSession::Start()
  25. {
  26. auto meta = boost::make_shared<RpcMeta>();
  27. auto message = boost::make_shared<std::string>();
  28. RecvMeta(meta, message);
  29. }
  30. void RpcSession::Stop()
  31. {
  32. if (isStopped)
  33. {
  34. return;
  35. }
  36. isStopped = true;
  37. // 延迟删除,必须等所有的bind执行完成后才能remove,
  38. // 否则会出现this指针失效的问题
  39. ioService.post(boost::bind(&RpcServer::Remove, &rpcServer, shared_from_this()));
  40. }
  41. } // namespace Egametang