/
epBaseServer.h
282 lines (231 loc) · 7.15 KB
/
epBaseServer.h
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*!
@file epBaseServer.h
@author Woong Gyu La a.k.a Chris. <juhgiyo@gmail.com>
<http://github.com/juhgiyo/epserverengine>
@date February 13, 2012
@brief Base Server Interface
@version 1.0
@section LICENSE
The MIT License (MIT)
Copyright (c) 2012-2013 Woong Gyu La <juhgiyo@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
@section DESCRIPTION
An Interface for Base Server.
*/
#ifndef __EP_BASE_SERVER_H__
#define __EP_BASE_SERVER_H__
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif //WIN32_LEAN_AND_MEAN
#include "epServerEngine.h"
#include "epBaseServerWorker.h"
#include "epServerConf.h"
#include "epBaseServerObject.h"
#include "epServerObjectList.h"
#include "epParserList.h"
using namespace std;
namespace epse{
/*!
@class BaseServer epBaseServer.h
@brief A class for Base Server.
*/
class EP_SERVER_ENGINE BaseServer:public BaseServerObject{
public:
/*!
Default Constructor
Initializes the Server
@param[in] port the port string
@param[in] syncPolicy Synchronous Policy
@param[in] maximumConnectionCount the maximum number of connection
@param[in] waitTimeMilliSec wait time for Server Thread to terminate
@param[in] lockPolicyType The lock policy
*/
BaseServer(const TCHAR * port=_T(DEFAULT_PORT),SyncPolicy syncPolicy=SYNC_POLICY_ASYNCHRONOUS, unsigned int maximumConnectionCount=CONNECTION_LIMIT_INFINITE,unsigned int waitTimeMilliSec=WAITTIME_INIFINITE, epl::LockPolicy lockPolicyType=epl::EP_LOCK_POLICY);
/*!
Default Copy Constructor
Initializes the BaseServer
@param[in] b the second object
*/
BaseServer(const BaseServer& b);
/*!
Default Destructor
Destroy the Server
*/
virtual ~BaseServer();
/*!
Assignment operator overloading
@param[in] b the second object
@return the new copied object
*/
BaseServer & operator=(const BaseServer&b);
/*!
Get Worker List
@return the list of the worker
*/
vector<BaseServerObject*> GetWorkerList() const;
/*!
Set the port for the server.
@remark Cannot be changed while connected to server
@param[in] port The port to set.
*/
void SetPort(const TCHAR * port);
/*!
Get the port number of server
@return the port number in string
*/
epl::EpTString GetPort() const;
/*!
Set the Maximum Connection Count for the server.
@param[in] maxConnectionCount The Maximum Connection Count to set.
@remark 0 means there is no limit
*/
void SetMaximumConnectionCount(unsigned int maxConnectionCount);
/*!
Get the Maximum Connection Count of server
@return The Maximum Connection Count
@remark 0 means there is no limit
*/
unsigned int GetMaximumConnectionCount() const;
/*!
Set Synchronous Policy
@param[in] syncPolicy Synchronous Policy to set
@return true if successfully set otherwise false
@remark SyncPolicy cannot be set when Server is running.
*/
bool SetSyncPolicy(SyncPolicy syncPolicy);
/*!
Get current Synchronous Policy
@return Synchronous Policy
*/
SyncPolicy GetSyncPolicy() const;
/*!
Start the server
@param[in] port the port string
@remark if argument is NULL then previously setting value is used
*/
bool StartServer(const TCHAR * port=NULL);
/*!
Stop the server
*/
void StopServer();
/*!
Check if the server is started
@return true if the server is started otherwise false
*/
bool IsServerStarted() const;
/*!
Terminate all clients' socket connected.
*/
void ShutdownAllClient();
/*!
Broadcast the packet
@param[in] packet
@param[in] waitTimeInMilliSec wait time for sending the packet in millisecond for each connection
*/
void Broadcast(const Packet& packet, unsigned int waitTimeInMilliSec=WAITTIME_INIFINITE);
/*!
Do the action given by input function for all workers
@param[in] DoFunc the action for each worker
@param[in] argCount the number of arguments
*/
void CommandWorkers(void (__cdecl *DoFunc)(BaseServerObject*,unsigned int,va_list),unsigned int argCount,...);
/*!
Find with given key by comparing worker with given function
@param[in] key the key to find
@param[in] EqualFunc the Compare Function
@return the found BaseServerObject
*/
template <typename T>
BaseServerObject *FindWorker(T const & key, bool (__cdecl *EqualFunc)(T const &, const BaseServerObject *))
{
return m_workerList.Find(key,EqualFunc);
}
protected:
/*!
Return the new server worker.
@param[in] sockAddrInfo the socket address info
@remark Sub-class should implement this to create new worker.
@remark Server will automatically release this worker.
@remark returning NULL will close the client connection.
@return the new server worker
*/
virtual BaseServerWorker* createNewWorker(sockaddr sockAddrInfo)=0;
private:
/*!
Actually set the port for the server.
@remark Cannot be changed while connected to server
@param[in] port The port to set.
*/
void setPort(const TCHAR *port);
/*!
Listening Loop Function
*/
virtual void execute() ;
/*!
Kill connection from the client
@param[in] clientObj client object
@param[in] argCount the argument count
@param[in] args the argument list
*/
static void killConnection(BaseServerObject *clientObj,unsigned int argCount,va_list args);
/*!
Send packet to given client
@param[in] clientObj client object
@param[in] argCount the argument count
@param[in] args the argument list
*/
static void sendPacket(BaseServerObject *clientObj,unsigned int argCount,va_list args);
/*!
Clean up the server initialization.
*/
void cleanUpServer();
/*!
Actually shut down all the clients (worker).
*/
void shutdownAllClient();
/*!
Actually Stop the server
*/
void stopServer();
/*!
Reset Server
*/
void resetServer();
private:
/// port number
epl::EpString m_port;
/// listening socket
SOCKET m_listenSocket;
/// internal use variable
struct addrinfo *m_result;
// Socket info
sockaddr m_socketInfo;
// Socket info size
int m_sockAddrSize;
/// general lock
epl::BaseLock *m_baseServerLock;
/// Lock Policy
epl::LockPolicy m_lockPolicy;
/// worker list
ServerObjectList m_workerList;
/// Server Parser List
ParserList *m_parserList;
/// Maximum Connection Count
unsigned int m_maxConnectionCount;
};
}
#endif //__EP_BASE_SERVER_H__