RpcSession.cc 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. RpcMetaPtr meta = boost::make_shared<RpcMeta>();
  27. StringPtr message = boost::make_shared<std::string>();
  28. RecvMeta(meta, message);
  29. }
  30. void RpcSession::Stop()
  31. {
  32. RpcSessionPtr session = shared_from_this();
  33. // 延迟删除,必须等所有的bind执行完成后才能remove,
  34. // 否则会出现this指针失效的问题
  35. ioService.post(boost::bind(&RpcServer::Remove, &rpcServer, session));
  36. }
  37. } // namespace Egametang