aboutsummaryrefslogtreecommitdiff
path: root/docs/doxygen/nel/buf__sock_8cpp-source.html
blob: abd06f7fc2be6482dc7a5e82d7975a45ec3f5d59 (plain)
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
<!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>&nbsp;<A CLASS=uplinks HREF='/'><b>Home</B></FONT></A>&nbsp;&nbsp;&nbsp;</TD>
        <TD><IMG  width=6 height=14  SRC="/inc/img/reddots.gif" ALT="#" VSPACE=2 HSPACE=2 BORDER=0 ></TD><TD VALIGN=middle>&nbsp;<A CLASS=uplinks HREF=><b>nevrax.com</B></FONT></A>&nbsp;&nbsp;&nbsp;</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>
&nbsp;

<!-- 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>&nbsp;</td>
</tr></table>
<!-- Generated by Doxygen 1.2.14 -->
<center>
<a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="namespaces.html">Namespace List</a> &nbsp; <a class="qindex" href="hierarchy.html">Class Hierarchy</a> &nbsp; <a class="qindex" href="classes.html">Alphabetical List</a> &nbsp; <a class="qindex" href="annotated.html">Compound List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="namespacemembers.html">Namespace Members</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; <a class="qindex" href="globals.html">File Members</a> &nbsp; <a class="qindex" href="pages.html">Related Pages</a> &nbsp; <a class="qindexRef" doxygen="_cgi:/cgi-bin/nel-search.cgi" href="/cgi-bin/nel-search.cgi">Search</a> &nbsp; </center>
<hr><h1>buf_sock.cpp</h1><a href="buf__sock_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__sock_8h.html">nel/net/buf_sock.h</a>"</font>
00031 <font class="preprocessor">#include "<a class="code" href="buf__server_8h.html">nel/net/buf_server.h</a>"</font>
00032 
00033 <font class="preprocessor">#ifdef NL_OS_WINDOWS</font>
00034 <font class="preprocessor"></font><font class="preprocessor">#include &lt;winsock2.h&gt;</font>
00035 <font class="preprocessor">#elif defined NL_OS_UNIX</font>
00036 <font class="preprocessor"></font><font class="preprocessor">#include &lt;netinet/in.h&gt;</font>
00037 <font class="preprocessor">#endif</font>
00038 <font class="preprocessor"></font>
00039 <font class="keyword">using</font> <font class="keyword">namespace </font>NLMISC;
00040 <font class="keyword">using</font> <font class="keyword">namespace </font>std;
00041 
00042 
00043 <font class="keyword">namespace </font>NLNET {
00044 
00045 
00046 NLMISC::CMutex <a class="code" href="namespaceNLNET.html#a87">nettrace_mutex</a>(<font class="stringliteral">"nettrace_mutex"</font>);
00047 
00048 
00049 <font class="comment">/*</font>
00050 <font class="comment"> * Constructor</font>
00051 <font class="comment"> */</font>
<a name="l00052"></a><a class="code" href="classNLNET_1_1CBufSock.html#b0">00052</a> CBufSock::CBufSock( CTcpSock *sock ) :
00053         SendNextValue(0),
00054         ReceiveNextValue(0),
00055         Sock( sock ),
00056         _KnowConnected( false ),
00057         _LastFlushTime( 0 ),
00058         _TriggerTime( 20 ),
00059         _TriggerSize( -1 ),
00060         _RTSBIndex( 0 ),
00061         _AppId( 0 ),
00062         _ConnectedState( false )
00063 {
00064         <a class="code" href="buf__sock_8h.html#a0">nlnettrace</a>( <font class="stringliteral">"CBufSock::CBufSock"</font> ); <font class="comment">// don't define a global object</font>
00065 
00066         <font class="keywordflow">if</font> ( <a class="code" href="classNLNET_1_1CBufSock.html#n1">Sock</a> == NULL )
00067           {
00068                 <a class="code" href="classNLNET_1_1CBufSock.html#n1">Sock</a> = <font class="keyword">new</font> CTcpSock();
00069           }
00070 
00071 <font class="preprocessor">#ifdef NL_DEBUG</font>
00072 <font class="preprocessor"></font>        _FlushTrigger = FTManual;
00073 <font class="preprocessor">#endif</font>
00074 <font class="preprocessor"></font>        <a class="code" href="classNLNET_1_1CBufSock.html#o0">_LastFlushTime</a> = CTime::getLocalTime();
00075 }
00076 
00077 
00078 <font class="comment">/*</font>
00079 <font class="comment"> * Destructor</font>
00080 <font class="comment"> */</font>
<a name="l00081"></a><a class="code" href="classNLNET_1_1CBufSock.html#a0">00081</a> CBufSock::~CBufSock()
00082 {
00083         <a class="code" href="debug_8h.html#a6">nlassert</a> (<font class="keyword">this</font> != InvalidSockId);       <font class="comment">// invalid bufsock</font>
00084 
00085         <a class="code" href="buf__sock_8h.html#a0">nlnettrace</a>( <font class="stringliteral">"CBufSock::~CBufSock"</font> );
00086         
00087         <font class="keyword">delete</font> <a class="code" href="classNLNET_1_1CBufSock.html#n1">Sock</a>; <font class="comment">// the socket disconnects automatically if needed</font>
00088         
00089         <font class="comment">// destroy the structur to be sure that other people will not access to this anymore</font>
00090         <a class="code" href="classNLNET_1_1CBufSock.html#m0">AuthorizedCallback</a> = <font class="stringliteral">""</font>;
00091         <a class="code" href="classNLNET_1_1CBufSock.html#n1">Sock</a> = NULL;
00092         <a class="code" href="classNLNET_1_1CBufSock.html#n2">_KnowConnected</a> = <font class="keyword">false</font>;
00093         <a class="code" href="classNLNET_1_1CBufSock.html#o0">_LastFlushTime</a> = 0;
00094         <a class="code" href="classNLNET_1_1CBufSock.html#o1">_TriggerTime</a> = 0;
00095         <a class="code" href="classNLNET_1_1CBufSock.html#o2">_TriggerSize</a> = 0;
00096         <a class="code" href="classNLNET_1_1CBufSock.html#o3">_ReadyToSendBuffer</a>.<a class="code" href="classNLMISC_1_1CObjectVector.html#z301_0">clear</a> ();
00097         <a class="code" href="classNLNET_1_1CBufSock.html#o4">_RTSBIndex</a> = 0;
00098         <a class="code" href="classNLNET_1_1CBufSock.html#o5">_AppId</a> = 0;
00099         <a class="code" href="classNLNET_1_1CBufSock.html#o6">_ConnectedState</a> = <font class="keyword">false</font>;
00100 }
00101 
00102 
00103 <font class="comment">/*</font>
00104 <font class="comment"> * Returns a readable string from a vector of bytes, beginning from pos, limited to 'len' characters. '\0' are replaced by ' '</font>
00105 <font class="comment"> */</font>
00106 string <a class="code" href="namespaceNLNET.html#a88">stringFromVectorPart</a>( <font class="keyword">const</font> vector&lt;uint8&gt;&amp; <a class="code" href="driver__opengl__extension__def_8h.html#a368">v</a>, uint32 pos, uint32 len )
00107 {
00108         <a class="code" href="debug_8h.html#a8">nlassertex</a>( pos+len &lt;= <a class="code" href="driver__opengl__extension__def_8h.html#a368">v</a>.size(), (<font class="stringliteral">"pos=%u len=%u size=%u"</font>, pos, len, <a class="code" href="driver__opengl__extension__def_8h.html#a368">v</a>.size()) );
00109 
00110         string <a class="code" href="driver__opengl__extension__def_8h.html#a383">s</a>;
00111         <font class="keywordflow">if</font> ( (! <a class="code" href="driver__opengl__extension__def_8h.html#a368">v</a>.empty()) &amp;&amp; (len!=0) )
00112         {
00113                 <font class="comment">// Copy contents</font>
00114                 <a class="code" href="driver__opengl__extension__def_8h.html#a383">s</a>.resize( len );
00115                 memcpy( &amp;*<a class="code" href="driver__opengl__extension__def_8h.html#a383">s</a>.begin(), &amp;*<a class="code" href="driver__opengl__extension__def_8h.html#a368">v</a>.begin()+pos, len );
00116 
00117                 <font class="comment">// Replace '\0' characters</font>
00118                 string::iterator is;
00119                 <font class="keywordflow">for</font> ( is=<a class="code" href="driver__opengl__extension__def_8h.html#a383">s</a>.begin(); is!=<a class="code" href="driver__opengl__extension__def_8h.html#a383">s</a>.end(); ++is )
00120                 {
00121                         <font class="keywordflow">if</font> ( ! isprint((uint8)(*is)) || (*is) == <font class="charliteral">'%'</font> )
00122                         {
00123                                 (*is) = <font class="charliteral">'?'</font>;
00124                         }
00125                 }
00126         }
00127 
00128         <font class="keywordflow">return</font> <a class="code" href="driver__opengl__extension__def_8h.html#a383">s</a>;
00129 }
00130 
00131 
00132 <font class="comment">/*</font>
00133 <font class="comment"> * Force to send all data pending in the send queue</font>
00134 <font class="comment"> * Note: this method is used by clients (blocking sockets) and servers (non-blocking sockets)</font>
00135 <font class="comment"> * so it works in both cases.</font>
00136 <font class="comment"> * Precond: the send queue should not contain an empty block</font>
00137 <font class="comment"> */</font>
<a name="l00138"></a><a class="code" href="classNLNET_1_1CBufSock.html#z347_3">00138</a> <font class="keywordtype">bool</font> CBufSock::flush()
00139 {
00140         <a class="code" href="debug_8h.html#a6">nlassert</a> (<font class="keyword">this</font> != InvalidSockId);       <font class="comment">// invalid bufsock</font>
00141         <font class="comment">//nlnettrace( "CBufSock::flush" );</font>
00142 
00143         <font class="comment">// Copy data from the send queue to _ReadyToSendBuffer</font>
00144         <a class="code" href="namespaceNLNET.html#a6">TBlockSize</a> netlen;
00145 <font class="comment">//      vector&lt;uint8&gt; tmpbuffer;</font>
00146 
00147         <font class="comment">// Process each element in the send queue</font>
00148         <font class="keywordflow">while</font> ( ! <a class="code" href="classNLNET_1_1CBufSock.html#n0">SendFifo</a>.<a class="code" href="classNLMISC_1_1CBufFIFO.html#a12">empty</a>() )
00149         {
00150                 uint8 *tmpbuffer;
00151                 uint32 size;
00152                 <a class="code" href="classNLNET_1_1CBufSock.html#n0">SendFifo</a>.<a class="code" href="classNLMISC_1_1CBufFIFO.html#a6">front</a>( tmpbuffer, size );
00153 
00154                 <font class="comment">// Compute the size and add it into the beginning of the buffer</font>
00155                 netlen = htonl( (<a class="code" href="namespaceNLNET.html#a6">TBlockSize</a>)size );
00156                 uint32 oldBufferSize = <a class="code" href="classNLNET_1_1CBufSock.html#o3">_ReadyToSendBuffer</a>.<a class="code" href="classNLMISC_1_1CObjectVector.html#z302_0">size</a>();
00157                 <a class="code" href="classNLNET_1_1CBufSock.html#o3">_ReadyToSendBuffer</a>.<a class="code" href="classNLMISC_1_1CObjectVector.html#z301_1">resize</a> (oldBufferSize+<font class="keyword">sizeof</font>(<a class="code" href="namespaceNLNET.html#a6">TBlockSize</a>)+size);
00158                 *(<a class="code" href="namespaceNLNET.html#a6">TBlockSize</a>*)&amp;(<a class="code" href="classNLNET_1_1CBufSock.html#o3">_ReadyToSendBuffer</a>[oldBufferSize])=netlen;
00159 
00160                 <font class="comment">// Append the temporary buffer to the global buffer</font>
00161                 CFastMem::memcpy (&amp;<a class="code" href="classNLNET_1_1CBufSock.html#o3">_ReadyToSendBuffer</a>[oldBufferSize+<font class="keyword">sizeof</font>(<a class="code" href="namespaceNLNET.html#a6">TBlockSize</a>)], tmpbuffer, size);
00162                 <a class="code" href="classNLNET_1_1CBufSock.html#n0">SendFifo</a>.<a class="code" href="classNLMISC_1_1CBufFIFO.html#a10">pop</a>();
00163         }
00164 
00165         <font class="comment">// Actual sending of _ReadyToSendBuffer</font>
00166         <font class="comment">//if ( ! _ReadyToSendBuffer.empty() )</font>
00167         <font class="keywordflow">if</font> ( <a class="code" href="classNLNET_1_1CBufSock.html#o3">_ReadyToSendBuffer</a>.<a class="code" href="classNLMISC_1_1CObjectVector.html#z302_0">size</a>() != 0 )
00168         {               
00169                 <font class="comment">// Send</font>
00170                 CSock::TSockResult <a class="code" href="driver__opengl__extension__def_8h.html#a400">res</a>;
00171                 <a class="code" href="namespaceNLNET.html#a6">TBlockSize</a> len = <a class="code" href="classNLNET_1_1CBufSock.html#o3">_ReadyToSendBuffer</a>.<a class="code" href="classNLMISC_1_1CObjectVector.html#z302_0">size</a>() - <a class="code" href="classNLNET_1_1CBufSock.html#o4">_RTSBIndex</a>;
00172 
00173                 <a class="code" href="driver__opengl__extension__def_8h.html#a400">res</a> = <a class="code" href="classNLNET_1_1CBufSock.html#n1">Sock</a>-&gt;send( <a class="code" href="classNLNET_1_1CBufSock.html#o3">_ReadyToSendBuffer</a>.<a class="code" href="classNLMISC_1_1CObjectVector.html#z302_2">getPtr</a>()+<a class="code" href="classNLNET_1_1CBufSock.html#o4">_RTSBIndex</a>, len, <font class="keyword">false</font> );
00174 
00175                 <font class="keywordflow">if</font> ( <a class="code" href="driver__opengl__extension__def_8h.html#a400">res</a> == CSock::Ok )
00176                 {
00177 <font class="comment">/*                      // Debug display</font>
00178 <font class="comment">                        switch ( _FlushTrigger )</font>
00179 <font class="comment">                        {</font>
00180 <font class="comment">                        case FTTime : nldebug( "LNETL1: Time triggered flush for %s:", asString().c_str() ); break;</font>
00181 <font class="comment">                        case FTSize : nldebug( "LNETL1: Size triggered flush for %s:", asString().c_str() ); break;</font>
00182 <font class="comment">                        default:          nldebug( "LNETL1: Manual flush for %s:", asString().c_str() );</font>
00183 <font class="comment">                        }</font>
00184 <font class="comment">                        _FlushTrigger = FTManual;</font>
00185 <font class="comment">                        nldebug( "LNETL1: %s sent effectively a buffer (%d on %d B)", asString().c_str(), len, _ReadyToSendBuffer.size() );</font>
00186 <font class="comment">*/</font>                      
00187                         
00188                         <font class="comment">// TODO OPTIM remove the nldebug for speed</font>
00189                         <font class="comment">//commented for optimisation nldebug( "LNETL1: %s sent effectively %u/%u bytes (pos %u wantedsend %u)", asString().c_str(), len, _ReadyToSendBuffer.size(), _RTSBIndex, realLen/*, stringFromVectorPart(_ReadyToSendBuffer,_RTSBIndex,len).c_str()*/ );</font>
00190 
00191                         <font class="keywordflow">if</font> ( <a class="code" href="classNLNET_1_1CBufSock.html#o4">_RTSBIndex</a>+len == <a class="code" href="classNLNET_1_1CBufSock.html#o3">_ReadyToSendBuffer</a>.<a class="code" href="classNLMISC_1_1CObjectVector.html#z302_0">size</a>() ) <font class="comment">// for non-blocking mode (server)</font>
00192                         {
00193                                 <font class="comment">// If sending is ok, clear the global buffer</font>
00194                                 <a class="code" href="classNLNET_1_1CBufSock.html#o3">_ReadyToSendBuffer</a>.<a class="code" href="classNLMISC_1_1CObjectVector.html#z301_0">clear</a>();
00195                                 <a class="code" href="classNLNET_1_1CBufSock.html#o4">_RTSBIndex</a> = 0;
00196                         }
00197                         <font class="keywordflow">else</font>
00198                         {
00199                                 <font class="comment">// Or clear only the data that was actually sent</font>
00200                                 <a class="code" href="debug_8h.html#a8">nlassertex</a>( <a class="code" href="classNLNET_1_1CBufSock.html#o4">_RTSBIndex</a>+len &lt; <a class="code" href="classNLNET_1_1CBufSock.html#o3">_ReadyToSendBuffer</a>.<a class="code" href="classNLMISC_1_1CObjectVector.html#z302_0">size</a>(), (<font class="stringliteral">"index=%u len=%u size=%u"</font>, <a class="code" href="classNLNET_1_1CBufSock.html#o4">_RTSBIndex</a>, len, <a class="code" href="classNLNET_1_1CBufSock.html#o3">_ReadyToSendBuffer</a>.<a class="code" href="classNLMISC_1_1CObjectVector.html#z302_0">size</a>()) );
00201                                 <a class="code" href="classNLNET_1_1CBufSock.html#o4">_RTSBIndex</a> += len;
00202                                 <font class="keywordflow">if</font> ( <a class="code" href="classNLNET_1_1CBufSock.html#o3">_ReadyToSendBuffer</a>.<a class="code" href="classNLMISC_1_1CObjectVector.html#z302_0">size</a>() &gt; 20480 ) <font class="comment">// if big, clear data already sent</font>
00203                                 {
00204                                         uint nbcpy = <a class="code" href="classNLNET_1_1CBufSock.html#o3">_ReadyToSendBuffer</a>.<a class="code" href="classNLMISC_1_1CObjectVector.html#z302_0">size</a>() - len;
00205                                         <font class="keywordflow">for</font> (uint i = 0; i &lt; nbcpy; i++)
00206                                         {
00207                                                 <a class="code" href="classNLNET_1_1CBufSock.html#o3">_ReadyToSendBuffer</a>[i] = <a class="code" href="classNLNET_1_1CBufSock.html#o3">_ReadyToSendBuffer</a>[i+len];
00208                                         }
00209                                         <a class="code" href="classNLNET_1_1CBufSock.html#o3">_ReadyToSendBuffer</a>.<a class="code" href="classNLMISC_1_1CObjectVector.html#z301_1">resize</a>(nbcpy);
00210                                         <font class="comment">//_ReadyToSendBuffer.erase( _ReadyToSendBuffer.begin(), _ReadyToSendBuffer.begin()+len );</font>
00211                                         <a class="code" href="classNLNET_1_1CBufSock.html#o4">_RTSBIndex</a> = 0;
00212                                 }
00213                         }
00214                 }
00215                 <font class="keywordflow">else</font>
00216                 {
00217 <font class="preprocessor">#ifdef NL_DEBUG</font>
00218 <font class="preprocessor"></font>                        <font class="comment">// can happen in a normal behavior if, for example, the other side is not connect anymore</font>
00219                         <a class="code" href="debug_8h.html#a0">nldebug</a>( <font class="stringliteral">"LNETL1: %s failed to send effectively a buffer of %d bytes"</font>, <a class="code" href="classNLNET_1_1CBufSock.html#a3">asString</a>().c_str(), <a class="code" href="classNLNET_1_1CBufSock.html#o3">_ReadyToSendBuffer</a>.<a class="code" href="classNLMISC_1_1CObjectVector.html#z302_0">size</a>() );
00220 <font class="preprocessor">#endif</font>
00221 <font class="preprocessor"></font>                        <font class="keywordflow">return</font> <font class="keyword">false</font>;
00222                 }
00223         }
00224 
00225         <font class="keywordflow">return</font> <font class="keyword">true</font>;
00226 }
00227 
00228 
00229 <font class="comment">/* Sets the time flush trigger (in millisecond). When this time is elapsed,</font>
00230 <font class="comment"> * all data in the send queue is automatically sent (-1 to disable this trigger)</font>
00231 <font class="comment"> */</font>
<a name="l00232"></a><a class="code" href="classNLNET_1_1CBufSock.html#z347_1">00232</a> <font class="keywordtype">void</font> CBufSock::setTimeFlushTrigger( sint32 ms )
00233 {
00234         <a class="code" href="debug_8h.html#a6">nlassert</a> (<font class="keyword">this</font> != InvalidSockId);       <font class="comment">// invalid bufsock</font>
00235         <a class="code" href="classNLNET_1_1CBufSock.html#o1">_TriggerTime</a> = ms;
00236         <a class="code" href="classNLNET_1_1CBufSock.html#o0">_LastFlushTime</a> = CTime::getLocalTime();
00237 }
00238 
00239 
00240 <font class="comment">/*</font>
00241 <font class="comment"> * Update the network sending (call this method evenly). Returns false if an error occured.</font>
00242 <font class="comment"> */</font>
<a name="l00243"></a><a class="code" href="classNLNET_1_1CBufSock.html#z347_0">00243</a> <font class="keywordtype">bool</font> CBufSock::update()
00244 {
00245         <a class="code" href="debug_8h.html#a6">nlassert</a> (<font class="keyword">this</font> != InvalidSockId);       <font class="comment">// invalid bufsock</font>
00246 <font class="comment">//      nlnettrace( "CBufSock::update-BEGIN" );</font>
00247         <font class="comment">// Time trigger</font>
00248 
00249         <font class="keywordflow">if</font> ( <a class="code" href="classNLNET_1_1CBufSock.html#o1">_TriggerTime</a> != -1 )
00250         {
00251                 <a class="code" href="namespaceNLMISC.html#a183">TTime</a> now = CTime::getLocalTime();
00252                 <font class="keywordflow">if</font> ( (sint32)(now-<a class="code" href="classNLNET_1_1CBufSock.html#o0">_LastFlushTime</a>) &gt;= <a class="code" href="classNLNET_1_1CBufSock.html#o1">_TriggerTime</a> )
00253                 {
00254 <font class="preprocessor">#ifdef NL_DEBUG</font>
00255 <font class="preprocessor"></font>                        _FlushTrigger = FTTime;
00256 <font class="preprocessor">#endif</font>
00257 <font class="preprocessor"></font>                        <font class="keywordflow">if</font> ( <a class="code" href="classNLNET_1_1CBufSock.html#z347_3">flush</a>() )
00258                         {
00259                                 <a class="code" href="classNLNET_1_1CBufSock.html#o0">_LastFlushTime</a> = now;
00260 <font class="comment">//                              nlnettrace ( "CBufSock::update-END time 1" );</font>
00261                                 <font class="keywordflow">return</font> <font class="keyword">true</font>;
00262                         }
00263                         <font class="keywordflow">else</font>
00264                         {
00265 <font class="comment">//                              nlnettrace ( "CBufSock::update-END time 0" );</font>
00266                                 <font class="keywordflow">return</font> <font class="keyword">false</font>;
00267                         }
00268                 }
00269         }
00270         <font class="comment">// Size trigger</font>
00271         <font class="keywordflow">if</font> ( <a class="code" href="classNLNET_1_1CBufSock.html#o2">_TriggerSize</a> != -1 )
00272         {
00273                 <font class="keywordflow">if</font> ( (sint32)<a class="code" href="classNLNET_1_1CBufSock.html#n0">SendFifo</a>.<a class="code" href="classNLMISC_1_1CBufFIFO.html#a14">size</a>() &gt; <a class="code" href="classNLNET_1_1CBufSock.html#o2">_TriggerSize</a> )
00274                 {
00275 <font class="preprocessor">#ifdef NL_DEBUG</font>
00276 <font class="preprocessor"></font>                        _FlushTrigger = FTSize;
00277 <font class="preprocessor">#endif</font>
00278 <font class="preprocessor"></font><font class="comment">//                      nlnettrace( "CBufSock::update-END size" );</font>
00279                         <font class="keywordflow">return</font> <a class="code" href="classNLNET_1_1CBufSock.html#z347_3">flush</a>();
00280                 }
00281         }
00282 <font class="comment">//      nlnettrace( "CBufSock::update-END nosend" );</font>
00283         <font class="keywordflow">return</font> <font class="keyword">true</font>;
00284 }
00285 
00286 
00287 <font class="comment">/*</font>
00288 <font class="comment"> * Connects to the specified addr; set connectedstate to true if no connection advertising is needed</font>
00289 <font class="comment"> * Precond: not connected</font>
00290 <font class="comment"> */</font>
<a name="l00291"></a><a class="code" href="classNLNET_1_1CBufSock.html#b5">00291</a> <font class="keywordtype">void</font> CBufSock::connect( <font class="keyword">const</font> CInetAddress&amp; <a class="code" href="driver__opengl__extension__def_8h.html#a414">addr</a>, <font class="keywordtype">bool</font> nodelay, <font class="keywordtype">bool</font> connectedstate )
00292 {
00293         <a class="code" href="debug_8h.html#a6">nlassert</a> (<font class="keyword">this</font> != InvalidSockId);       <font class="comment">// invalid bufsock</font>
00294         <a class="code" href="debug_8h.html#a6">nlassert</a>( ! <a class="code" href="classNLNET_1_1CBufSock.html#n1">Sock</a>-&gt;connected() );
00295 
00296         <a class="code" href="classNLNET_1_1CBufSock.html#m1">SendNextValue</a> = <a class="code" href="classNLNET_1_1CBufSock.html#m2">ReceiveNextValue</a> = 0;
00297         
00298         <a class="code" href="classNLNET_1_1CBufSock.html#n1">Sock</a>-&gt;connect( <a class="code" href="driver__opengl__extension__def_8h.html#a414">addr</a> );
00299         <a class="code" href="classNLNET_1_1CBufSock.html#o6">_ConnectedState</a> = connectedstate;
00300         <a class="code" href="classNLNET_1_1CBufSock.html#n2">_KnowConnected</a> = connectedstate;
00301         <font class="keywordflow">if</font> ( nodelay )
00302         {
00303                 <a class="code" href="classNLNET_1_1CBufSock.html#n1">Sock</a>-&gt;setNoDelay( <font class="keyword">true</font> );
00304         }
00305 }
00306 
00307 
00308 <font class="comment">/*</font>
00309 <font class="comment"> * Disconnects; set connectedstate to false if no disconnection advertising is needed</font>
00310 <font class="comment"> */</font>
<a name="l00311"></a><a class="code" href="classNLNET_1_1CBufSock.html#b6">00311</a> <font class="keywordtype">void</font> CBufSock::disconnect( <font class="keywordtype">bool</font> connectedstate )
00312 {
00313         <a class="code" href="debug_8h.html#a6">nlassert</a> (<font class="keyword">this</font> != InvalidSockId);       <font class="comment">// invalid bufsock</font>
00314         <a class="code" href="classNLNET_1_1CBufSock.html#n1">Sock</a>-&gt;disconnect();
00315         <a class="code" href="classNLNET_1_1CBufSock.html#o6">_ConnectedState</a> = connectedstate;
00316         <a class="code" href="classNLNET_1_1CBufSock.html#n2">_KnowConnected</a> = connectedstate;
00317 
00318         <a class="code" href="classNLNET_1_1CBufSock.html#m1">SendNextValue</a> = <a class="code" href="classNLNET_1_1CBufSock.html#m2">ReceiveNextValue</a> = 0;
00319 }
00320 
00321 
00322 <font class="comment">/*</font>
00323 <font class="comment"> * Returns a string with the characteristics of the object</font>
00324 <font class="comment"> */</font>
<a name="l00325"></a><a class="code" href="classNLNET_1_1CBufSock.html#a3">00325</a> string CBufSock::asString()<font class="keyword"> const</font>
00326 <font class="keyword"></font>{
00327         stringstream ss;
00328         <font class="keywordflow">if</font> (<font class="keyword">this</font> == InvalidSockId) <font class="comment">// tricky</font>
00329                 ss &lt;&lt; <font class="stringliteral">"&lt;Null&gt;"</font>;
00330         <font class="keywordflow">else</font>
00331         {
00332                 <font class="comment">// if it crashs here, it means that the CBufSock was deleted and you try to access to the virtual table that is empty</font>
00333                 <font class="comment">// because the object is destroyed.</font>
00334                 ss &lt;&lt; <a class="code" href="classNLNET_1_1CBufSock.html#b1">typeStr</a>();
00335                 ss &lt;&lt; hex &lt;&lt; <font class="keyword">this</font> &lt;&lt; dec &lt;&lt; <font class="stringliteral">" (socket "</font>;
00336                 
00337                 <font class="keywordflow">if</font> (<a class="code" href="classNLNET_1_1CBufSock.html#n1">Sock</a> == NULL)
00338                         ss &lt;&lt; <font class="stringliteral">"&lt;Null&gt;"</font>;
00339                 <font class="keywordflow">else</font>
00340                         ss &lt;&lt; <a class="code" href="classNLNET_1_1CBufSock.html#n1">Sock</a>-&gt;descriptor();
00341 
00342                 ss &lt;&lt; <font class="stringliteral">")"</font>;
00343         }
00344         <font class="keywordflow">return</font> ss.str();
00345 }
00346 
00347 
00348 <font class="comment">/*</font>
00349 <font class="comment"> * Constructor with an existing socket (created by an accept())</font>
00350 <font class="comment"> */</font>
<a name="l00351"></a><a class="code" href="classNLNET_1_1CServerBufSock.html#b0">00351</a> CServerBufSock::CServerBufSock( CTcpSock *sock ) :
00352         CBufSock( sock ),
00353         _Advertised( false ),
00354         _NowReadingBuffer( false ),
00355         _BytesRead( 0 ),
00356         _Length( 0 ),
00357         _OwnerTask( NULL )
00358 {
00359         <a class="code" href="debug_8h.html#a6">nlassert</a> (<font class="keyword">this</font> != InvalidSockId);       <font class="comment">// invalid bufsock</font>
00360         <a class="code" href="buf__sock_8h.html#a0">nlnettrace</a>( <font class="stringliteral">"CServerBufSock::CServerBufSock"</font> );
00361 }
00362 
00363 
00364 <font class="comment">// In Receive Threads:</font>
00365 
00366 
00367 <font class="comment">/*</font>
00368 <font class="comment"> * Receives a part of a message (nonblocking socket only)</font>
00369 <font class="comment"> */</font>
<a name="l00370"></a><a class="code" href="classNLNET_1_1CServerBufSock.html#b3">00370</a> <font class="keywordtype">bool</font> CServerBufSock::receivePart()
00371 {
00372         <a class="code" href="debug_8h.html#a6">nlassert</a> (<font class="keyword">this</font> != InvalidSockId);       <font class="comment">// invalid bufsock</font>
00373         <a class="code" href="buf__sock_8h.html#a0">nlnettrace</a>( <font class="stringliteral">"CServerBufSock::receivePart"</font> );
00374 
00375         <a class="code" href="namespaceNLNET.html#a6">TBlockSize</a> actuallen;
00376         <font class="keywordflow">if</font> ( ! <a class="code" href="classNLNET_1_1CServerBufSock.html#o1">_NowReadingBuffer</a> )
00377         {
00378                 <font class="comment">// Receiving length prefix</font>
00379                 actuallen = <font class="keyword">sizeof</font>(_Length)-<a class="code" href="classNLNET_1_1CServerBufSock.html#o2">_BytesRead</a>;
00380                 <a class="code" href="classNLNET_1_1CBufSock.html#n1">Sock</a>-&gt;receive( (uint8*)(&amp;<a class="code" href="classNLNET_1_1CServerBufSock.html#o3">_Length</a>)+<a class="code" href="classNLNET_1_1CServerBufSock.html#o2">_BytesRead</a>, actuallen );
00381                 <a class="code" href="classNLNET_1_1CServerBufSock.html#o2">_BytesRead</a> += actuallen;
00382                 <font class="keywordflow">if</font> ( <a class="code" href="classNLNET_1_1CServerBufSock.html#o2">_BytesRead</a> == <font class="keyword">sizeof</font>(_Length ) )
00383                 {
00384                         <font class="keywordflow">if</font> ( <a class="code" href="classNLNET_1_1CServerBufSock.html#o3">_Length</a> != 0 )
00385                         {
00386                                 <a class="code" href="classNLNET_1_1CServerBufSock.html#o3">_Length</a> = ntohl( <a class="code" href="classNLNET_1_1CServerBufSock.html#o3">_Length</a> );
00387 
00388                                 <font class="comment">// Test size limit</font>
00389                                 <font class="keywordflow">if</font> ( <a class="code" href="classNLNET_1_1CServerBufSock.html#o3">_Length</a> &gt; <a class="code" href="classNLNET_1_1CServerBufSock.html#o5">_OwnerTask</a>-&gt;server()-&gt;maxExpectedBlockSize() )
00390                                 {
00391                                         <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_1CBufSock.html#a3">asString</a>().c_str() );
00392                                         <font class="keywordflow">throw</font> ESocket( <font class="stringliteral">"Received length exceeding max expected"</font>, <font class="keyword">false</font> );
00393                                 }
00394 
00395                                 <a class="code" href="classNLNET_1_1CServerBufSock.html#o1">_NowReadingBuffer</a> = <font class="keyword">true</font>;
00396                                 <a class="code" href="classNLNET_1_1CServerBufSock.html#o4">_ReceiveBuffer</a>.resize( <a class="code" href="classNLNET_1_1CServerBufSock.html#o3">_Length</a> );
00397                         }
00398                         <font class="keywordflow">else</font>
00399                         {
00400                                 <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_1CBufSock.html#a3">asString</a>().c_str() );
00401                         }
00402                         <a class="code" href="classNLNET_1_1CServerBufSock.html#o2">_BytesRead</a> = 0;
00403                 }
00404         }
00405 
00406         <font class="keywordflow">if</font> ( _NowReadingBuffer )
00407         {
00408                 <font class="comment">// Receiving payload buffer</font>
00409                 actuallen = <a class="code" href="classNLNET_1_1CServerBufSock.html#o3">_Length</a>-<a class="code" href="classNLNET_1_1CServerBufSock.html#o2">_BytesRead</a>;
00410                 <a class="code" href="classNLNET_1_1CBufSock.html#n1">Sock</a>-&gt;receive( &amp;*<a class="code" href="classNLNET_1_1CServerBufSock.html#o4">_ReceiveBuffer</a>.begin()+<a class="code" href="classNLNET_1_1CServerBufSock.html#o2">_BytesRead</a>, actuallen );
00411                 <a class="code" href="classNLNET_1_1CServerBufSock.html#o2">_BytesRead</a> += actuallen;
00412 
00413                 <font class="keywordflow">if</font> ( <a class="code" href="classNLNET_1_1CServerBufSock.html#o2">_BytesRead</a> == <a class="code" href="classNLNET_1_1CServerBufSock.html#o3">_Length</a> )
00414                 {
00415 <font class="preprocessor">#ifdef NL_DEBUG</font>
00416 <font class="preprocessor"></font>                        <a class="code" href="debug_8h.html#a0">nldebug</a>( <font class="stringliteral">"LNETL1: %s received buffer (%u bytes): [%s]"</font>, <a class="code" href="classNLNET_1_1CBufSock.html#a3">asString</a>().c_str(), <a class="code" href="classNLNET_1_1CServerBufSock.html#o4">_ReceiveBuffer</a>.size(), stringFromVector(_ReceiveBuffer).c_str() );
00417 <font class="preprocessor">#endif</font>
00418 <font class="preprocessor"></font>                        <a class="code" href="classNLNET_1_1CServerBufSock.html#o1">_NowReadingBuffer</a> = <font class="keyword">false</font>;
00419                         <a class="code" href="classNLNET_1_1CServerBufSock.html#o2">_BytesRead</a> = 0;
00420                         <font class="keywordflow">return</font> <font class="keyword">true</font>;
00421                 }
00422         }
00423 
00424         <font class="keywordflow">return</font> <font class="keyword">false</font>;
00425 }
00426 
00427 } <font class="comment">// NLNET</font>
</pre></div>

<!-- footer -->
<BR><FONT Size=+5>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </FONT>
</TD>
<TD WIDTH=15><IMG  SRC=/inc/img/pixel.gif WIDTH=15 HEIGHT=15 BORDER=0 ALT=""></TD>
</TR>
</TABLE>
</BODY>
</HTML>