Repository navigation
Expand file tree
/
Copy pathmessage_handler.hpp
More file actions
146 lines (114 loc) · 3.52 KB
/
Copy pathmessage_handler.hpp
File metadata and controls
146 lines (114 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
This source file is part of KBEngine
For the latest info, see http://www.kbengine.org/
Copyright (c) 2008-2012 KBEngine.
KBEngine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KBEngine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __MESSAGE_HANDLER_HPP__
#define __MESSAGE_HANDLER_HPP__
#include "cstdkbe/memorystream.hpp"
#include "cstdkbe/smartpointer.hpp"
#include "helper/debug_helper.hpp"
#include "network/common.hpp"
namespace KBEngine{
namespace Mercury
{
class Channel;
/** 一个消息的参数抽象类 */
class MessageArgs
{
public:
enum MESSAGE_ARGS_TYPE
{
MESSAGE_ARGS_TYPE_VARIABLE = -1, // 可变参数长度
MESSAGE_ARGS_TYPE_FIXED = 0 // 固定参数长度
};
MessageArgs():strArgsTypes(){};
virtual ~MessageArgs(){};
virtual void createFromStream(MemoryStream& s) = 0;
virtual void addToStream(MemoryStream& s) = 0;
virtual int32 dataSize(void) = 0;
virtual MessageArgs::MESSAGE_ARGS_TYPE type(void){ return MESSAGE_ARGS_TYPE_FIXED; }
std::vector<std::string> strArgsTypes;
};
struct ExposedMessageInfo
{
std::string name;
Mercury::MessageID id;
int16 msgLen; // 对外消息不会超过1500
int8 argsType;
std::vector<uint8> argsTypes;
};
class MessageHandler
{
public:
MessageHandler();
virtual ~MessageHandler();
std::string name;
MessageID msgID;
MessageArgs* pArgs;
int32 msgLen; // 如果长度为-1则为非固定长度消息
bool exposed;
// stats
volatile mutable uint32 send_size;
volatile mutable uint32 send_count;
volatile mutable uint32 recv_size;
volatile mutable uint32 recv_count;
uint32 sendsize()const { return send_size; }
uint32 sendcount()const { return send_count; }
uint32 sendavgsize()const { return (send_count <= 0) ? 0 : send_size / send_count; }
uint32 recvsize()const { return recv_size; }
uint32 recvcount()const { return recv_count; }
uint32 recvavgsize()const { return (recv_count <= 0) ? 0 : recv_size / recv_count; }
/**
默认返回类别为组件消息
*/
virtual MERCURY_MESSAGE_TYPE type()const
{
return MERCURY_MESSAGE_TYPE_COMPONENT;
}
virtual int32 msglenMax(){ return MERCURY_MESSAGE_MAX_SIZE / 2; }
const char* c_str();
/**
当这个handler被正是安装到MessageHandlers后被调用
*/
virtual void onInstall(){}
virtual void handle(Channel* pChannel, MemoryStream& s)
{
pArgs->createFromStream(s);
// 将参数传给最终的接口
};
};
class MessageHandlers
{
public:
static Mercury::MessageHandlers* pMainMessageHandlers;
typedef std::map<MessageID, MessageHandler*> MessageHandlerMap;
MessageHandlers();
~MessageHandlers();
MessageHandler* add(std::string ihName, MessageArgs* args, int32 msgLen,
MessageHandler* msgHandler);
bool pushExposedMessage(std::string msgname);
MessageHandler* find(MessageID msgID);
MessageID lastMsgID() {return msgID_ - 1;}
bool initializeWatcher();
static void finalise(void);
static std::vector<MessageHandlers*>& messageHandlers();
const MessageHandlerMap& msgHandlers(){ return msgHandlers_; }
private:
MessageHandlerMap msgHandlers_;
MessageID msgID_;
std::vector< std::string > exposedMessages_;
};
}
}
#endif