forked from pig4210/xlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xServer.cpp
336 lines (314 loc) · 6.45 KB
/
xServer.cpp
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "xServer.h"
#ifndef FOR_RING0
using namespace std;
//注意,服务器绑定只能NULL而不能是127.0.0.1,否则不响应它机
xTCPServ::xTCPServ(const unsigned short ports)
:_Mybase(AddrInfo(INADDR_ANY,ports))
{
needclose = false;
filter_container tmpv;
for(int i = 0;i < filter_end;++i)
{
filter.push_back(tmpv);
}
s.insert(this);
}
xTCPServ::~xTCPServ()
{
closeit();
}
void xTCPServ::close()
{
needclose = true;
}
void xTCPServ::setfilter(enum_deal index,func_deal func,bool addordel)
{
try
{
if(index >= filter_end) return;
filter_container& con = filter[index];
if(addordel)
{
con.push_back(func);
}
else
{
for(filter_container::iterator it = con.begin();it != con.end();++it)
{
if(*it == func)
{
con.erase(it);
break;
}
}
}
}
catch(...)
{
xerr << __FUNCTION__ << "★★ 发生异常!";
}
}
bool xTCPServ::wait()
{
try
{
if(needclose || s.empty()) return false;
//组织fd_set
sline.clear();
sline << (u_int)s.size();
for(_Mybase* ns : s)
{
sline << ns->hSocket();
}
//监听
if(SOCKET_ERROR == select((int)s.size(),(fd_set*)sline.c_str(),nullptr,nullptr,nullptr))
{
xerr << __FUNCTION__ << "★ 数据监听失败!";
return false;
}
//处理响应,内部接受connect
for(_Mybase* ns : s)
{
if(FD_ISSET(ns->hSocket(),(fd_set*)sline.c_str()))
{
recvit(ns);
}
}
//判断连接、加入连接队列
for(_Mybase* ns : waitsock)
{
aptit(ns);
}
//枚举连接(枚举必须放在“删除连接”前)
for(_Mybase* ns : s)
{
enumit(ns);
}
//删除连接
for(_Mybase* ns : delsock)
{
delit(ns);
}
delsock.clear();
waitsock.clear();
return true;
}
catch(TCHAR* msg)
{
xerr << __FUNCTION__ << "★★ " << ws2s(msg);
}
catch(...)
{
xerr << __FUNCTION__ << "★★ 发生未知异常!";
}
return false;
}
void xTCPServ::delit(_Mybase* ns)
{
try
{
if(ns == nullptr || ns == this) return;
filter_container& vec = filter[deal_del];
for(func_deal func : vec)
{
func(ns);
}
sock_container::iterator it = s.find(ns);
if(it != s.end()) s.erase(it);
delete ns;
}
catch(...)
{
xerr << __FUNCTION__ << "★★ 处理" << IpString(ns->addrto) << "时发生异常!";
}
}
void xTCPServ::catchit(_Mybase* ns)
{
try
{
if(ns == nullptr || ns == this) return;
filter_container& vec = filter[deal_excpt];
if(vec.empty())
{
delit(ns);
return;
}
for(func_deal func : vec)
{
if(func(ns)) delit(ns);
}
}
catch(...)
{
xerr << __FUNCTION__ << "★★ 处理" << IpString(ns->addrto) << "时发生异常!";
//这里再次尝试无条件结束连接,如果失败将会生成多重异常
delit(ns);
}
}
void xTCPServ::aptit(_Mybase* ns)
{
try
{
if(ns == nullptr || ns == this) return;
filter_container& vec = filter[deal_apt];
for(func_deal func : vec)
{
if(func(ns))
{
delete ns;
return;
}
}
s.insert(ns);
}
catch(...)
{
xerr << __FUNCTION__ << "★★ 处理" << IpString(ns->addrto) << "时发生异常!";
catchit(ns);
}
}
void xTCPServ::enumit(_Mybase* ns)
{
try
{
if(ns == nullptr || ns == this) return;
if(delsock.find(ns) != delsock.end()) return;
filter_container& vec = filter[deal_enum];
for(func_deal func : vec)
{
if(func(ns)) delsock.insert(ns);
}
}
catch(...)
{
xerr << __FUNCTION__ << "★★ 处理" << IpString(ns->addrto) << "时发生异常!";
catchit(ns);
}
}
void xTCPServ::recvit(_Mybase* ns)
{
try
{
if(ns == nullptr) return;
//如果是服务器收到消息,则是连接消息
if(ns == this)
{
//接受连接,放入等待队列
//必须先apt后,addrfrom才有值,为保准确,需要先做
waitsock.insert(this->apt());
return;
}
int rets = (*ns).recv();
if(rets <= 0)
{
//连接断开,放入删除队列中
delsock.insert(ns);
return;
}
filter_container& vec = filter[deal_recv];
for(func_deal func : vec)
{
if(func(ns))
{
delsock.insert(ns);
break;
}
}
}
catch(...)
{
xerr << __FUNCTION__ << "★★ 处理" << IpString(ns->addrto) << "时发生异常!";
catchit(ns);
}
}
void xTCPServ::closeit()
{
try
{
netline nline;
filter_container& vec = filter[deal_close];
//反向删除
for(sock_container::reverse_iterator it = s.rbegin(); it != s.rend(); ++it)
{
if(*it == this) continue;
for(func_deal func : vec)
{
func(*it);
}
delete *it;
}
s.clear();
}
catch(...)
{
xerr << __FUNCTION__ << "★★ 发生异常!";
}
}
xUDPServer::xUDPServer(const unsigned short ports)
:_Mybase(AddrInfo(INADDR_ANY,ports))
{
needclose = false;
}
void xUDPServer::close()
{
needclose = true;
}
bool xUDPServer::wait()
{
try
{
if(needclose) return false;
//组织fd_set
fd_set fs;
fs.fd_count = 1;
fs.fd_array[0] = hSocket();
//监听
if(SOCKET_ERROR == select(fs.fd_count,&fs,nullptr,nullptr,nullptr))
{
xerr << __FUNCTION__ << "★ 数据监听失败!";
return false;
}
int rets = recv();
if(rets <= 0) throw runtime_error("xUDPServer 接收数据失败");
for(func_deal func : filter)
{
if(func(this)) break;
}
return true;
}
catch(TCHAR* msg)
{
xerr << __FUNCTION__ << "★★ " << ws2s(msg);
}
catch(...)
{
xerr << __FUNCTION__ << "★★ 发生未知异常!";
}
return false;
}
void xUDPServer::setfilter(func_deal func,bool addordel)
{
try
{
if(addordel)
{
filter.push_back(func);
}
else
{
for(filter_container::iterator it = filter.begin();it != filter.end();++it)
{
if(*it == func)
{
filter.erase(it);
break;
}
}
}
}
catch(...)
{
xerr << __FUNCTION__ << "★★ 发生异常!";
}
}
#endif //#ifndef FOR_RING0