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