PageRenderTime 26ms CodeModel.GetById 19ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/TGame/TServerMS/Database/DBMessageHandler.cpp

http://awoe.googlecode.com/
C++ | 83 lines | 60 code | 15 blank | 8 comment | 4 complexity | 47a2e8bf48e79de83d9bfcf96c28dca3 MD5 | raw file
 1/////////////////////////////////////////////////////////////////////
 2// DBMessageHandler.cpp
 3//
 4//
 5// 11/16/2010
 6//
 7/////////////////////////////////////////////////////////////////////
 8
 9#include "stdafx.h"
10#include "DBMessageHandler.h"
11#include "Message/DBMessage.h"
12#include "DBMessageHandlerMapper.h"
13
14using namespace woe;
15
16extern INT64	sqlResult = 0;
17DBMessageHandler::MessageMap DBMessageHandler::ms_MessageMap;
18Service* DBMessageHandler::m_wpSvcLgc = NULL;
19DBMysql DBMessageHandler::m_DBMysql;
20
21DBMessageHandler::DBMessageHandler(  )
22{
23	
24}
25
26DBMessageHandler::~DBMessageHandler(  )
27{
28}
29
30bool DBMessageHandler::initialize(IMsgHandlerInitData* data)
31{
32	MsgHandlerInitDataDB* d = dynamic_cast<MsgHandlerInitDataDB*>(data);
33	if (d)
34	{
35		m_wpSvcLgc = d->wpSvcLgc;
36
37		DBMessageHandlerMapper mapper(this);
38
39		if (m_DBMysql.Create( d->db_server, d->db_user, d->db_password, d->db_name, d->db_port))
40		{
41			LOG_INFO("Success to connect to database!");
42			return true;
43		}
44		else
45		{
46			LOG_INFO("Failed to connect to database!");
47			return false;
48		}
49		return true;
50	}
51	else
52		return false;
53}
54
55
56void DBMessageHandler::addSubHandler( int nMsgCate, int nMsgID, IMsgHandler* pHandler )
57{
58	ms_MessageMap.insert( MessageMap::value_type( int((nMsgCate<<16) | nMsgID), pHandler ) );
59}
60
61void DBMessageHandler::rmvSubHandler( int nMsgCate, int nMsgID)
62{;
63}
64
65
66bool DBMessageHandler::handleMsg( IMsg * message )
67{
68	int nKey = (int)message->getMsgCate();
69	nKey <<= 16;
70	nKey |= message->getMsgID();
71
72	MessageMapConstIter iter = ms_MessageMap.find( nKey );
73	if ( iter != ms_MessageMap.end() )
74	{
75		iter->second->handleMsg( message );
76	}
77	else
78	{
79		// log()
80		return false;
81	}
82	return true;
83}