/TGame/TCommon/Service/Session.cpp

http://awoe.googlecode.com/ · C++ · 118 lines · 97 code · 19 blank · 2 comment · 5 complexity · 2594de30fdfa3b67ff67c2429c7668c1 MD5 · raw file

  1. #include "stdafx.h"
  2. #include "Session.h"
  3. #include <iostream>
  4. #include "boost/bind.hpp"
  5. Session::Session( boost::asio::io_service &svcIO)
  6. :m_theSvcIO(svcIO),
  7. m_theSocket(svcIO),
  8. m_wpMsgBufferFactory(NULL),
  9. m_nSesId(0),
  10. m_nEttId(0)
  11. {
  12. }
  13. int
  14. Session::getID()const
  15. {
  16. return m_nSesId;
  17. }
  18. void
  19. Session::setID(int nID)
  20. {
  21. m_nSesId = nID;
  22. }
  23. int
  24. Session::getSID()const
  25. {
  26. return m_nEttId;
  27. }
  28. void
  29. Session::setSID(int nID)
  30. {
  31. m_nEttId = nID;
  32. }
  33. boost::asio::ip::tcp::socket&
  34. Session::getSocket()
  35. {
  36. return m_theSocket;
  37. }
  38. bool
  39. Session::read(MsgBuffer* pBuffer)
  40. {
  41. if (pBuffer==NULL)
  42. {
  43. pBuffer = new MsgBuffer;
  44. }
  45. if (pBuffer)
  46. {
  47. m_theSocket.async_read_some(pBuffer->prepare(),
  48. boost::bind(&Session::onInput, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, pBuffer));
  49. }
  50. return true;
  51. }
  52. bool
  53. Session::write(MsgBuffer* pBuffer)
  54. {
  55. if (pBuffer)
  56. {
  57. pBuffer->update_buffer_length4();
  58. m_theSocket.async_write_some(pBuffer->data(),
  59. boost::bind(&Session::onOutput, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, pBuffer));
  60. }
  61. return true;
  62. }
  63. bool
  64. Session::write(IMsg* pMsg)
  65. {
  66. return false;
  67. }
  68. void
  69. Session::setDataStreamPool(MsgBufferFactory* pMsgBuffFact)
  70. {
  71. m_wpMsgBufferFactory = pMsgBuffFact;
  72. }
  73. bool
  74. Session::onUpdate(const int& nElapse)
  75. {
  76. return true;
  77. }
  78. void
  79. Session::onInput(const boost::system::error_code& error, size_t bytes_transferred, MsgBuffer* pBuffer )
  80. {
  81. if (!error)
  82. {
  83. read();
  84. }
  85. else
  86. {
  87. std::cout<<"Error!"<<std::endl;
  88. }
  89. }
  90. void
  91. Session::onOutput(const boost::system::error_code& error, size_t bytes_transferred, MsgBuffer* pBuffer )
  92. {
  93. if (pBuffer)
  94. {
  95. // recycle
  96. //m_wpMsgBufferFactory->free(buffer);
  97. delete pBuffer;
  98. }
  99. }