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

/TGame/TServerGate/Network/DataSession.cpp

http://awoe.googlecode.com/
C++ | 96 lines | 79 code | 16 blank | 1 comment | 5 complexity | 73736711fa7eb5b785cb5d448bcd380a MD5 | raw file
 1#include "stdafx.h"
 2#include "DataSession.h"
 3#include <iostream>
 4#include "boost/bind.hpp"
 5
 6DataSession::DataSession( boost::asio::io_service &svcIO)
 7:m_theSvcIO(svcIO), m_theSocket(svcIO), m_wpMsgBufferFactory(NULL)
 8{
 9}
10
11int
12DataSession::getID()const
13{
14	return m_nSesId;
15}
16
17void
18DataSession::setID(int nID)
19{
20	m_nSesId = nID;
21}
22
23boost::asio::ip::tcp::socket&
24DataSession::getSocket()
25{
26	return m_theSocket;
27}
28
29bool
30DataSession::read(MsgBuffer* pStream)
31{
32	if (pStream==NULL)
33	{
34		pStream = m_wpMsgBufferFactory->malloc();
35	}
36
37	if (pStream)
38	{
39		boost::asio::async_read(m_theSocket,
40			pStream->prepare(1024),
41			boost::bind(&DataSession::onInput, this, boost::asio::placeholders::error, pStream));
42	}
43
44	return true;
45}
46
47
48bool
49DataSession::write(MsgBuffer* pStream)
50{
51	if (pStream)
52	{
53		boost::asio::async_write(m_theSocket,
54			pStream->data(),
55			boost::bind(&DataSession::onOutput, this, boost::asio::placeholders::error, pStream));
56	}
57
58	return true;
59}
60
61void
62DataSession::setDataStreamPool(MsgBufferFactory* pMsgBuffFact)
63{
64	m_wpMsgBufferFactory = pMsgBuffFact;
65}
66
67bool
68DataSession::onUpdate(const int& nElapse)
69{
70	return true;
71}
72
73void 
74DataSession::onInput(const boost::system::error_code& error, MsgBuffer* pStream )
75{
76	if (!error)
77	{
78		
79
80		read();
81	}
82	else
83	{
84		std::cout<<"Error!"<<std::endl;
85	}
86}
87
88void
89DataSession::onOutput(const boost::system::error_code& error, MsgBuffer* pStream )
90{
91	if (pStream)
92	{
93		//	recycle
94		m_wpMsgBufferFactory->free(pStream);
95	}
96}