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
|
<!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=http://www.nevrax.com><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>buf_client.cpp</h1><a href="buf__client_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 <font class="preprocessor">#include "<a class="code" href="stdnet_8h.html">stdnet.h</a>"</font>
00027
00028 <font class="preprocessor">#include "<a class="code" href="hierarchical__timer_8h.html">nel/misc/hierarchical_timer.h</a>"</font>
00029
00030 <font class="preprocessor">#include "<a class="code" href="buf__client_8h.html">nel/net/buf_client.h</a>"</font>
00031 <font class="preprocessor">#include "<a class="code" href="thread_8h.html">nel/misc/thread.h</a>"</font>
00032 <font class="preprocessor">#include "<a class="code" href="dummy__tcp__sock_8h.html">nel/net/dummy_tcp_sock.h</a>"</font>
00033
00034 <font class="preprocessor">#ifdef NL_OS_WINDOWS</font>
00035 <font class="preprocessor"></font><font class="preprocessor">#include <winsock2.h></font>
00036 <font class="preprocessor">#elif defined NL_OS_UNIX</font>
00037 <font class="preprocessor"></font><font class="preprocessor">#include <netinet/in.h></font>
00038 <font class="preprocessor">#endif</font>
00039 <font class="preprocessor"></font>
00040 <font class="keyword">using</font> <font class="keyword">namespace </font>NLMISC;
00041 <font class="keyword">using</font> <font class="keyword">namespace </font>std;
00042
00043
00044 <font class="keyword">namespace </font>NLNET {
00045
00046
00047 <font class="comment">/***************************************************************************************************</font>
00048 <font class="comment"> * User main thread (initialization)</font>
00049 <font class="comment"> **************************************************************************************************/</font>
00050
00051 <font class="comment">/*</font>
00052 <font class="comment"> * Constructor</font>
00053 <font class="comment"> */</font>
<a name="l00054"></a><a class="code" href="classNLNET_1_1CBufClient.html#a0">00054</a> CBufClient::CBufClient( <font class="keywordtype">bool</font> nodelay, <font class="keywordtype">bool</font> replaymode ) :
00055 CBufNetBase(),
00056 _NoDelay( nodelay ),
00057 _PrevBytesDownloaded( 0 ),
00058 _PrevBytesUploaded( 0 ),
00059 _RecvTask( NULL ),
00060 _RecvThread( NULL )
00061 <font class="comment">/*_PrevBytesReceived( 0 ),</font>
00062 <font class="comment"> _PrevBytesSent( 0 )*/</font>
00063 {
00064 <a class="code" href="buf__sock_8h.html#a0">nlnettrace</a>( <font class="stringliteral">"CBufClient::CBufClient"</font> ); <font class="comment">// don't define a global object</font>
00065
00066 <font class="keywordflow">if</font> ( replaymode )
00067 {
00068 <a class="code" href="classNLNET_1_1CBufClient.html#n0">_BufSock</a> = <font class="keyword">new</font> CBufSock( <font class="keyword">new</font> CDummyTcpSock() );
00069 }
00070 <font class="keywordflow">else</font>
00071 {
00072 <a class="code" href="classNLNET_1_1CBufClient.html#n0">_BufSock</a> = <font class="keyword">new</font> CBufSock();
00073 <a class="code" href="classNLNET_1_1CBufClient.html#o0">_RecvTask</a> = <font class="keyword">new</font> <a class="code" href="classNLNET_1_1CBufClient.html#l0">CClientReceiveTask</a>( <font class="keyword">this</font>, <a class="code" href="classNLNET_1_1CBufClient.html#n0">_BufSock</a> );
00074 }
00075 }
00076
00077
00078 <font class="comment">/*</font>
00079 <font class="comment"> * Connects to the specified host</font>
00080 <font class="comment"> * Precond: not connected</font>
00081 <font class="comment"> */</font>
<a name="l00082"></a><a class="code" href="classNLNET_1_1CBufClient.html#a2">00082</a> <font class="keywordtype">void</font> CBufClient::connect( <font class="keyword">const</font> CInetAddress& <a class="code" href="driver__opengl__extension__def_8h.html#a414">addr</a> )
00083 {
00084 <a class="code" href="buf__sock_8h.html#a0">nlnettrace</a>( <font class="stringliteral">"CBufClient::connect"</font> );
00085 <a class="code" href="debug_8h.html#a6">nlassert</a>( ! <a class="code" href="classNLNET_1_1CBufClient.html#n0">_BufSock</a>->Sock->connected() );
00086 <a class="code" href="classNLNET_1_1CBufClient.html#n0">_BufSock</a>->connect( <a class="code" href="driver__opengl__extension__def_8h.html#a414">addr</a>, <a class="code" href="classNLNET_1_1CBufClient.html#n1">_NoDelay</a>, <font class="keyword">true</font> );
00087 <a class="code" href="classNLNET_1_1CBufClient.html#n2">_PrevBytesDownloaded</a> = 0;
00088 <a class="code" href="classNLNET_1_1CBufClient.html#n3">_PrevBytesUploaded</a> = 0;
00089 <font class="comment">/*_PrevBytesReceived = 0;</font>
00090 <font class="comment"> _PrevBytesSent = 0;*/</font>
00091
00092 <font class="comment">// Allow reconnection</font>
00093 <font class="keywordflow">if</font> ( <a class="code" href="classNLNET_1_1CBufClient.html#o1">_RecvThread</a> != NULL )
00094 {
00095 <font class="keyword">delete</font> <a class="code" href="classNLNET_1_1CBufClient.html#o1">_RecvThread</a>;
00096 }
00097
00098 <a class="code" href="classNLNET_1_1CBufClient.html#o1">_RecvThread</a> = IThread::create( <a class="code" href="classNLNET_1_1CBufClient.html#o0">_RecvTask</a> );
00099 <a class="code" href="classNLNET_1_1CBufClient.html#o1">_RecvThread</a>-><a class="code" href="classNLMISC_1_1IThread.html#a1">start</a>();
00100 }
00101
00102
00103 <font class="comment">/***************************************************************************************************</font>
00104 <font class="comment"> * User main thread (running)</font>
00105 <font class="comment"> **************************************************************************************************/</font>
00106
00107
00108 <font class="comment">/*</font>
00109 <font class="comment"> * Sends a message to the remote host</font>
00110 <font class="comment"> */</font>
<a name="l00111"></a><a class="code" href="classNLNET_1_1CBufClient.html#a4">00111</a> <font class="keywordtype">void</font> CBufClient::send( <font class="keyword">const</font> <a class="code" href="classNLMISC_1_1CMemStream.html">NLMISC::CMemStream</a>& buffer )
00112 {
00113 <a class="code" href="buf__sock_8h.html#a0">nlnettrace</a>( <font class="stringliteral">"CBufClient::send"</font> );
00114 <a class="code" href="debug_8h.html#a6">nlassert</a>( buffer.<a class="code" href="classNLMISC_1_1CMemStream.html#a10">length</a>() > 0 );
00115 <a class="code" href="debug_8h.html#a6">nlassert</a>( buffer.<a class="code" href="classNLMISC_1_1CMemStream.html#a10">length</a>() <= <a class="code" href="classNLNET_1_1CBufNetBase.html#a6">maxSentBlockSize</a>() );
00116
00117 <font class="comment">// slow down the layer H_AUTO (CBufServer_send);</font>
00118
00119 <font class="keywordflow">if</font> ( ! <a class="code" href="classNLNET_1_1CBufClient.html#n0">_BufSock</a>->pushBuffer( buffer ) )
00120 {
00121 <font class="comment">// Disconnection event if disconnected</font>
00122 <a class="code" href="classNLNET_1_1CBufClient.html#n0">_BufSock</a>->advertiseDisconnection( <font class="keyword">this</font>, NULL );
00123 }
00124 }
00125
00126
00127 <font class="comment">/*</font>
00128 <font class="comment"> * Checks if there are some data to receive</font>
00129 <font class="comment"> */</font>
<a name="l00130"></a><a class="code" href="classNLNET_1_1CBufClient.html#a5">00130</a> <font class="keywordtype">bool</font> CBufClient::dataAvailable()
00131 {
00132 <font class="comment">// slow down the layer H_AUTO (CBufClient_dataAvailable);</font>
00133 {
00134 <font class="comment">/* If no data available, enter the 'while' loop and return false (1 volatile test)</font>
00135 <font class="comment"> * If there are user data available, enter the 'while' and return true immediately (1 volatile test + 1 short locking)</font>
00136 <font class="comment"> * If there is a disconnection event (rare), call the callback and loop</font>
00137 <font class="comment"> */</font>
00138 <font class="keywordflow">while</font> ( <a class="code" href="classNLNET_1_1CBufNetBase.html#b7">dataAvailableFlag</a>() )
00139 {
00140 <font class="comment">// Because _DataAvailable is true, the receive queue is not empty at this point</font>
00141 uint8 val;
00142 {
00143 <a class="code" href="namespaceNLNET.html#a5">CFifoAccessor</a> recvfifo( &<a class="code" href="classNLNET_1_1CBufNetBase.html#b1">receiveQueue</a>() );
00144 val = recvfifo.value().frontLast ();
00145 }
00146
00147 <font class="comment">// Test if it the next block is a system event</font>
00148 <font class="keywordflow">switch</font> ( val )
00149 {
00150
00151 <font class="comment">// Normal message available</font>
00152 <font class="keywordflow">case</font> CBufNetBase::User:
00153 <font class="keywordflow">return</font> <font class="keyword">true</font>; <font class="comment">// return immediatly, do not extract the message</font>
00154
00155 <font class="comment">// Process disconnection event</font>
00156 <font class="keywordflow">case</font> CBufNetBase::Disconnection:
00157
00158 <a class="code" href="debug_8h.html#a0">nldebug</a>( <font class="stringliteral">"Disconnection event"</font> );
00159 <a class="code" href="classNLNET_1_1CBufClient.html#n0">_BufSock</a>->setConnectedState( <font class="keyword">false</font> );
00160
00161 <font class="comment">// Call callback if needed</font>
00162 <font class="keywordflow">if</font> ( <a class="code" href="classNLNET_1_1CBufNetBase.html#b2">disconnectionCallback</a>() != NULL )
00163 {
00164 <a class="code" href="classNLNET_1_1CBufNetBase.html#b2">disconnectionCallback</a>()( <a class="code" href="classNLNET_1_1CBufClient.html#a18">id</a>(), <a class="code" href="classNLNET_1_1CBufNetBase.html#b3">argOfDisconnectionCallback</a>() );
00165 }
00166
00167 <font class="comment">// Unlike the server version, we do not delete the CBufSock object here,</font>
00168 <font class="comment">// it will be done in the destructor of CBufClient</font>
00169 <font class="keywordflow">break</font>;
00170
00171 <font class="keywordflow">default</font>: <font class="comment">// should not occur</font>
00172 {
00173 <a class="code" href="namespaceNLNET.html#a5">CFifoAccessor</a> recvfifo( &<a class="code" href="classNLNET_1_1CBufNetBase.html#b1">receiveQueue</a>() );
00174 vector<uint8> buffer;
00175 recvfifo.value().front (buffer);
00176 <a class="code" href="debug_8h.html#a1">nlinfo</a>( <font class="stringliteral">"LNETL1: Invalid block type: %hu (should be = %hu)"</font>, (uint16)(buffer[buffer.size()-1]), (uint16)val );
00177 <a class="code" href="debug_8h.html#a1">nlinfo</a>( <font class="stringliteral">"LNETL1: Buffer (%d B): [%s]"</font>, buffer.size(), stringFromVector(buffer).c_str() );
00178 <a class="code" href="debug_8h.html#a1">nlinfo</a>( <font class="stringliteral">"LNETL1: Receive queue:"</font> );
00179 recvfifo.value().display();
00180 <a class="code" href="debug_8h.html#a3">nlerror</a>( <font class="stringliteral">"LNETL1: Invalid system event type in client receive queue"</font> );
00181 }
00182 }
00183 <font class="comment">// Extract system event</font>
00184 {
00185 <a class="code" href="namespaceNLNET.html#a5">CFifoAccessor</a> recvfifo( &<a class="code" href="classNLNET_1_1CBufNetBase.html#b1">receiveQueue</a>() );
00186 recvfifo.value().pop();
00187 <a class="code" href="classNLNET_1_1CBufNetBase.html#b6">setDataAvailableFlag</a>( ! recvfifo.value().empty() );
00188 }
00189
00190 }
00191 <font class="comment">// _DataAvailable is false here</font>
00192 <font class="keywordflow">return</font> <font class="keyword">false</font>;
00193 }
00194 }
00195
00196
00197 <font class="comment">/* // OLD VERSION</font>
00198 <font class="comment">bool CBufClient::dataAvailable()</font>
00199 <font class="comment">{</font>
00200 <font class="comment"> // slow down the layer H_AUTO (CBufClient_dataAvailable);</font>
00201 <font class="comment"> {</font>
00202 <font class="comment"> CFifoAccessor recvfifo( &receiveQueue() );</font>
00203 <font class="comment"> do</font>
00204 <font class="comment"> {</font>
00205 <font class="comment"> // Check if the receive queue is empty</font>
00206 <font class="comment"> if ( recvfifo.value().empty() )</font>
00207 <font class="comment"> {</font>
00208 <font class="comment"> return false;</font>
00209 <font class="comment"> }</font>
00210 <font class="comment"> else</font>
00211 <font class="comment"> {</font>
00212 <font class="comment"> uint8 val = recvfifo.value().frontLast ();</font>
00213 <font class="comment"></font>
00214 <font class="comment"> // Test if it the next block is a system event</font>
00215 <font class="comment"> switch ( val )</font>
00216 <font class="comment"> {</font>
00217 <font class="comment"> </font>
00218 <font class="comment"> // Normal message available</font>
00219 <font class="comment"> case CBufNetBase::User:</font>
00220 <font class="comment"> return true; // return immediatly, do not extract the message</font>
00221 <font class="comment"></font>
00222 <font class="comment"> // Process disconnection event</font>
00223 <font class="comment"> case CBufNetBase::Disconnection:</font>
00224 <font class="comment"></font>
00225 <font class="comment"> nldebug( "Disconnection event" );</font>
00226 <font class="comment"> _BufSock->setConnectedState( false );</font>
00227 <font class="comment"></font>
00228 <font class="comment"> // Call callback if needed</font>
00229 <font class="comment"> if ( disconnectionCallback() != NULL )</font>
00230 <font class="comment"> {</font>
00231 <font class="comment"> disconnectionCallback()( id(), argOfDisconnectionCallback() );</font>
00232 <font class="comment"> }</font>
00233 <font class="comment"></font>
00234 <font class="comment"> // Unlike the server version, we do not delete the CBufSock object here,</font>
00235 <font class="comment"> // it will be done in the destructor of CBufClient</font>
00236 <font class="comment"> break;</font>
00237 <font class="comment"></font>
00238 <font class="comment"> default:</font>
00239 <font class="comment"> {</font>
00240 <font class="comment"> vector<uint8> buffer;</font>
00241 <font class="comment"> recvfifo.value().front (buffer);</font>
00242 <font class="comment"> nlinfo( "LNETL1: Invalid block type: %hu (should be = %hu)", (uint16)(buffer[buffer.size()-1]), (uint16)val );</font>
00243 <font class="comment"> nlinfo( "LNETL1: Buffer (%d B): [%s]", buffer.size(), stringFromVector(buffer).c_str() );</font>
00244 <font class="comment"> nlinfo( "LNETL1: Receive queue:" );</font>
00245 <font class="comment"> recvfifo.value().display();</font>
00246 <font class="comment"> nlerror( "LNETL1: Invalid system event type in client receive queue" );</font>
00247 <font class="comment"> }</font>
00248 <font class="comment"> }</font>
00249 <font class="comment"> // Extract system event</font>
00250 <font class="comment"> recvfifo.value().pop();</font>
00251 <font class="comment"> }</font>
00252 <font class="comment"> }</font>
00253 <font class="comment"> while ( true );</font>
00254 <font class="comment"> }</font>
00255 <font class="comment">}</font>
00256 <font class="comment">*/</font>
00257
00258 <font class="comment">/*</font>
00259 <font class="comment"> * Receives next block of data in the specified buffer (resizes the vector)</font>
00260 <font class="comment"> * Precond: dataAvailable() has returned true</font>
00261 <font class="comment"> */</font>
<a name="l00262"></a><a class="code" href="classNLNET_1_1CBufClient.html#a6">00262</a> <font class="keywordtype">void</font> CBufClient::receive( <a class="code" href="classNLMISC_1_1CMemStream.html">NLMISC::CMemStream</a>& buffer )
00263 {
00264 <a class="code" href="buf__sock_8h.html#a0">nlnettrace</a>( <font class="stringliteral">"CBufClient::receive"</font> );
00265 <font class="comment">//nlassert( dataAvailable() );</font>
00266
00267 <font class="comment">// Extract buffer from the receive queue</font>
00268 {
00269 <a class="code" href="namespaceNLNET.html#a5">CFifoAccessor</a> recvfifo( &<a class="code" href="classNLNET_1_1CBufNetBase.html#b1">receiveQueue</a>() );
00270 <a class="code" href="debug_8h.html#a6">nlassert</a>( ! recvfifo.value().empty() );
00271 recvfifo.value().front( buffer );
00272 recvfifo.value().pop();
00273 <a class="code" href="classNLNET_1_1CBufNetBase.html#b6">setDataAvailableFlag</a>( ! recvfifo.value().empty() );
00274 }
00275
00276 <font class="comment">// Extract event type</font>
00277 <a class="code" href="debug_8h.html#a6">nlassert</a>( buffer.<a class="code" href="classNLMISC_1_1CMemStream.html#a11">buffer</a>()[buffer.<a class="code" href="classNLMISC_1_1CMemStream.html#a10">length</a>()-1] == CBufNetBase::User );
00278 <font class="comment">//commented for optimisation nldebug( "LNETL1: Client read buffer (%d+%d B)", buffer.length(), sizeof(TSockId)+1 );</font>
00279 buffer.<a class="code" href="classNLMISC_1_1CMemStream.html#a14">resize</a>( buffer.<a class="code" href="classNLMISC_1_1CMemStream.html#a10">length</a>()-1 );
00280 }
00281
00282
00283 <font class="comment">/*</font>
00284 <font class="comment"> * Update the network (call this method evenly)</font>
00285 <font class="comment"> */</font>
<a name="l00286"></a><a class="code" href="classNLNET_1_1CBufClient.html#a7">00286</a> <font class="keywordtype">void</font> CBufClient::update()
00287 {
00288 <font class="comment">//nlnettrace( "CBufClient::update" );</font>
00289
00290 <font class="comment">// Update sending</font>
00291 <font class="keywordtype">bool</font> sendingok = <a class="code" href="classNLNET_1_1CBufClient.html#n0">_BufSock</a>->update();
00292
00293 <font class="comment">// Disconnection event if disconnected</font>
00294 <font class="keywordflow">if</font> ( ! ( <a class="code" href="classNLNET_1_1CBufClient.html#n0">_BufSock</a>->Sock->connected() && sendingok ) )
00295 {
00296 <font class="keywordflow">if</font> ( <a class="code" href="classNLNET_1_1CBufClient.html#n0">_BufSock</a>->Sock->connected() )
00297 {
00298 <a class="code" href="classNLNET_1_1CBufClient.html#n0">_BufSock</a>->Sock->disconnect();
00299 }
00300 <a class="code" href="classNLNET_1_1CBufClient.html#n0">_BufSock</a>->advertiseDisconnection( <font class="keyword">this</font>, NULL );
00301 }
00302 }
00303
00304
00305 <font class="comment">/*</font>
00306 <font class="comment"> * Disconnect the remote host</font>
00307 <font class="comment"> */</font>
<a name="l00308"></a><a class="code" href="classNLNET_1_1CBufClient.html#a3">00308</a> <font class="keywordtype">void</font> CBufClient::disconnect( <font class="keywordtype">bool</font> quick )
00309 {
00310 <a class="code" href="buf__sock_8h.html#a0">nlnettrace</a>( <font class="stringliteral">"CBufClient::disconnect"</font> );
00311
00312 <font class="comment">// Do not allow to disconnect a socket that is not connected</font>
00313 <a class="code" href="debug_8h.html#a6">nlassert</a>( <a class="code" href="classNLNET_1_1CBufClient.html#n0">_BufSock</a>->connectedState() );
00314
00315 <font class="comment">// When the NS tells us to remove this connection AND the connection has physically</font>
00316 <font class="comment">// disconnected but not yet logically (i.e. disconnection event not processed yet),</font>
00317 <font class="comment">// skip flushing and physical active disconnection</font>
00318 <font class="keywordflow">if</font> ( <a class="code" href="classNLNET_1_1CBufClient.html#n0">_BufSock</a>->Sock->connected() )
00319 {
00320 <font class="comment">// Flush sending is asked for</font>
00321 <font class="keywordflow">if</font> ( ! quick )
00322 {
00323 <a class="code" href="classNLNET_1_1CBufClient.html#n0">_BufSock</a>->flush();
00324 }
00325
00326 <font class="comment">// Disconnect and prevent from advertising the disconnection</font>
00327 <a class="code" href="classNLNET_1_1CBufClient.html#n0">_BufSock</a>->disconnect( <font class="keyword">false</font> );
00328 }
00329
00330 <font class="comment">// Empty the receive queue</font>
00331 {
00332 <a class="code" href="namespaceNLNET.html#a5">CFifoAccessor</a> recvfifo( &<a class="code" href="classNLNET_1_1CBufNetBase.html#b1">receiveQueue</a>() );
00333 recvfifo.value().clear();
00334 <a class="code" href="classNLNET_1_1CBufNetBase.html#b6">setDataAvailableFlag</a>( <font class="keyword">false</font> );
00335 }
00336 }
00337
00338
00339 <font class="comment">// Utility function for newBytes...()</font>
00340 <font class="keyword">inline</font> uint64 <a class="code" href="namespaceNLNET.html#a86">updateStatCounter</a>( uint64& counter, uint64 newvalue )
00341 {
00342 uint64 result = newvalue - counter;
00343 counter = newvalue;
00344 <font class="keywordflow">return</font> result;
00345 }
00346
00347
00348 <font class="comment">/*</font>
00349 <font class="comment"> * Returns the number of bytes downloaded since the previous call to this method</font>
00350 <font class="comment"> */</font>
<a name="l00351"></a><a class="code" href="classNLNET_1_1CBufClient.html#a16">00351</a> uint64 CBufClient::newBytesDownloaded()
00352 {
00353 <font class="keywordflow">return</font> <a class="code" href="namespaceNLNET.html#a86">updateStatCounter</a>( <a class="code" href="classNLNET_1_1CBufClient.html#n2">_PrevBytesDownloaded</a>, <a class="code" href="classNLNET_1_1CBufClient.html#a14">bytesDownloaded</a>() );
00354 }
00355
00356
00357 <font class="comment">/*</font>
00358 <font class="comment"> * Returns the number of bytes uploaded since the previous call to this method</font>
00359 <font class="comment"> */</font>
<a name="l00360"></a><a class="code" href="classNLNET_1_1CBufClient.html#a17">00360</a> uint64 CBufClient::newBytesUploaded()
00361 {
00362 <font class="keywordflow">return</font> <a class="code" href="namespaceNLNET.html#a86">updateStatCounter</a>( <a class="code" href="classNLNET_1_1CBufClient.html#n3">_PrevBytesUploaded</a>, <a class="code" href="classNLNET_1_1CBufClient.html#a15">bytesUploaded</a>() );
00363 }
00364
00365
00366 <font class="comment">/*</font>
00367 <font class="comment"> * Returns the number of bytes popped by receive() since the previous call to this method</font>
00368 <font class="comment"> */</font>
00369 <font class="comment">/*uint64 CBufClient::newBytesReceived()</font>
00370 <font class="comment">{</font>
00371 <font class="comment"> return updateStatCounter( _PrevBytesReceived, bytesReceived() );</font>
00372 <font class="comment">}*/</font>
00373
00374
00375 <font class="comment">/*</font>
00376 <font class="comment"> * Returns the number of bytes pushed by send() since the previous call to this method</font>
00377 <font class="comment"> */</font>
00378 <font class="comment">/*uint64 CBufClient::newBytesSent()</font>
00379 <font class="comment">{</font>
00380 <font class="comment"> return updateStatCounter( _PrevBytesSent, bytesSent() );</font>
00381 <font class="comment">}*/</font>
00382
00383
00384 <font class="comment">/*</font>
00385 <font class="comment"> * Destructor</font>
00386 <font class="comment"> */</font>
<a name="l00387"></a><a class="code" href="classNLNET_1_1CBufClient.html#a1">00387</a> CBufClient::~CBufClient()
00388 {
00389 <a class="code" href="buf__sock_8h.html#a0">nlnettrace</a>( <font class="stringliteral">"CBufClient::~CBufClient"</font> );
00390
00391 <font class="comment">// Disconnect if not done</font>
00392 <font class="keywordflow">if</font> ( <a class="code" href="classNLNET_1_1CBufClient.html#n0">_BufSock</a>->Sock->connected() )
00393 {
00394 <a class="code" href="debug_8h.html#a6">nlassert</a>( <a class="code" href="classNLNET_1_1CBufClient.html#n0">_BufSock</a>->connectedState() );
00395
00396 <a class="code" href="classNLNET_1_1CBufClient.html#a3">disconnect</a>( <font class="keyword">true</font> );
00397 }
00398
00399 <font class="comment">// Clean thread termination</font>
00400 <font class="keywordflow">if</font> ( <a class="code" href="classNLNET_1_1CBufClient.html#o1">_RecvThread</a> != NULL )
00401 {
00402 <a class="code" href="debug_8h.html#a0">nldebug</a>( <font class="stringliteral">"LNETL1: Waiting for the end of the receive thread..."</font> );
00403 <a class="code" href="classNLNET_1_1CBufClient.html#o1">_RecvThread</a>-><a class="code" href="classNLMISC_1_1IThread.html#a3">wait</a>();
00404 }
00405
00406 <font class="keywordflow">if</font> ( <a class="code" href="classNLNET_1_1CBufClient.html#o0">_RecvTask</a> != NULL )
00407 <font class="keyword">delete</font> <a class="code" href="classNLNET_1_1CBufClient.html#o0">_RecvTask</a>;
00408
00409 <font class="keywordflow">if</font> ( <a class="code" href="classNLNET_1_1CBufClient.html#o1">_RecvThread</a> != NULL )
00410 <font class="keyword">delete</font> <a class="code" href="classNLNET_1_1CBufClient.html#o1">_RecvThread</a>;
00411
00412 <font class="keywordflow">if</font> ( <a class="code" href="classNLNET_1_1CBufClient.html#n0">_BufSock</a> != NULL )
00413 <font class="keyword">delete</font> <a class="code" href="classNLNET_1_1CBufClient.html#n0">_BufSock</a>;
00414
00415 <a class="code" href="buf__sock_8h.html#a0">nlnettrace</a>( <font class="stringliteral">"Exiting CBufClient::~CBufClient"</font> );
00416 }
00417
00418
00419 <font class="comment">/***************************************************************************************************</font>
00420 <font class="comment"> * Receive thread </font>
00421 <font class="comment"> **************************************************************************************************/</font>
00422
00423
00424 <font class="comment">/*</font>
00425 <font class="comment"> * Code of receiving thread for clients</font>
00426 <font class="comment"> */</font>
<a name="l00427"></a><a class="code" href="classNLNET_1_1CClientReceiveTask.html#a1">00427</a> <font class="keywordtype">void</font> CClientReceiveTask::run()
00428 {
00429 <a class="code" href="buf__sock_8h.html#a0">nlnettrace</a>( <font class="stringliteral">"CClientReceiveTask::run"</font> );
00430
00431 <font class="keywordtype">bool</font> connected = <font class="keyword">true</font>;
00432 <font class="keywordflow">while</font> ( connected ) <font class="comment">// does not call _Sock->connected() to avoid mutex (not needed for client)</font>
00433 {
00434 <font class="keywordflow">try</font>
00435 {
00436 <font class="comment">// Receive message length (in blocking mode)</font>
00437 <a class="code" href="namespaceNLNET.html#a6">TBlockSize</a> blocklen;
00438 uint32 lenoflen = <font class="keyword">sizeof</font>(blocklen);
00439 <a class="code" href="classNLNET_1_1CClientReceiveTask.html#a2">sock</a>()->receive( (uint8*)&blocklen, lenoflen );
00440 uint32 len = ntohl( blocklen );
00441
00442 <font class="keywordflow">if</font> ( len != 0 )
00443 {
00444 <font class="comment">// Test size limit</font>
00445 <font class="keywordflow">if</font> ( len > <a class="code" href="classNLNET_1_1CClientReceiveTask.html#o0">_Client</a>->maxExpectedBlockSize() )
00446 {
00447 <a class="code" href="debug_8h.html#a2">nlwarning</a>( <font class="stringliteral">"LNETL1: Socket %s received length exceeding max expected, in block header... Disconnecting"</font>, <a class="code" href="classNLNET_1_1CClientReceiveTask.html#o1">_SockId</a>->asString().c_str() );
00448 <font class="keywordflow">throw</font> ESocket( <font class="stringliteral">"Received length exceeding max expected"</font>, <font class="keyword">false</font> );
00449 }
00450
00451 <font class="comment">// Receive message payload (in blocking mode)</font>
00452 CObjectVector<uint8> buffer;
00453 buffer.resize(len+1);
00454
00455 <a class="code" href="classNLNET_1_1CClientReceiveTask.html#a2">sock</a>()->receive( buffer.getPtr(), len );
00456
00457 <font class="comment">//commented out for optimisation: nldebug( "LNETL1: Client %s received buffer (%u bytes)", _SockId->asString().c_str(), buffer.size()/*, stringFromVector(buffer).c_str()*/ );</font>
00458 <font class="comment">// Add event type</font>
00459 buffer[len] = CBufNetBase::User;
00460
00461 <font class="comment">// Push message into receive queue</font>
00462 <a class="code" href="classNLNET_1_1CClientReceiveTask.html#o0">_Client</a>->pushMessageIntoReceiveQueue( buffer.getPtr(), buffer.size() );
00463 }
00464 <font class="keywordflow">else</font>
00465 {
00466 <a class="code" href="debug_8h.html#a2">nlwarning</a>( <font class="stringliteral">"LNETL1: Socket %s received null length in block header"</font>, <a class="code" href="classNLNET_1_1CClientReceiveTask.html#o1">_SockId</a>->asString().c_str() );
00467 }
00468 }
00469 <font class="keywordflow">catch</font> ( ESocketConnectionClosed& )
00470 {
00471 <a class="code" href="debug_8h.html#a0">nldebug</a>( <font class="stringliteral">"LNETL1: Client connection %s closed"</font>, <a class="code" href="classNLNET_1_1CClientReceiveTask.html#o1">_SockId</a>->asString().c_str() );
00472 <font class="comment">// The socket went to _Connected=false when throwing the exception</font>
00473 connected = <font class="keyword">false</font>;
00474 }
00475 <font class="keywordflow">catch</font> ( ESocket& )
00476 {
00477 <a class="code" href="debug_8h.html#a0">nldebug</a>( <font class="stringliteral">"LNETL1: Client connection %s broken"</font>, <a class="code" href="classNLNET_1_1CClientReceiveTask.html#o1">_SockId</a>->asString().c_str() );
00478 <a class="code" href="classNLNET_1_1CClientReceiveTask.html#a2">sock</a>()->disconnect();
00479 connected = <font class="keyword">false</font>;
00480 }
00481 }
00482
00483 <a class="code" href="buf__sock_8h.html#a0">nlnettrace</a>( <font class="stringliteral">"Exiting CClientReceiveTask::run()"</font> );
00484 }
00485
00486
00487 } <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>
|