PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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