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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
/*
* RequestManager.h
*
* Copyright (C) 2008 Matthias Schiffer <matthias@gamezock.de>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef MAD_COMMON_REQUESTMANAGER_H_
#define MAD_COMMON_REQUESTMANAGER_H_
#include "Request.h"
#include <map>
#include <memory>
#include <string>
#include <boost/thread/locks.hpp>
#include <boost/thread/shared_mutex.hpp>
namespace Mad {
namespace Net {
class Packet;
}
namespace Common {
class RequestManager : boost::noncopyable {
private:
class RequestMap : private std::map<boost::uint16_t, boost::shared_ptr<RequestHandler> >, boost::noncopyable {
public:
bool addRequest(boost::uint16_t id, boost::shared_ptr<RequestHandler> info);
boost::shared_ptr<RequestHandler> findRequest(boost::uint16_t id);
bool deleteRequest(boost::uint16_t id);
};
class RequestHandlerFactory {
protected:
RequestHandlerFactory() {}
public:
virtual boost::shared_ptr<RequestHandler> createRequestHandler(Connection *connection, boost::uint16_t requestId) = 0;
virtual ~RequestHandlerFactory() {}
};
template<class T> class SpecificRequestHandlerFactory : public RequestHandlerFactory {
public:
virtual boost::shared_ptr<RequestHandler> createRequestHandler(Connection *connection, boost::uint16_t requestId) {
return boost::shared_ptr<RequestHandler>(new T(connection, requestId));
}
};
static RequestManager requestManager;
boost::shared_mutex mutex;
std::map<Connection*, boost::shared_ptr<RequestMap> > requestMaps;
bool server;
boost::uint16_t lastRequestId;
std::map<std::string, boost::shared_ptr<RequestHandlerFactory> > requestHandlerFactories;
boost::uint16_t _getRequestId() {
return lastRequestId+=2;
}
void receiveHandler(Connection *connection, const XmlPacket &packet, boost::uint16_t requestId);
boost::shared_ptr<RequestMap> _getUnusedRequestId(Connection *connection, boost::uint16_t *requestId);
bool send(Request *request);
RequestManager();
public:
static RequestManager* get() {
return &requestManager;
}
bool isServer() {
boost::shared_lock<boost::shared_mutex> lock(mutex);
return server;
}
void setServer(bool newServer) {
boost::lock_guard<boost::shared_mutex> lock(mutex);
server = newServer;
if(server)
lastRequestId &= ~0x01;
else
lastRequestId |= 0x01;
}
void registerConnection(Connection *connection);
void unregisterConnection(Connection *connection) {
boost::lock_guard<boost::shared_mutex> lock(mutex);
requestMaps.erase(connection);
}
template <class T> void registerPacketType(const std::string &type) {
boost::lock_guard<boost::shared_mutex> lock(mutex);
requestHandlerFactories.insert(std::make_pair(type, boost::shared_ptr<SpecificRequestHandlerFactory<T> >(new SpecificRequestHandlerFactory<T>())));
}
void unregisterPacketType(const std::string &type) {
boost::lock_guard<boost::shared_mutex> lock(mutex);
requestHandlerFactories.erase(type);
}
template <class T>
bool sendRequest(Connection *connection, Request::slot_type slot) {
boost::unique_lock<boost::shared_mutex> lock(mutex);
boost::shared_ptr<RequestMap> requestMap;
boost::uint16_t requestId;
requestMap = _getUnusedRequestId(connection, &requestId);
if(!requestMap)
return false;
boost::shared_ptr<Request> request(new T(connection, requestId, slot));
requestMap->addRequest(requestId, request);
lock.unlock();
request->sendRequest();
return true;
}
template <class T, typename T1>
bool sendRequest(Connection *connection, Request::slot_type slot, T1 t1) {
boost::unique_lock<boost::shared_mutex> lock(mutex);
boost::shared_ptr<RequestMap> requestMap;
boost::uint16_t requestId;
requestMap = _getUnusedRequestId(connection, &requestId);
if(!requestMap)
return false;
boost::shared_ptr<Request> request(new T(connection, requestId, slot, t1));
requestMap->addRequest(requestId, request);
lock.unlock();
request->sendRequest();
return true;
}
template <class T, typename T1, typename T2>
bool sendRequest(Connection *connection, Request::slot_type slot, T1 t1, T2 t2) {
boost::unique_lock<boost::shared_mutex> lock(mutex);
boost::shared_ptr<RequestMap> requestMap;
boost::uint16_t requestId;
requestMap = _getUnusedRequestId(connection, &requestId);
if(!requestMap)
return false;
boost::shared_ptr<Request> request(new T(connection, requestId, slot, t1, t2));
requestMap->addRequest(requestId, request);
lock.unlock();
request->sendRequest();
return true;
}
template <class T, typename T1, typename T2, typename T3>
bool sendRequest(Connection *connection, Request::slot_type slot, T1 t1, T2 t2, T3 t3) {
boost::unique_lock<boost::shared_mutex> lock(mutex);
boost::shared_ptr<RequestMap> requestMap;
boost::uint16_t requestId;
requestMap = _getUnusedRequestId(connection, &requestId);
if(!requestMap)
return false;
boost::shared_ptr<T> request(new T(connection, requestId, slot, t1, t2, t3));
requestMap->addRequest(requestId, request);
lock.unlock();
request->sendRequest();
return true;
}
template <class T, typename T1, typename T2, typename T3, typename T4>
bool sendRequest(Connection *connection, Request::slot_type slot, T1 t1, T2 t2, T3 t3, T4 t4) {
boost::unique_lock<boost::shared_mutex> lock(mutex);
boost::shared_ptr<RequestMap> requestMap;
boost::uint16_t requestId;
requestMap = _getUnusedRequestId(connection, &requestId);
if(!requestMap)
return false;
boost::shared_ptr<Request> request(new T(connection, requestId, slot, t1, t2, t3, t4));
requestMap->addRequest(requestId, request);
lock.unlock();
request->sendRequest();
return true;
}
};
}
}
#endif /* MAD_COMMON_REQUESTMANAGER_H_ */
|