RpcSession.cc 1.1 KB

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