… | |
… | |
33 | */ |
33 | */ |
34 | |
34 | |
35 | using namespace std; |
35 | using namespace std; |
36 | |
36 | |
37 | #include <global.h> |
37 | #include <global.h> |
38 | #include <newclient.h> |
|
|
39 | #include <sproto.h> |
38 | #include <sproto.h> |
40 | #include <cstdarg> |
39 | #include <cstdarg> |
41 | |
40 | |
42 | #ifdef __linux__ |
41 | #ifdef __linux__ |
43 | # include <sys/types.h> |
42 | # include <sys/types.h> |
44 | # include <sys/socket.h> |
43 | # include <sys/socket.h> |
45 | # include <netinet/in.h> |
44 | # include <netinet/in.h> |
46 | # define TCP_HZ 1000 // sorry... |
|
|
47 | # include <netinet/tcp.h> |
45 | # include <netinet/tcp.h> |
48 | #endif |
46 | #endif |
49 | |
47 | |
50 | // use a really low timeout, as it doesn't cost any bandwidth, and you can |
48 | // use a really low timeout, as it doesn't cost any bandwidth, and you can |
51 | // easily die in 20 seconds... |
49 | // easily die in 20 seconds... |
52 | #define SOCKET_TIMEOUT1 10 |
50 | #define SOCKET_TIMEOUT1 10 * 1000 |
53 | #define SOCKET_TIMEOUT2 20 |
51 | #define SOCKET_TIMEOUT2 20 * 1000 |
54 | |
52 | |
55 | void |
53 | void |
56 | client_socket::flush () |
54 | client::flush () |
57 | { |
55 | { |
58 | #ifdef __linux__ |
56 | #ifdef __linux__ |
59 | // check time of last ack, and, if too old, kill connection |
57 | // check time of last ack, and, if too old, kill connection |
60 | struct tcp_info tcpi; |
58 | struct tcp_info tcpi; |
61 | socklen_t len = sizeof (tcpi); |
59 | socklen_t len = sizeof (tcpi); |
… | |
… | |
65 | unsigned int diff = tcpi.tcpi_last_ack_recv - tcpi.tcpi_last_data_sent; |
63 | unsigned int diff = tcpi.tcpi_last_ack_recv - tcpi.tcpi_last_data_sent; |
66 | |
64 | |
67 | rtt = tcpi.tcpi_rtt; |
65 | rtt = tcpi.tcpi_rtt; |
68 | rttvar = tcpi.tcpi_rttvar; |
66 | rttvar = tcpi.tcpi_rttvar; |
69 | |
67 | |
70 | if (tcpi.tcpi_unacked && SOCKET_TIMEOUT1 * TCP_HZ < diff && diff < 0x80000000UL // ack delayed for 20s |
68 | if (tcpi.tcpi_unacked && SOCKET_TIMEOUT1 < diff && diff < 0x80000000UL // ack delayed for 20s |
71 | && SOCKET_TIMEOUT2 * TCP_HZ < tcpi.tcpi_last_data_sent) // no data sent for 10s |
69 | && SOCKET_TIMEOUT2 < tcpi.tcpi_last_data_sent) // no data sent for 10s |
72 | { |
70 | { |
73 | LOG (llevDebug, "Connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd, |
71 | LOG (llevDebug, "Connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd, |
74 | (unsigned) tcpi.tcpi_last_ack_recv, (unsigned) tcpi.tcpi_last_data_sent, (unsigned) tcpi.tcpi_unacked); |
72 | (unsigned) tcpi.tcpi_last_ack_recv, (unsigned) tcpi.tcpi_last_data_sent, (unsigned) tcpi.tcpi_unacked); |
75 | status = Ns_Dead; |
73 | destroy (); |
76 | } |
74 | } |
77 | } |
75 | } |
78 | #endif |
76 | #endif |
79 | |
77 | |
80 | /** |
78 | /** |
… | |
… | |
89 | |
87 | |
90 | write_outputbuffer (); |
88 | write_outputbuffer (); |
91 | } |
89 | } |
92 | |
90 | |
93 | void |
91 | void |
94 | client_socket::write_outputbuffer () |
92 | client::write_outputbuffer () |
95 | { |
93 | { |
96 | while (outputbuffer.len) |
94 | while (outputbuffer.len) |
97 | { |
95 | { |
98 | int res = write (fd, outputbuffer.data + outputbuffer.start, |
96 | int res = write (fd, outputbuffer.data + outputbuffer.start, |
99 | min (outputbuffer.len, SOCKETBUFSIZE - outputbuffer.start)); |
97 | min (outputbuffer.len, SOCKETBUFSIZE - outputbuffer.start)); |
… | |
… | |
112 | #endif |
110 | #endif |
113 | } |
111 | } |
114 | else if (res == 0) |
112 | else if (res == 0) |
115 | { |
113 | { |
116 | LOG (llevError, "socket write failed, connection closed.\n"); |
114 | LOG (llevError, "socket write failed, connection closed.\n"); |
117 | status = Ns_Dead; |
115 | destroy (); |
118 | return; |
116 | return; |
119 | } |
117 | } |
120 | else if (errno == EINTR) |
118 | else if (errno == EINTR) |
121 | { |
119 | { |
122 | // just retry |
120 | // just retry |
… | |
… | |
129 | return; |
127 | return; |
130 | } |
128 | } |
131 | else |
129 | else |
132 | { |
130 | { |
133 | LOG (llevError, "socket write failed: %s\n", strerror (errno)); |
131 | LOG (llevError, "socket write failed: %s\n", strerror (errno)); |
134 | status = Ns_Dead; |
132 | destroy (); |
135 | return; |
133 | return; |
136 | } |
134 | } |
137 | } |
135 | } |
138 | |
136 | |
139 | socket_ev.poll (socket_ev.poll () & ~PE_W); |
137 | socket_ev.poll (socket_ev.poll () & ~PE_W); |
140 | } |
138 | } |
141 | |
139 | |
|
|
140 | /****************************************************************************** |
|
|
141 | * |
|
|
142 | * Start of read routines. |
|
|
143 | * |
|
|
144 | ******************************************************************************/ |
|
|
145 | |
|
|
146 | int |
|
|
147 | client::next_packet () |
|
|
148 | { |
|
|
149 | if (inbuf_len >= 2) |
|
|
150 | { |
|
|
151 | int pkt_len = (inbuf [0] << 8) | inbuf [1]; |
|
|
152 | |
|
|
153 | if (inbuf_len >= 2 + pkt_len) |
|
|
154 | return 2 + pkt_len; |
|
|
155 | |
|
|
156 | if (inbuf_len == sizeof (inbuf)) |
|
|
157 | { |
|
|
158 | send_packet_printf ("drawinfo %d input buffer overflow - closing connection.", NDI_RED); |
|
|
159 | destroy (); |
|
|
160 | return -1; |
|
|
161 | } |
|
|
162 | } |
|
|
163 | |
|
|
164 | return 0; |
|
|
165 | } |
|
|
166 | |
|
|
167 | void |
|
|
168 | client::skip_packet (int len) |
|
|
169 | { |
|
|
170 | inbuf_len -= len; |
|
|
171 | memmove (inbuf, inbuf + len, inbuf_len); |
|
|
172 | } |
|
|
173 | |
142 | /*********************************************************************** |
174 | /***************************************************************************** |
143 | * |
175 | * Start of command dispatch area. |
144 | * packet functions/utilities |
176 | * The commands here are protocol commands. |
145 | * |
|
|
146 | **********************************************************************/ |
177 | ****************************************************************************/ |
147 | |
178 | |
148 | packet &packet::operator <<(const data &v) |
179 | // SocketCommand, PlayingCommand, should not exist with those ugly casts |
|
|
180 | #define SC(cb) (void *)static_cast<void (*)(char *, int, client *)>(cb), |
|
|
181 | #define PC(cb) (void *)static_cast<void (*)(char *, int, player *)>(cb), PF_PLAYER | |
|
|
182 | |
|
|
183 | /** |
|
|
184 | * Dispatch table for the server. |
|
|
185 | */ |
|
|
186 | static struct packet_type packets[] = { |
|
|
187 | {"ncom", PC(NewPlayerCmd) PF_PLAYING }, |
|
|
188 | {"command", PC(PlayerCmd) PF_PLAYING }, |
|
|
189 | |
|
|
190 | {"examine", PC(ExamineCmd) PF_PLAYING }, |
|
|
191 | {"apply", PC(ApplyCmd) PF_PLAYING }, |
|
|
192 | {"reply", PC(ReplyCmd) 0 }, |
|
|
193 | {"lookat", PC(LookAt) PF_PLAYING }, |
|
|
194 | {"lock", PC(LockItem) PF_PLAYING }, |
|
|
195 | {"mark", PC(MarkItem) PF_PLAYING }, |
|
|
196 | {"move", PC(MoveCmd) PF_PLAYING }, |
|
|
197 | {"ext", PC(ExtCmd) 0 }, /* CF+ */ |
|
|
198 | {"mapredraw", PC(MapRedrawCmd) 0 }, /* Added: phil */ |
|
|
199 | {"mapinfo", PC(MapInfoCmd) 0 }, /* CF+ */ |
|
|
200 | |
|
|
201 | {"exti", SC(ExtiCmd) 0 }, /* CF+ */ |
|
|
202 | {"addme", SC(AddMeCmd) 0 }, |
|
|
203 | {"askface", SC(SendFaceCmd) 0 }, /* Added: phil */ |
|
|
204 | {"requestinfo", SC(RequestInfo) 0 }, |
|
|
205 | {"setfacemode", SC(SetFaceMode) 0 }, |
|
|
206 | {"setsound", SC(SetSound) 0 }, |
|
|
207 | {"setup", SC(SetUp) 0 }, |
|
|
208 | {"version", SC(VersionCmd) 0 }, |
|
|
209 | {"toggleextendedinfos", SC(ToggleExtendedInfos) 0 }, /*Added: tchize */ |
|
|
210 | {"toggleextendedtext", SC(ToggleExtendedText) 0 }, /*Added: tchize */ |
|
|
211 | {"asksmooth", SC(AskSmooth) 0 }, /*Added: tchize (smoothing technologies) */ |
|
|
212 | }; |
|
|
213 | |
|
|
214 | bool |
|
|
215 | client::may_execute (const packet_type *pkt) const |
149 | { |
216 | { |
150 | if (room () < v.len) |
217 | return (!(pkt->flags & PF_PLAYER) || pl) |
151 | reset (); |
218 | && (!(pkt->flags & PF_PLAYING) || (pl && pl->state == ST_PLAYING)); |
|
|
219 | } |
|
|
220 | |
|
|
221 | void |
|
|
222 | client::execute (const packet_type *pkt, char *data, int datalen) |
|
|
223 | { |
|
|
224 | if (may_execute (pkt)) |
|
|
225 | { |
|
|
226 | //TODO: only one format |
|
|
227 | if (pkt->flags & PF_PLAYER) |
|
|
228 | ((void (*)(char *, int, player *))pkt->cb)((char *)data, datalen, pl); |
|
|
229 | else |
|
|
230 | ((void (*)(char *, int, client *))pkt->cb)((char *)data, datalen, this); |
|
|
231 | } |
152 | else |
232 | else |
|
|
233 | send_packet_printf ("drawinfo %d ERROR: you cannot execute '%s' now.", NDI_RED, pkt->name); |
|
|
234 | } |
|
|
235 | |
|
|
236 | bool |
|
|
237 | client::handle_packet () |
|
|
238 | { |
|
|
239 | int pkt_len = next_packet (); |
|
|
240 | |
|
|
241 | if (!pkt_len) |
|
|
242 | return false; |
|
|
243 | else if (pkt_len < 0) |
|
|
244 | { |
|
|
245 | LOG (llevError, "read error on player %s\n", |
|
|
246 | pl && pl->ob ? &pl->ob->name : "[anonymous]"); |
|
|
247 | destroy (); |
|
|
248 | return false; |
153 | { |
249 | } |
154 | if (v.len) |
250 | |
|
|
251 | inbuf [pkt_len] = 0; /* Terminate buffer - useful for string data */ |
|
|
252 | |
|
|
253 | /* First, break out beginning word. There are at least |
|
|
254 | * a few commands that do not have any paremeters. If |
|
|
255 | * we get such a command, don't worry about trying |
|
|
256 | * to break it up. |
|
|
257 | */ |
|
|
258 | int datalen; |
|
|
259 | char *data = strchr ((char *)inbuf + 2, ' '); |
|
|
260 | |
|
|
261 | if (data) |
|
|
262 | { |
|
|
263 | *data++ = 0; |
|
|
264 | datalen = pkt_len - (data - (char *)inbuf); |
|
|
265 | } |
|
|
266 | else |
|
|
267 | { |
|
|
268 | data = (char *)inbuf + 2; // better read garbage than segfault |
|
|
269 | datalen = 0; |
|
|
270 | } |
|
|
271 | |
|
|
272 | for (packet_type *pkt = packets; pkt < packets + (sizeof (packets) / sizeof (packets[0])); ++pkt) |
|
|
273 | if (!strcmp ((char *)inbuf + 2, pkt->name)) |
155 | { |
274 | { |
156 | memcpy (cur, v.ptr, v.len); |
275 | if (pkt->flags & PF_PLAYER) |
157 | cur += v.len; |
276 | queue_command (pkt, data, datalen); |
|
|
277 | else |
|
|
278 | execute (pkt, data, datalen); |
|
|
279 | |
|
|
280 | goto next_packet; |
158 | } |
281 | } |
|
|
282 | |
|
|
283 | // If we get here, we didn't find a valid command. |
|
|
284 | send_packet_printf ("drawinfo %d ERROR: command '%s' not supported.", NDI_RED, (char *)inbuf + 2); |
|
|
285 | next_packet: |
|
|
286 | skip_packet (pkt_len); |
|
|
287 | |
|
|
288 | // input buffer has space again |
|
|
289 | socket_ev.poll (socket_ev.poll () | PE_R); |
|
|
290 | |
|
|
291 | return true; |
|
|
292 | } |
|
|
293 | |
|
|
294 | // callback called when socket is either readable or writable |
|
|
295 | void |
|
|
296 | client::socket_cb (iow &w, int got) |
|
|
297 | { |
|
|
298 | //TODO remove when we have better socket cleanup logic |
|
|
299 | if (status == Ns_Dead) |
159 | } |
300 | { |
160 | |
301 | socket_ev.poll (0); |
161 | return *this; |
302 | return; |
162 | } |
|
|
163 | |
|
|
164 | packet &packet::operator <<(const data8 &v) |
|
|
165 | { |
|
|
166 | unsigned int len = min (v.len, 0x00FF); |
|
|
167 | return *this << uint8 (len) << data (v.ptr, len); |
|
|
168 | } |
|
|
169 | |
|
|
170 | packet &packet::operator <<(const data16 &v) |
|
|
171 | { |
|
|
172 | unsigned int len = min (v.len, 0xFFFF); |
|
|
173 | return *this << uint16 (len) << data (v.ptr, len); |
|
|
174 | } |
|
|
175 | |
|
|
176 | packet &packet::operator <<(const char *v) |
|
|
177 | { |
|
|
178 | return *this << data (v, strlen (v ? v : 0)); |
|
|
179 | } |
|
|
180 | |
|
|
181 | void |
|
|
182 | packet::printf (const char *format, ...) |
|
|
183 | { |
|
|
184 | int size = room (); |
|
|
185 | |
|
|
186 | va_list ap; |
|
|
187 | va_start (ap, format); |
|
|
188 | int len = vsnprintf ((char *)cur, size, format, ap); |
|
|
189 | va_end (ap); |
|
|
190 | |
|
|
191 | if (len >= size) |
|
|
192 | return reset (); |
|
|
193 | |
|
|
194 | cur += len; |
|
|
195 | } |
|
|
196 | |
|
|
197 | /****************************************************************************** |
|
|
198 | * |
|
|
199 | * Start of read routines. |
|
|
200 | * |
|
|
201 | ******************************************************************************/ |
|
|
202 | |
|
|
203 | int |
|
|
204 | client_socket::read_packet () |
|
|
205 | { |
|
|
206 | for (;;) |
|
|
207 | { |
303 | } |
208 | if (inbuf_len >= 2) |
|
|
209 | { |
|
|
210 | unsigned int pkt_len = (inbuf [0] << 8) | inbuf [1]; |
|
|
211 | |
304 | |
212 | if (inbuf_len >= 2 + pkt_len) |
305 | if (got & PE_W) |
213 | return pkt_len + 2; |
306 | { |
214 | } |
307 | write_outputbuffer (); |
|
|
308 | |
|
|
309 | if (!outputbuffer.len) |
|
|
310 | socket_ev.poll (socket_ev.poll () & ~PE_W); |
|
|
311 | } |
|
|
312 | |
|
|
313 | if (got & PE_R) |
|
|
314 | { |
|
|
315 | //TODO: rate-limit tcp connection in better ways, important |
215 | |
316 | |
216 | int amount = sizeof (inbuf) - inbuf_len; |
317 | int amount = sizeof (inbuf) - inbuf_len; |
217 | |
318 | |
218 | if (amount <= 0) |
319 | if (!amount) |
219 | { |
320 | { |
220 | LOG (llevError, "packet too large");//TODO |
321 | // input buffer full |
|
|
322 | socket_ev.poll (socket_ev.poll () & ~PE_R); |
221 | return -1; |
323 | return; |
222 | } |
324 | } |
223 | |
325 | |
224 | amount = read (fd, inbuf + inbuf_len, amount); |
326 | amount = read (fd, inbuf + inbuf_len, amount); |
225 | |
327 | |
226 | if (!amount) |
328 | if (!amount) |
227 | { |
329 | { |
228 | status = Ns_Dead; |
330 | destroy (); |
229 | return -1; |
331 | return; |
230 | } |
332 | } |
231 | else if (amount < 0) |
333 | else if (amount < 0) |
232 | { |
334 | { |
233 | if (errno != EAGAIN && errno != EINTR) |
335 | if (errno != EAGAIN && errno != EINTR) |
234 | { |
336 | { |
235 | LOG (llevError, "read error: %s\n", strerror (errno)); |
337 | LOG (llevError, "read error: %s\n", strerror (errno)); |
|
|
338 | destroy (); |
236 | return -1; |
339 | return; |
237 | } |
340 | } |
238 | |
341 | |
239 | return 0; |
342 | // should not be here, normally |
|
|
343 | } |
|
|
344 | else |
240 | } |
345 | { |
241 | |
|
|
242 | inbuf_len += amount; |
346 | inbuf_len += amount; |
243 | |
347 | |
244 | cst_tot.ibytes += amount; |
348 | cst_tot.ibytes += amount; |
245 | cst_lst.ibytes += amount; |
349 | cst_lst.ibytes += amount; |
246 | } |
|
|
247 | } |
|
|
248 | |
350 | |
|
|
351 | cmd_ev.start (); |
|
|
352 | } |
|
|
353 | } |
|
|
354 | } |
|
|
355 | |
|
|
356 | // called whenever we have additional commands to process |
249 | void |
357 | void |
250 | client_socket::skip_packet (int len) |
358 | client::cmd_cb (iw &w) |
251 | { |
359 | { |
252 | inbuf_len -= len; |
360 | if (handle_packet ()) |
253 | memmove (inbuf, inbuf + len, inbuf_len); |
361 | w.start (); |
|
|
362 | else |
|
|
363 | flush (); |
254 | } |
364 | } |
255 | |
365 | |
256 | /******************************************************************************* |
366 | /******************************************************************************* |
257 | * |
367 | * |
258 | * Start of write related routines. |
368 | * Start of write related routines. |
… | |
… | |
264 | * |
374 | * |
265 | * ns is the socket we are adding the data to, buf is the start of the |
375 | * ns is the socket we are adding the data to, buf is the start of the |
266 | * data, and len is the number of bytes to add. |
376 | * data, and len is the number of bytes to add. |
267 | */ |
377 | */ |
268 | void |
378 | void |
269 | client_socket::send (void *buf_, int len) |
379 | client::send (void *buf_, int len) |
270 | { |
380 | { |
271 | char *buf = (char *)buf_; |
381 | char *buf = (char *)buf_; |
272 | char *pos = buf; |
382 | char *pos = buf; |
273 | int amt = 0; |
383 | int amt = 0; |
274 | |
384 | |
275 | if (status == Ns_Dead || !buf) |
385 | if (status == Ns_Dead || !buf) |
276 | { |
|
|
277 | LOG (llevDebug, "Write_To_Socket called with dead socket\n"); |
|
|
278 | return; |
386 | return; |
279 | } |
|
|
280 | |
387 | |
281 | if ((len + outputbuffer.len) > SOCKETBUFSIZE) |
388 | if ((len + outputbuffer.len) > SOCKETBUFSIZE) |
282 | { |
389 | { |
283 | LOG (llevDebug, "Socket on fd %d has overrun internal buffer - marking as dead\n", fd); |
390 | LOG (llevDebug, "socket on fd %d has overrun internal buffer - marking as dead\n", fd); |
284 | status = Ns_Dead; |
391 | destroy (); |
285 | return; |
392 | return; |
286 | } |
393 | } |
287 | |
394 | |
288 | int avail, end; |
395 | int avail, end; |
289 | |
396 | |
… | |
… | |
307 | } |
414 | } |
308 | |
415 | |
309 | outputbuffer.len += len; |
416 | outputbuffer.len += len; |
310 | } |
417 | } |
311 | |
418 | |
312 | void |
|
|
313 | client_socket::socket_cb (iow &w, int got) |
|
|
314 | { |
|
|
315 | write_outputbuffer (); |
|
|
316 | |
|
|
317 | if (!outputbuffer.len) |
|
|
318 | socket_ev.poll (socket_ev.poll () & ~PE_W); |
|
|
319 | } |
|
|
320 | |
|
|
321 | /** |
419 | /** |
322 | * Takes a string of data, and writes it out to the socket. A very handy |
420 | * Takes a string of data, and writes it out to the socket. A very handy |
323 | * shortcut function. |
421 | * shortcut function. |
324 | */ |
422 | */ |
325 | void |
423 | void |
326 | client_socket::send_packet (packet &sl) |
424 | client::send_packet (packet &sl) |
327 | { |
425 | { |
328 | if (status == Ns_Dead) |
426 | if (status == Ns_Dead) |
329 | return; |
427 | return; |
330 | |
428 | |
331 | if (sl.length () >= MAXSOCKBUF) |
429 | if (sl.length () >= MAXSOCKBUF) |
… | |
… | |
347 | |
445 | |
348 | send (sl.buf_, sl.length () + sl.hdrlen); |
446 | send (sl.buf_, sl.length () + sl.hdrlen); |
349 | } |
447 | } |
350 | |
448 | |
351 | void |
449 | void |
352 | client_socket::send_packet (const char *buf, int len) |
450 | client::send_packet (const char *buf, int len) |
353 | { |
451 | { |
354 | packet sl; |
452 | packet sl; |
355 | |
453 | |
356 | sl << data (buf, len); |
454 | sl << data (buf, len); |
357 | send_packet (sl); |
455 | send_packet (sl); |
358 | } |
456 | } |
359 | |
457 | |
360 | void |
458 | void |
361 | client_socket::send_packet (const char *buf) |
459 | client::send_packet (const char *buf) |
362 | { |
460 | { |
363 | send_packet (buf, strlen (buf)); |
461 | send_packet (buf, strlen (buf)); |
|
|
462 | } |
|
|
463 | |
|
|
464 | void |
|
|
465 | client::send_packet_printf (const char *format, ...) |
|
|
466 | { |
|
|
467 | packet sl; |
|
|
468 | |
|
|
469 | va_list ap; |
|
|
470 | va_start (ap, format); |
|
|
471 | sl.vprintf (format, ap); |
|
|
472 | va_end (ap); |
|
|
473 | |
|
|
474 | send_packet (sl); |
|
|
475 | } |
|
|
476 | |
|
|
477 | /*********************************************************************** |
|
|
478 | * |
|
|
479 | * packet functions/utilities |
|
|
480 | * |
|
|
481 | **********************************************************************/ |
|
|
482 | |
|
|
483 | packet::packet (const char *name) |
|
|
484 | { |
|
|
485 | reset (); |
|
|
486 | |
|
|
487 | int len = strlen (name); |
|
|
488 | memcpy (cur, name, len); cur += len; |
|
|
489 | *cur++ = ' '; |
|
|
490 | } |
|
|
491 | |
|
|
492 | packet &packet::operator <<(const data &v) |
|
|
493 | { |
|
|
494 | if (room () < v.len) |
|
|
495 | reset (); |
|
|
496 | else |
|
|
497 | { |
|
|
498 | if (v.len) |
|
|
499 | { |
|
|
500 | memcpy (cur, v.ptr, v.len); |
|
|
501 | cur += v.len; |
|
|
502 | } |
|
|
503 | } |
|
|
504 | |
|
|
505 | return *this; |
|
|
506 | } |
|
|
507 | |
|
|
508 | packet &packet::operator <<(const data8 &v) |
|
|
509 | { |
|
|
510 | unsigned int len = min (v.len, 0x00FF); |
|
|
511 | return *this << uint8 (len) << data (v.ptr, len); |
|
|
512 | } |
|
|
513 | |
|
|
514 | packet &packet::operator <<(const data16 &v) |
|
|
515 | { |
|
|
516 | unsigned int len = min (v.len, 0xFFFF); |
|
|
517 | return *this << uint16 (len) << data (v.ptr, len); |
|
|
518 | } |
|
|
519 | |
|
|
520 | packet &packet::operator <<(const char *v) |
|
|
521 | { |
|
|
522 | return *this << data (v, strlen (v ? v : 0)); |
|
|
523 | } |
|
|
524 | |
|
|
525 | void |
|
|
526 | packet::vprintf (const char *format, va_list ap) |
|
|
527 | { |
|
|
528 | int size = room (); |
|
|
529 | |
|
|
530 | int len = vsnprintf ((char *)cur, size, format, ap); |
|
|
531 | |
|
|
532 | if (len >= size) |
|
|
533 | return reset (); |
|
|
534 | |
|
|
535 | cur += len; |
364 | } |
536 | } |
365 | |
537 | |
366 | /****************************************************************************** |
538 | /****************************************************************************** |
367 | * |
539 | * |
368 | * statistics logging functions. |
540 | * statistics logging functions. |