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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
|
<!doctype html public "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<TITLE>nevrax.org : docs</TITLE>
<LINK REL=stylesheet TYPE="text/css" HREF="/inc/css/nevrax.css">
<link href="doxygen.css" rel="stylesheet" type="text/css">
</HEAD>
<BODY MARGINHEIGHT="0" MARGINWIDTH="0">
<!-- uplinks -->
<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0>
<TR>
<TD WIDTH=16><IMG SRC="/inc/img/pixel.gif" WIDTH="16" HEIGHT="16" BORDER=0 ALT=""></TD>
<TD WIDTH=140 BGCOLOR=#dddddd><IMG SRC="/inc/img/pixel.gif" WIDTH="140" HEIGHT="16" BORDER=0 ALT=""></TD>
<TD WIDTH=16><IMG SRC="/inc/img/pixel.gif" WIDTH="16" HEIGHT="16" BORDER=0 ALT=""></TD>
<TD><IMG width=6 height=14 SRC="/inc/img/reddots.gif" ALT="#" VSPACE=2 HSPACE=2 BORDER=0 ></TD><TD VALIGN=middle> <A CLASS=uplinks HREF='/'><b>Home</B></FONT></A> </TD>
<TD><IMG width=6 height=14 SRC="/inc/img/reddots.gif" ALT="#" VSPACE=2 HSPACE=2 BORDER=0 ></TD><TD VALIGN=middle> <A CLASS=uplinks HREF=><b>nevrax.com</B></FONT></A> </TD>
</TR>
</TABLE>
<!-- banner Nevrax -->
<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0 WIDTH=100%>
<TR><TD BGCOLOR="#000000" BACKGROUND="/inc/img/black_banner.jpg"><A HREF=""><IMG SRC="/inc/img/nevrax.gif" WIDTH="170" HEIGHT="45" BORDER=0 ALT="Nevrax" ></A></TD></TR>
</TABLE>
<!-- main table -->
<TABLE CELLSPACING=0 CELLPADDING=0 BORDER=0 height=100%>
<TR>
<TD WIDTH=16><IMG SRC="/inc/img/pixel.gif" WIDTH="16" HEIGHT="10" BORDER=0 ALT=""></TD>
<TD WIDTH=140 BGCOLOR=#dddddd VALIGN=TOP ALIGN=middle><IMG SRC="/inc/img/pixel.gif" WIDTH="140" HEIGHT="10" BORDER=0 ALT="">
<!------ Begin Box ------>
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 BGCOLOR=black><TR><TD><TABLE border=0 cellspacing=2 cellpadding=0 width=120><tr><TD ALIGN=middle bgcolor=black>
<FONT COLOR=white FACE="sans-serif"><B>Nevrax.org</B></FONT></TD></TR><tr><td colspan=2 bgcolor=#FFFFFF>
<TABLE cellspacing=0 cellpadding=1 border=0>
<tr><td ALIGN=middle><a class='linkbox' href="/news/" TITLE="Rubrique news"><img width=13 height=15 hspace=5 border=0 src=/inc/img/picto-news.gif ALT=#></A></td><td><a class='linkbox' href="/news/" TITLE="News">News</a></td></tr>
<tr><td ALIGN=middle><a class='linkbox' href="/mail/" TITLE="Rubrique mail"><img width=15 height=11 hspace=5 border=0 src=/inc/img/picto-mail.gif ALT=#></A></td><td><a class='linkbox' href="/mail/" TITLE="Mailing list archive">Mailing-list</a></td></tr>
<tr><td ALIGN=middle><a class='linkbox' href="/docs/" TITLE="Rubrique docs"><img width=14 height=16 hspace=5 border=0 src=/inc/img/picto-docs.gif ALT=#></A></td><td><a class='linkbox' href="/docs/" TITLE="Documentation">Documentation</a></td></tr>
<tr><td ALIGN=middle><a class='linkbox' href="/cvs/" TITLE="Rubrique cvs"><img width=13 height=17 hspace=5 border=0 src=/inc/img/picto-cvs.gif ALT=#></A></td><td><a class='linkbox' href="/cvs/" TITLE="CVS Web">CVS</a></td></tr>
<tr><td ALIGN=middle><a class='linkbox' href="/bugs/" TITLE="Rubrique bugs"><img width=20 height=16 hspace=5 border=0 src=/inc/img/picto-bugs.gif ALT=#></A></td><td><a class='linkbox' href="/bugs/" TITLE="Bugtracking">Bugs</a></td></tr>
<tr><td ALIGN=middle><a class='linkbox' href="/GPL.php3" TITLE="Rubrique license"><img width=18 height=12 hspace=5 border=0 src=/inc/img/picto-gpl.gif ALT=#></A></td><td><a class='linkbox' href="/GPL.php3" TITLE="License">License</a></td></tr>
</TABLE>
</TD></TR></TABLE></TD></TR></TABLE>
<!------ End Box ------>
</TD>
<TD WIDTH=15><IMG SRC="/inc/img/pixel.gif" WIDTH="16" HEIGHT="16" BORDER=0 ALT=""></TD>
<TD ALIGN=left valign=top><IMG SRC="/inc/img/pixel.gif" WIDTH="140" HEIGHT="10" BORDER=0 ALT="">
<!-- title -->
<TABLE background="/inc/img/redline.gif" CELLSPACING=0 CELLPADDING=0 BORDER=0 width=100%><tr><td>
<A HREF="/docs/"><img src="/inc/img/t_docs.gif" ALT="Docs" HEIGHT=20 BORDER=0></A>
</td><td><IMG SRC="/inc/img/pixel.gif" WIDTH="1" HEIGHT="1" BORDER=0 ALT="">
</td></tr></table>
<!-- block -->
<TABLE bgcolor="#dddddd" CELLSPACING=0 CELLPADDING=0 BORDER=0 width=100%><tr><td width=1% valign=middle><img width=6 height=14 hspace=2 vspace=2 src="/inc/img/reddots.gif"></TD>
<TD><B>Documentation</B></TD>
<TD ALIGN=RIGHT> </td>
</tr></table>
<!-- Generated by Doxygen 1.2.14 -->
<center>
<a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="namespaces.html">Namespace List</a> <a class="qindex" href="hierarchy.html">Class Hierarchy</a> <a class="qindex" href="classes.html">Alphabetical List</a> <a class="qindex" href="annotated.html">Compound List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="namespacemembers.html">Namespace Members</a> <a class="qindex" href="functions.html">Compound Members</a> <a class="qindex" href="globals.html">File Members</a> <a class="qindex" href="pages.html">Related Pages</a> <a class="qindexRef" doxygen="_cgi:/cgi-bin/nel-search.cgi" href="/cgi-bin/nel-search.cgi">Search</a> </center>
<hr><h1>net_manager.cpp</h1><a href="net__manager_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre>00001
00007 <font class="comment">/* Copyright, 2001 Nevrax Ltd.</font>
00008 <font class="comment"> *</font>
00009 <font class="comment"> * This file is part of NEVRAX NEL.</font>
00010 <font class="comment"> * NEVRAX NEL is free software; you can redistribute it and/or modify</font>
00011 <font class="comment"> * it under the terms of the GNU General Public License as published by</font>
00012 <font class="comment"> * the Free Software Foundation; either version 2, or (at your option)</font>
00013 <font class="comment"> * any later version.</font>
00014 <font class="comment"></font>
00015 <font class="comment"> * NEVRAX NEL is distributed in the hope that it will be useful, but</font>
00016 <font class="comment"> * WITHOUT ANY WARRANTY; without even the implied warranty of</font>
00017 <font class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU</font>
00018 <font class="comment"> * General Public License for more details.</font>
00019 <font class="comment"></font>
00020 <font class="comment"> * You should have received a copy of the GNU General Public License</font>
00021 <font class="comment"> * along with NEVRAX NEL; see the file COPYING. If not, write to the</font>
00022 <font class="comment"> * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,</font>
00023 <font class="comment"> * MA 02111-1307, USA.</font>
00024 <font class="comment"> */</font>
00025
00026
00027
00028
00029 <font class="comment">/**************************************************************************</font>
00030 <font class="comment">********************* THIS CLASS IS DEPRECATED ****************************</font>
00031 <font class="comment">**************************************************************************/</font>
00032
00033
00034
00035
00036 <font class="preprocessor">#include "<a class="code" href="stdnet_8h.html">stdnet.h</a>"</font>
00037
00038 <font class="preprocessor">#include "<a class="code" href="string__id__array_8h.html">nel/misc/string_id_array.h</a>"</font>
00039 <font class="preprocessor">#include "<a class="code" href="time__nl_8h.html">nel/misc/time_nl.h</a>"</font>
00040
00041 <font class="preprocessor">#include "<a class="code" href="naming__client_8h.html">nel/net/naming_client.h</a>"</font>
00042
00043 <font class="preprocessor">#include "<a class="code" href="callback__client_8h.html">nel/net/callback_client.h</a>"</font>
00044 <font class="preprocessor">#include "<a class="code" href="callback__server_8h.html">nel/net/callback_server.h</a>"</font>
00045
00046 <font class="preprocessor">#include "<a class="code" href="naming__client_8h.html">nel/net/naming_client.h</a>"</font>
00047
00048 <font class="preprocessor">#include "<a class="code" href="net__manager_8h.html">nel/net/net_manager.h</a>"</font>
00049
00050 <font class="keyword">using</font> <font class="keyword">namespace </font>std;
00051 <font class="keyword">using</font> <font class="keyword">namespace </font>NLMISC;
00052
00053 <font class="keyword">namespace </font>NLNET {
00054
00055
<a name="l00056"></a><a class="code" href="classNLNET_1_1CNetManager.html#r0">00056</a> CNetManager::TBaseMap CNetManager::_BaseMap;
00057
<a name="l00058"></a><a class="code" href="classNLNET_1_1CNetManager.html#r1">00058</a> CCallbackNetBase::TRecordingState CNetManager::_RecordingState;
00059
<a name="l00060"></a><a class="code" href="classNLNET_1_1CNetManager.html#r2">00060</a> <a class="code" href="namespaceNLMISC.html#a183">TTime</a> CNetManager::_NextUpdateTime = 0;
00061
00062 <font class="keyword">static</font> <font class="keywordtype">void</font> <a class="code" href="namespaceNLNET.html#a112">nmNewConnection</a> (<a class="code" href="namespaceNLNET.html#a0">TSockId</a> from, <font class="keywordtype">void</font> *arg)
00063 {
00064 <a class="code" href="debug_8h.html#a6">nlassert</a> (arg != NULL);
00065 CBaseStruct *basest = (CBaseStruct *)arg;
00066
00067 <a class="code" href="debug_8h.html#a0">nldebug</a>(<font class="stringliteral">"HNETL4: nmNewConnection() from service '%s'"</font>, basest->Name.c_str ());
00068
00069 <font class="comment">// call the client callback if necessary</font>
00070 <font class="keywordflow">if</font> (basest->ConnectionCallback != NULL)
00071 basest->ConnectionCallback (basest->Name, from, basest->ConnectionCbArg);
00072 }
00073
00074 <font class="keyword">static</font> <font class="keywordtype">void</font> <a class="code" href="namespaceNLNET.html#a113">nmNewDisconnection</a> (<a class="code" href="namespaceNLNET.html#a0">TSockId</a> from, <font class="keywordtype">void</font> *arg)
00075 {
00076 <a class="code" href="debug_8h.html#a6">nlassert</a> (arg != NULL);
00077 CBaseStruct *basest = (CBaseStruct *)arg;
00078
00079 <a class="code" href="debug_8h.html#a0">nldebug</a>(<font class="stringliteral">"HNETL4: nmNewDisconnection() from service '%s'"</font>, basest->Name.c_str ());
00080
00081 <font class="comment">// call the client callback if necessary</font>
00082 <font class="keywordflow">if</font> (basest->DisconnectionCallback != NULL)
00083 basest->DisconnectionCallback (basest->Name, from, basest->DisconnectionCbArg);
00084
00086
00087 <font class="comment">// on a client, we have to clear the associations</font>
00088 <font class="keywordflow">if</font> (basest->Type != CBaseStruct::Server)
00089 {
00090 <a class="code" href="debug_8h.html#a6">nlassert</a> (basest->NetBase.size() == 1);
00091 basest->NetBase[0]->getSIDA ().clear ();
00092 }
00093 }
00094
00095
00096 <font class="comment">// find a not connected callbackclient or create a new one and connect to the Addr</font>
00097 <font class="keywordtype">void</font> CNetManager::createConnection(CBaseStruct &Base, <font class="keyword">const</font> CInetAddress &Addr, <font class="keyword">const</font> string& name)
00098 {
00099 uint i;
00100 <font class="keywordflow">for</font> (i = 0; i < Base.NetBase.size (); i++)
00101 {
00102 <font class="keywordflow">if</font> (!Base.NetBase[i]->connected ())
00103 {
00104 <font class="keywordflow">break</font>;
00105 }
00106 }
00107 <font class="keywordflow">if</font> (i == Base.NetBase.size ())
00108 {
00109 CCallbackClient *cc = <font class="keyword">new</font> CCallbackClient( <a class="code" href="classNLNET_1_1CNetManager.html#r1">_RecordingState</a>, name+string(<font class="stringliteral">".nmr"</font>) );
00110 Base.NetBase.push_back (cc);
00111 }
00112
00113 CCallbackClient *cc = dynamic_cast<CCallbackClient *>(Base.NetBase[i]);
00114
00115 cc->CCallbackNetBase::setDisconnectionCallback (<a class="code" href="namespaceNLNET.html#a113">nmNewDisconnection</a>, (<font class="keywordtype">void</font>*) &Base);
00116
00117 <font class="keywordflow">try</font>
00118 {
00119 cc->connect (Addr);
00120
00121 <font class="keywordflow">if</font> (Base.ConnectionCallback != NULL)
00122 Base.ConnectionCallback (Base.Name, cc->getSockId(), Base.ConnectionCbArg);
00123 }
00124 <font class="keywordflow">catch</font> (ESocketConnectionFailed &e)
00125 {
00126 <a class="code" href="debug_8h.html#a1">nlinfo</a> (<font class="stringliteral">"HNETL4: can't connect now (%s)"</font>, e.what ());
00127 }
00128 }
00129
00130
00131 <font class="keywordtype">void</font> <a class="code" href="namespaceNLNET.html#a114">RegistrationBroadcast</a> (<font class="keyword">const</font> std::string &name, <a class="code" href="namespaceNLNET.html#a19">TServiceId</a> sid, <font class="keyword">const</font> vector<CInetAddress> &<a class="code" href="driver__opengl__extension__def_8h.html#a414">addr</a>)
00132 {
00133 <a class="code" href="debug_8h.html#a0">nldebug</a>(<font class="stringliteral">"HNETL4: RegistrationBroadcast() of service %s-%hu"</font>, name.c_str (), (uint16)sid);
00134
00135 <font class="comment">// find if this new service is interesting</font>
00136 <font class="keywordflow">for</font> (CNetManager::ItBaseMap itbm = CNetManager::_BaseMap.begin (); itbm != CNetManager::_BaseMap.end (); itbm++)
00137 {
00138 <font class="keywordflow">if</font> ((*itbm).second.Type == CBaseStruct::Client && !(*itbm).second.NetBase[0]->connected())
00139 {
00140 <font class="keywordflow">if</font> (name == (*itbm).first)
00141 {
00142 <font class="comment">// ok! it's cool, the service is here, go and connect to him!</font>
00143 <font class="comment">// ace warning don't work if more than one connection</font>
00144 CNetManager::createConnection ((*itbm).second, <a class="code" href="driver__opengl__extension__def_8h.html#a414">addr</a>[0], name);
00145 }
00146 }
00147 <font class="keywordflow">else</font> <font class="keywordflow">if</font> ((*itbm).second.Type == CBaseStruct::Group)
00148 {
00149 <font class="comment">// ok, it's a group, try to see if it wants this!</font>
00150 <font class="keywordflow">for</font> (uint i = 0; i < (*itbm).second.ServiceNames.size (); i++)
00151 {
00152 <font class="keywordflow">if</font> ((*itbm).second.ServiceNames[i] == name)
00153 {
00154 <font class="comment">// ace warning don't work if more than one connection</font>
00155 CNetManager::createConnection ((*itbm).second, <a class="code" href="driver__opengl__extension__def_8h.html#a414">addr</a>[0], name);
00156 <font class="keywordflow">break</font>;
00157 }
00158 }
00159 }
00160 }
00161
00162 }
00163
00164 <font class="keyword">static</font> <font class="keywordtype">void</font> <a class="code" href="namespaceNLNET.html#a115">UnregistrationBroadcast</a> (<font class="keyword">const</font> std::string &name, <a class="code" href="namespaceNLNET.html#a19">TServiceId</a> sid, <font class="keyword">const</font> vector<CInetAddress> &<a class="code" href="driver__opengl__extension__def_8h.html#a414">addr</a>)
00165 {
00166 <a class="code" href="debug_8h.html#a0">nldebug</a>(<font class="stringliteral">"HNETL4: UnregistrationBroadcast() of service %s-%hu"</font>, name.c_str (), (uint16)sid);
00167 }
00168
<a name="l00169"></a><a class="code" href="classNLNET_1_1CNetManager.html#d0">00169</a> <font class="keywordtype">void</font> CNetManager::init (<font class="keyword">const</font> CInetAddress *<a class="code" href="driver__opengl__extension__def_8h.html#a414">addr</a>, CCallbackNetBase::TRecordingState rec )
00170 {
00171 <font class="keywordflow">if</font> (<a class="code" href="driver__opengl__extension__def_8h.html#a414">addr</a> == NULL) <font class="keywordflow">return</font>;
00172
00173 <a class="code" href="classNLNET_1_1CNetManager.html#r1">_RecordingState</a> = rec;
00174
00175 <font class="comment">// connect to the naming service (may generate a ESocketConnectionFailed exception)</font>
00176
00177 vector<CInetAddress> laddr = CInetAddress::localAddresses();
00178 CNamingClient::connect( *<a class="code" href="driver__opengl__extension__def_8h.html#a414">addr</a>, <a class="code" href="classNLNET_1_1CNetManager.html#r1">_RecordingState</a>, laddr );
00179
00180 <font class="comment">// connect the callback to know when a new service comes in or goes down</font>
00181 CNamingClient::setRegistrationBroadcastCallback (<a class="code" href="classNLNET_1_1CNetManager.html#l0">RegistrationBroadcast</a>);
00182 CNamingClient::setUnregistrationBroadcastCallback (<a class="code" href="namespaceNLNET.html#a115">UnregistrationBroadcast</a>);
00183 }
00184
<a name="l00185"></a><a class="code" href="classNLNET_1_1CNetManager.html#d1">00185</a> <font class="keywordtype">void</font> CNetManager::release ()
00186 {
00187 <font class="keywordflow">if</font> (CNamingClient::connected ())
00188 CNamingClient::disconnect ();
00189
00190 <font class="keywordflow">for</font> (<a class="code" href="classNLNET_1_1CNetManager.html#u1">ItBaseMap</a> itbm = <a class="code" href="classNLNET_1_1CNetManager.html#r0">_BaseMap</a>.begin (); itbm != <a class="code" href="classNLNET_1_1CNetManager.html#r0">_BaseMap</a>.end (); itbm++)
00191 {
00192 <font class="keywordflow">for</font> (uint32 i = 0; i < (*itbm).second.NetBase.size(); i++)
00193 {
00194 (*itbm).second.NetBase[i]->disconnect ();
00195 <font class="keyword">delete</font> (*itbm).second.NetBase[i];
00196 }
00197 }
00198 <a class="code" href="classNLNET_1_1CNetManager.html#r0">_BaseMap</a>.clear ();
00199 }
00200
<a name="l00201"></a><a class="code" href="classNLNET_1_1CNetManager.html#d2">00201</a> <font class="keywordtype">void</font> CNetManager::addServer (<font class="keyword">const</font> std::string &serviceName, uint16 servicePort, <font class="keywordtype">bool</font> external)
00202 {
00203 <a class="code" href="namespaceNLNET.html#a19">TServiceId</a> sid = 0;
00204 <a class="code" href="classNLNET_1_1CNetManager.html#d2">addServer</a> (serviceName, servicePort, sid, external);
00205 }
00206
<a name="l00207"></a><a class="code" href="classNLNET_1_1CNetManager.html#d3">00207</a> <font class="keywordtype">void</font> CNetManager::addServer (<font class="keyword">const</font> std::string &serviceName, uint16 servicePort, <a class="code" href="namespaceNLNET.html#a19">TServiceId</a> &sid, <font class="keywordtype">bool</font> external)
00208 {
00209 <a class="code" href="debug_8h.html#a0">nldebug</a> (<font class="stringliteral">"HNETL4: Adding server '%s' in CNetManager"</font>, serviceName.c_str ());
00210 <a class="code" href="classNLNET_1_1CNetManager.html#u1">ItBaseMap</a> itbm = <a class="code" href="classNLNET_1_1CNetManager.html#f0">find</a> (serviceName);
00211
00212 <font class="comment">// check if it's a new server</font>
00213 <a class="code" href="debug_8h.html#a6">nlassert</a> ((*itbm).second.NetBase.empty());
00214
00215 CCallbackServer *cs = <font class="keyword">new</font> CCallbackServer( <a class="code" href="classNLNET_1_1CNetManager.html#r1">_RecordingState</a>, serviceName+string(<font class="stringliteral">".nmr"</font>) );
00216 (*itbm).second.NetBase.push_back (cs);
00217
00218 (*itbm).second.Type = CBaseStruct::Server;
00219
00220 <font class="comment">// install the server</font>
00221
00222 cs->setConnectionCallback (<a class="code" href="namespaceNLNET.html#a112">nmNewConnection</a>, (<font class="keywordtype">void</font>*) &((*itbm).second));
00223 cs->CCallbackNetBase::setDisconnectionCallback (<a class="code" href="namespaceNLNET.html#a113">nmNewDisconnection</a>, (<font class="keywordtype">void</font>*) &((*itbm).second));
00224
00225 <font class="keywordflow">if</font> (servicePort == 0)
00226 {
00227 <a class="code" href="debug_8h.html#a6">nlassert</a> (CNamingClient::connected ());
00228 servicePort = CNamingClient::queryServicePort ();
00229 }
00230
00231 cs->init (servicePort);
00232
00233 <font class="comment">// register the server to the naming service if we are connected to Naming Service</font>
00234
00235 <font class="keywordflow">if</font> (CNamingClient::connected () && !external)
00236 {
00237 <font class="comment">//CInetAddress addr = CInetAddress::localHost ();</font>
00238 <font class="comment">//addr.setPort (servicePort);</font>
00239 vector<CInetAddress> <a class="code" href="driver__opengl__extension__def_8h.html#a414">addr</a> = CInetAddress::localAddresses();
00240 <font class="keywordflow">for</font> (uint i = 0; i < <a class="code" href="driver__opengl__extension__def_8h.html#a414">addr</a>.size(); i++)
00241 <a class="code" href="driver__opengl__extension__def_8h.html#a414">addr</a>[i].setPort(servicePort);
00242
00243 <font class="keywordflow">if</font> (sid == 0)
00244 {
00245 CNamingClient::registerService (serviceName, <a class="code" href="driver__opengl__extension__def_8h.html#a414">addr</a>, sid);
00246 }
00247 <font class="keywordflow">else</font>
00248 {
00249 CNamingClient::registerServiceWithSId (serviceName, <a class="code" href="driver__opengl__extension__def_8h.html#a414">addr</a>, sid);
00250 }
00251 }
00252 <a class="code" href="debug_8h.html#a1">nlinfo</a> (<font class="stringliteral">"HNETL4: Server '%s' added, registered and listen to port %hu"</font>, serviceName.c_str (), servicePort);
00253 }
00254
00255
<a name="l00256"></a><a class="code" href="classNLNET_1_1CNetManager.html#d4">00256</a> <font class="keywordtype">void</font> CNetManager::addClient (<font class="keyword">const</font> std::string &serviceName, <font class="keyword">const</font> std::string &<a class="code" href="driver__opengl__extension__def_8h.html#a414">addr</a>, <font class="keywordtype">bool</font> autoRetry)
00257 {
00258 <a class="code" href="debug_8h.html#a0">nldebug</a> (<font class="stringliteral">"HNETL4: Adding client '%s' with addr '%s' in CNetManager"</font>, serviceName.c_str (), <a class="code" href="driver__opengl__extension__def_8h.html#a414">addr</a>.c_str());
00259 <a class="code" href="classNLNET_1_1CNetManager.html#u1">ItBaseMap</a> itbm = <a class="code" href="classNLNET_1_1CNetManager.html#f0">find</a> (serviceName);
00260
00261 <font class="comment">// it's a new client, add the connection</font>
00262 (*itbm).second.Type = CBaseStruct::ClientWithAddr;
00263 (*itbm).second.AutoRetry = autoRetry;
00264
00265 <font class="keywordflow">if</font> ((*itbm).second.ServiceNames.empty())
00266 {
00267 (*itbm).second.ServiceNames.push_back(<a class="code" href="driver__opengl__extension__def_8h.html#a414">addr</a>);
00268 }
00269 <font class="keywordflow">else</font>
00270 {
00271 (*itbm).second.ServiceNames[0] = <a class="code" href="driver__opengl__extension__def_8h.html#a414">addr</a>;
00272 }
00273
00274 <a class="code" href="debug_8h.html#a6">nlassert</a> ((*itbm).second.NetBase.size() < 2);
00275
00276 <a class="code" href="classNLNET_1_1CNetManager.html#d15">createConnection</a> ((*itbm).second, <a class="code" href="driver__opengl__extension__def_8h.html#a414">addr</a>, serviceName);
00277 }
00278
00279
<a name="l00280"></a><a class="code" href="classNLNET_1_1CNetManager.html#d5">00280</a> <font class="keywordtype">void</font> CNetManager::addClient (<font class="keyword">const</font> std::string &serviceName)
00281 {
00282 <a class="code" href="debug_8h.html#a6">nlassert</a> (CNamingClient::connected ());
00283 <a class="code" href="debug_8h.html#a0">nldebug</a> (<font class="stringliteral">"HNETL4: Adding client '%s' in CNetManager"</font>, serviceName.c_str ());
00284 <a class="code" href="classNLNET_1_1CNetManager.html#u1">ItBaseMap</a> itbm = <a class="code" href="classNLNET_1_1CNetManager.html#f0">find</a> (serviceName);
00285
00286 <font class="comment">// check if it's a new client</font>
00287 <a class="code" href="debug_8h.html#a6">nlassert</a> ((*itbm).second.NetBase.empty());
00288
00289 CCallbackClient *cc = <font class="keyword">new</font> CCallbackClient( <a class="code" href="classNLNET_1_1CNetManager.html#r1">_RecordingState</a>, serviceName+string(<font class="stringliteral">".nmr"</font>) ); <font class="comment">// ? - would not work if several clients with the same name</font>
00290 (*itbm).second.NetBase.push_back (cc);
00291
00292 (*itbm).second.Type = CBaseStruct::Client;
00293
00294 cc->CCallbackNetBase::setDisconnectionCallback (<a class="code" href="namespaceNLNET.html#a113">nmNewDisconnection</a>, (<font class="keywordtype">void</font>*) &((*itbm).second));
00295
00296 <font class="comment">// find the service in the naming_service and connect if exists</font>
00297 <font class="keywordflow">if</font> (CNamingClient::lookupAndConnect (serviceName, *cc))
00298 {
00299 <font class="comment">// call the user that we are connected</font>
00300 <font class="keywordflow">if</font> ((*itbm).second.ConnectionCallback != NULL)
00301 (*itbm).second.ConnectionCallback (serviceName, cc->getSockId(), (*itbm).second.ConnectionCbArg);
00302 }
00303 }
00304
00305
00306
<a name="l00307"></a><a class="code" href="classNLNET_1_1CNetManager.html#d6">00307</a> <font class="keywordtype">void</font> CNetManager::addGroup (<font class="keyword">const</font> std::string &groupName, <font class="keyword">const</font> std::string &serviceName)
00308 {
00309 <a class="code" href="debug_8h.html#a6">nlassert</a> (CNamingClient::connected ());
00310 <a class="code" href="debug_8h.html#a0">nldebug</a> (<font class="stringliteral">"HNETL4: Adding '%s' to group '%s' in CNetManager"</font>, serviceName.c_str (), groupName.c_str());
00311 <a class="code" href="classNLNET_1_1CNetManager.html#u1">ItBaseMap</a> itbm = <a class="code" href="classNLNET_1_1CNetManager.html#f0">find</a> (groupName);
00312
00313 (*itbm).second.Type = CBaseStruct::Group;
00314
00315 <font class="comment">// check if you don't already add this service in this group</font>
00316 vector<string>::iterator it = std::find ((*itbm).second.ServiceNames.begin(), (*itbm).second.ServiceNames.end(), serviceName);
00317 <a class="code" href="debug_8h.html#a6">nlassert</a> (it == (*itbm).second.ServiceNames.end());
00318
00319 (*itbm).second.ServiceNames.push_back(serviceName);
00320
00321
00322 <font class="comment">// find the service in the naming_service and connect if exists</font>
00323 vector<CInetAddress> addrs;
00324 CNamingClient::lookupAll (serviceName, addrs);
00325
00326 <font class="comment">// connect to all these services</font>
00327 <font class="keywordflow">for</font> (uint i = 0; i < addrs.size (); i++)
00328 {
00329 <a class="code" href="classNLNET_1_1CNetManager.html#d15">createConnection</a> ((*itbm).second, addrs[i], serviceName);
00330 }
00331 }
00332
00333
00334
<a name="l00335"></a><a class="code" href="classNLNET_1_1CNetManager.html#d12">00335</a> <a class="code" href="classNLMISC_1_1CStringIdArray.html">NLMISC::CStringIdArray</a> &CNetManager::getSIDA (<font class="keyword">const</font> std::string &serviceName)
00336 {
00337 <a class="code" href="debug_8h.html#a0">nldebug</a> (<font class="stringliteral">"HNETL4: getSIDA() for service '%s'"</font>, serviceName.c_str ());
00338 <a class="code" href="classNLNET_1_1CNetManager.html#u1">ItBaseMap</a> itbm = <a class="code" href="classNLNET_1_1CNetManager.html#f0">find</a> (serviceName);
00339
00340 <font class="comment">// in case of group, we can return association only if there s only one service on it</font>
00341 <a class="code" href="debug_8h.html#a6">nlassert</a> ((*itbm).second.NetBase.size() == 1);
00342
00343 <font class="keywordflow">return</font> (*itbm).second.NetBase[0]->getSIDA ();
00344 }
00345
<a name="l00346"></a><a class="code" href="classNLNET_1_1CNetManager.html#d7">00346</a> <font class="keywordtype">void</font> CNetManager::addCallbackArray (<font class="keyword">const</font> std::string &serviceName, <font class="keyword">const</font> TCallbackItem *callbackarray, <a class="code" href="classNLMISC_1_1CStringIdArray.html#s0">NLMISC::CStringIdArray::TStringId</a> arraysize)
00347 {
00348 <a class="code" href="debug_8h.html#a0">nldebug</a> (<font class="stringliteral">"HNETL4: addingCallabckArray() for service '%s'"</font>, serviceName.c_str ());
00349 <a class="code" href="classNLNET_1_1CNetManager.html#u1">ItBaseMap</a> itbm = <a class="code" href="classNLNET_1_1CNetManager.html#f0">find</a> (serviceName);
00350 <font class="keywordflow">for</font> (uint32 i = 0; i < (*itbm).second.NetBase.size(); i++)
00351 {
00352 <font class="comment">// if ((*itbm).second.NetBase[i]->connected())</font>
00353 (*itbm).second.NetBase[i]->addCallbackArray (callbackarray, arraysize);
00354 }
00355 }
00356
<a name="l00357"></a><a class="code" href="classNLNET_1_1CNetManager.html#d8">00357</a> <font class="keywordtype">void</font> CNetManager::update (<a class="code" href="namespaceNLMISC.html#a183">TTime</a> timeout)
00358 {
00359 <font class="comment">// nldebug ("HNETL4: update()");</font>
00360
00361 <font class="comment">// sint64 p1 = CTime::getPerformanceTime ();</font>
00362
00363 <a class="code" href="namespaceNLMISC.html#a183">TTime</a> t0 = CTime::getLocalTime ();
00364
00365 <font class="keywordflow">if</font> (timeout > 0)
00366 {
00367 <font class="keywordflow">if</font> (<a class="code" href="classNLNET_1_1CNetManager.html#r2">_NextUpdateTime</a> == 0)
00368 {
00369 <a class="code" href="classNLNET_1_1CNetManager.html#r2">_NextUpdateTime</a> = t0 + timeout;
00370 }
00371 <font class="keywordflow">else</font>
00372 {
00373 <a class="code" href="namespaceNLMISC.html#a183">TTime</a> err = t0 - <a class="code" href="classNLNET_1_1CNetManager.html#r2">_NextUpdateTime</a>;
00374 <a class="code" href="classNLNET_1_1CNetManager.html#r2">_NextUpdateTime</a> += timeout;
00375
00376 <font class="comment">// if we are too late, resync to the next value</font>
00377 <font class="keywordflow">while</font> (err > timeout)
00378 {
00379 err -= timeout;
00380 <a class="code" href="classNLNET_1_1CNetManager.html#r2">_NextUpdateTime</a> += timeout;
00381 }
00382
00383 timeout -= err;
00384 <font class="keywordflow">if</font> (timeout < 0) timeout = 0;
00385 }
00386 }
00387
00388 <font class="comment">// sint64 p2 = CTime::getPerformanceTime ();</font>
00389
00390 <font class="keywordflow">while</font> (true)
00391 {
00392 <font class="keywordflow">for</font> (<a class="code" href="classNLNET_1_1CNetManager.html#u1">ItBaseMap</a> itbm = <a class="code" href="classNLNET_1_1CNetManager.html#r0">_BaseMap</a>.begin (); itbm != <a class="code" href="classNLNET_1_1CNetManager.html#r0">_BaseMap</a>.end (); itbm++)
00393 {
00394 <font class="keywordflow">for</font> (uint32 i = 0; i < (*itbm).second.NetBase.size(); i++)
00395 {
00397 <font class="comment">// we get and treat all messages in this connection</font>
00398 (*itbm).second.NetBase[i]->update (0);
00399 <font class="keywordflow">if</font> ((*itbm).second.NetBase[i]->connected())
00400 {
00401 <font class="comment">// if connected, update</font>
00402 <font class="comment">// (*itbm).second.NetBase[i]->update ();</font>
00403 }
00404 <font class="keywordflow">else</font>
00405 {
00406 <font class="keyword">static</font> <a class="code" href="namespaceNLMISC.html#a183">TTime</a> lastTime = CTime::getLocalTime();
00407 <font class="keywordflow">if</font> (CTime::getLocalTime() - lastTime > 5000)
00408 {
00409 lastTime = CTime::getLocalTime();
00410
00411 <font class="comment">// if not connected, try to connect ClientWithAddr</font>
00412 <font class="keywordflow">if</font> ((*itbm).second.Type == CBaseStruct::ClientWithAddr && (*itbm).second.AutoRetry)
00413 {
00414 CCallbackClient *cc = dynamic_cast<CCallbackClient *>((*itbm).second.NetBase[i]);
00415 <font class="keywordflow">try</font>
00416 {
00417 <a class="code" href="debug_8h.html#a6">nlassert</a> ((*itbm).second.ServiceNames.size()==1);
00418 cc->connect (CInetAddress((*itbm).second.ServiceNames[0]));
00419
00420 <font class="keywordflow">if</font> ((*itbm).second.ConnectionCallback != NULL)
00421 (*itbm).second.ConnectionCallback ((*itbm).second.Name, cc->getSockId(), (*itbm).second.ConnectionCbArg);
00422 }
00423 <font class="keywordflow">catch</font> (ESocketConnectionFailed &e)
00424 {
00425 <font class="comment">// can't connect now, try later</font>
00426 <a class="code" href="debug_8h.html#a1">nlinfo</a>(<font class="stringliteral">"HNETL4: can't connect now to %s (reason: %s)"</font>, (*itbm).second.ServiceNames[0].c_str(), e.what());
00427 }
00428 }
00429 }
00430 }
00431 }
00432 }
00433
00434 <font class="comment">// If it's the end, don't nlSleep()</font>
00435 <font class="keywordflow">if</font> (CTime::getLocalTime() - t0 > timeout)
00436 <font class="keywordflow">break</font>;
00437
00438 <font class="comment">// Enable windows multithreading before rescanning all connections</font>
00439 <font class="comment">// slow down the layer H_BEFORE (CNetManager_update_nlSleep);</font>
00440 <a class="code" href="namespaceNLMISC.html#a236">nlSleep</a> (1);
00441 <font class="comment">// slow down the layer H_AFTER (CNetManager_update_nlSleep);</font>
00442 }
00443
00444 <font class="comment">// sint64 p3 = CTime::getPerformanceTime ();</font>
00445
00446 <font class="keywordflow">if</font> (CNamingClient::connected ())
00447 CNamingClient::update ();
00448
00449 <font class="comment">// sint64 p4 = CTime::getPerformanceTime ();</font>
00450
00451 <font class="comment">// nlinfo("time : %f %f %f %d", CTime::ticksToSecond(p2-p1), CTime::ticksToSecond(p3-p2), CTime::ticksToSecond(p4-p3), timeout);</font>
00452 }
00453
00454
<a name="l00455"></a><a class="code" href="classNLNET_1_1CNetManager.html#d9">00455</a> <font class="keywordtype">void</font> CNetManager::send (<font class="keyword">const</font> std::string &serviceName, <font class="keyword">const</font> CMessage &buffer, <a class="code" href="namespaceNLNET.html#a0">TSockId</a> hostid)
00456 {
00457 <a class="code" href="debug_8h.html#a0">nldebug</a> (<font class="stringliteral">"HNETL4: send for service '%s' message %s to %s"</font>, serviceName.c_str(), buffer.toString().c_str(), hostid->asString().c_str());
00458 <a class="code" href="classNLNET_1_1CNetManager.html#u1">ItBaseMap</a> itbm = <a class="code" href="classNLNET_1_1CNetManager.html#f0">find</a> (serviceName);
00459 <font class="keywordflow">for</font> (uint32 i = 0; i < (*itbm).second.NetBase.size(); i++)
00460 {
00461 <font class="keywordflow">if</font> ((*itbm).second.NetBase[i]->connected())
00462 (*itbm).second.NetBase[i]->send (buffer, hostid);
00463 }
00464 }
00465
<a name="l00466"></a><a class="code" href="classNLNET_1_1CNetManager.html#d13">00466</a> CCallbackNetBase *CNetManager::getNetBase (<font class="keyword">const</font> std::string &serviceName)
00467 {
00468 <a class="code" href="classNLNET_1_1CNetManager.html#u1">ItBaseMap</a> itbm = <a class="code" href="classNLNET_1_1CNetManager.html#f0">find</a> (serviceName);
00469 <font class="keywordflow">return</font> (*itbm).second.NetBase[0];
00470 }
00471
<a name="l00472"></a><a class="code" href="classNLNET_1_1CNetManager.html#d10">00472</a> <font class="keywordtype">void</font> CNetManager::setConnectionCallback (<font class="keyword">const</font> std::string &serviceName, <a class="code" href="namespaceNLNET.html#a22">TNetManagerCallback</a> cb, <font class="keywordtype">void</font> *arg)
00473 {
00474 <a class="code" href="debug_8h.html#a0">nldebug</a> (<font class="stringliteral">"HNETL4: setConnectionCallback() for service '%s'"</font>, serviceName.c_str ());
00475 <a class="code" href="classNLNET_1_1CNetManager.html#u1">ItBaseMap</a> itbm = <a class="code" href="classNLNET_1_1CNetManager.html#f0">find</a> (serviceName);
00476 (*itbm).second.ConnectionCallback = cb;
00477 (*itbm).second.ConnectionCbArg = arg;
00478 }
00479
<a name="l00480"></a><a class="code" href="classNLNET_1_1CNetManager.html#d11">00480</a> <font class="keywordtype">void</font> CNetManager::setDisconnectionCallback (<font class="keyword">const</font> std::string &serviceName, <a class="code" href="namespaceNLNET.html#a22">TNetManagerCallback</a> cb, <font class="keywordtype">void</font> *arg)
00481 {
00482 <a class="code" href="debug_8h.html#a0">nldebug</a> (<font class="stringliteral">"HNETL4: setDisconnectionCallback() for service '%s'"</font>, serviceName.c_str ());
00483 <a class="code" href="classNLNET_1_1CNetManager.html#u1">ItBaseMap</a> itbm = <a class="code" href="classNLNET_1_1CNetManager.html#f0">find</a> (serviceName);
00484 (*itbm).second.DisconnectionCallback = cb;
00485 (*itbm).second.DisconnectionCbArg = arg;
00486 }
00487
00488
<a name="l00489"></a><a class="code" href="classNLNET_1_1CNetManager.html#f0">00489</a> CNetManager::ItBaseMap CNetManager::find (<font class="keyword">const</font> std::string &serviceName)
00490 {
00491 <font class="comment">// find the service or add it if not found</font>
00492 pair<ItBaseMap, bool> p;
00493 p = <a class="code" href="classNLNET_1_1CNetManager.html#r0">_BaseMap</a>.insert (make_pair (serviceName, CBaseStruct (serviceName)));
00494 <font class="keywordflow">return</font> p.first;
00495 }
00496
<a name="l00497"></a><a class="code" href="classNLNET_1_1CNetManager.html#d16">00497</a> uint64 CNetManager::getBytesSent ()
00498 {
00499 uint64 sent = 0;
00500 <font class="keywordflow">for</font> (<a class="code" href="classNLNET_1_1CNetManager.html#u1">ItBaseMap</a> itbm = <a class="code" href="classNLNET_1_1CNetManager.html#r0">_BaseMap</a>.begin (); itbm != <a class="code" href="classNLNET_1_1CNetManager.html#r0">_BaseMap</a>.end (); itbm++)
00501 {
00502 <font class="keywordflow">for</font> (uint32 i = 0; i < (*itbm).second.NetBase.size(); i++)
00503 {
00504 sent += (*itbm).second.NetBase[i]->getBytesSent ();
00505 }
00506 }
00507 <font class="keywordflow">return</font> sent;
00508 }
00509
<a name="l00510"></a><a class="code" href="classNLNET_1_1CNetManager.html#d17">00510</a> uint64 CNetManager::getBytesReceived ()
00511 {
00512 uint64 received = 0;
00513 <font class="keywordflow">for</font> (<a class="code" href="classNLNET_1_1CNetManager.html#u1">ItBaseMap</a> itbm = <a class="code" href="classNLNET_1_1CNetManager.html#r0">_BaseMap</a>.begin (); itbm != <a class="code" href="classNLNET_1_1CNetManager.html#r0">_BaseMap</a>.end (); itbm++)
00514 {
00515 <font class="keywordflow">for</font> (uint32 i = 0; i < (*itbm).second.NetBase.size(); i++)
00516 {
00517 received += (*itbm).second.NetBase[i]->getBytesReceived ();
00518 }
00519 }
00520 <font class="keywordflow">return</font> received;
00521 }
00522
<a name="l00523"></a><a class="code" href="classNLNET_1_1CNetManager.html#d19">00523</a> uint64 CNetManager::getSendQueueSize ()
00524 {
00525 uint64 val = 0;
00526 <font class="keywordflow">for</font> (<a class="code" href="classNLNET_1_1CNetManager.html#u1">ItBaseMap</a> itbm = <a class="code" href="classNLNET_1_1CNetManager.html#r0">_BaseMap</a>.begin (); itbm != <a class="code" href="classNLNET_1_1CNetManager.html#r0">_BaseMap</a>.end (); itbm++)
00527 {
00528 <font class="keywordflow">for</font> (uint32 i = 0; i < (*itbm).second.NetBase.size(); i++)
00529 {
00530 val += (*itbm).second.NetBase[i]->getSendQueueSize ();
00531 }
00532 }
00533 <font class="keywordflow">return</font> val;
00534 }
00535
<a name="l00536"></a><a class="code" href="classNLNET_1_1CNetManager.html#d18">00536</a> uint64 CNetManager::getReceiveQueueSize ()
00537 {
00538 uint64 val = 0;
00539 <font class="keywordflow">for</font> (<a class="code" href="classNLNET_1_1CNetManager.html#u1">ItBaseMap</a> itbm = <a class="code" href="classNLNET_1_1CNetManager.html#r0">_BaseMap</a>.begin (); itbm != <a class="code" href="classNLNET_1_1CNetManager.html#r0">_BaseMap</a>.end (); itbm++)
00540 {
00541 <font class="keywordflow">for</font> (uint32 i = 0; i < (*itbm).second.NetBase.size(); i++)
00542 {
00543 val += (*itbm).second.NetBase[i]->getReceiveQueueSize ();
00544 }
00545 }
00546 <font class="keywordflow">return</font> val;
00547 }
00548
00549 } <font class="comment">// NLNET</font>
</pre></div>
<!-- footer -->
<BR><FONT Size=+5> </FONT>
</TD>
<TD WIDTH=15><IMG SRC=/inc/img/pixel.gif WIDTH=15 HEIGHT=15 BORDER=0 ALT=""></TD>
</TR>
</TABLE>
</BODY>
</HTML>
|