1 | /* |
1 | /* |
2 | CrossFire, A Multiplayer game for X-windows |
2 | * CrossFire, A Multiplayer game for X-windows |
3 | |
3 | * |
|
|
4 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
4 | Copyright (C) 1992 Frank Tore Johansen |
5 | * Copyright (C) 1992 Frank Tore Johansen |
5 | |
6 | * |
6 | This program is free software; you can redistribute it and/or modify |
7 | * This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by |
8 | * it under the terms of the GNU General Public License as published by |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | * the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. |
10 | * (at your option) any later version. |
10 | |
11 | * |
11 | This program is distributed in the hope that it will be useful, |
12 | * This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | GNU General Public License for more details. |
15 | * GNU General Public License for more details. |
15 | |
16 | * |
16 | You should have received a copy of the GNU General Public License |
17 | * You should have received a copy of the GNU General Public License |
17 | along with this program; if not, write to the Free Software |
18 | * along with this program; if not, write to the Free Software |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 | |
20 | * |
20 | The author can be reached via e-mail to mark@pyramid.com |
21 | * The author can be reached via e-mail to mark@pyramid.com |
21 | */ |
22 | */ |
22 | |
23 | |
23 | /** |
24 | /** |
24 | * \file |
25 | * \file |
25 | * Low-level socket-related functions. |
26 | * Low-level socket-related functions. |
26 | * |
27 | * |
27 | * \date 2003-12-02 |
28 | * \date 2003-12-02 |
… | |
… | |
51 | #define SOCKET_TIMEOUT2 20 * 1000 |
52 | #define SOCKET_TIMEOUT2 20 * 1000 |
52 | |
53 | |
53 | void |
54 | void |
54 | client::flush () |
55 | client::flush () |
55 | { |
56 | { |
|
|
57 | if (destroyed ()) |
|
|
58 | return; |
|
|
59 | |
56 | #ifdef __linux__ |
60 | #ifdef __linux__ |
57 | // check time of last ack, and, if too old, kill connection |
61 | // check time of last ack, and, if too old, kill connection |
58 | struct tcp_info tcpi; |
62 | struct tcp_info tcpi; |
59 | socklen_t len = sizeof (tcpi); |
63 | socklen_t len = sizeof (tcpi); |
60 | |
64 | |
… | |
… | |
213 | |
217 | |
214 | bool |
218 | bool |
215 | client::may_execute (const packet_type *pkt) const |
219 | client::may_execute (const packet_type *pkt) const |
216 | { |
220 | { |
217 | return (!(pkt->flags & PF_PLAYER) || pl) |
221 | return (!(pkt->flags & PF_PLAYER) || pl) |
218 | && (!(pkt->flags & PF_PLAYING) || (pl && pl->state == ST_PLAYING)); |
222 | && (!(pkt->flags & PF_PLAYING) || state == ST_PLAYING); |
219 | } |
223 | } |
220 | |
224 | |
221 | void |
225 | void |
222 | client::execute (const packet_type *pkt, char *data, int datalen) |
226 | client::execute (const packet_type *pkt, char *data, int datalen) |
223 | { |
227 | { |
… | |
… | |
294 | // callback called when socket is either readable or writable |
298 | // callback called when socket is either readable or writable |
295 | void |
299 | void |
296 | client::socket_cb (iow &w, int got) |
300 | client::socket_cb (iow &w, int got) |
297 | { |
301 | { |
298 | //TODO remove when we have better socket cleanup logic |
302 | //TODO remove when we have better socket cleanup logic |
299 | if (destroyed) |
303 | if (destroyed ()) |
300 | { |
304 | { |
301 | socket_ev.poll (0); |
305 | socket_ev.poll (0); |
302 | return; |
306 | return; |
303 | } |
307 | } |
304 | |
308 | |
… | |
… | |
380 | { |
384 | { |
381 | char *buf = (char *)buf_; |
385 | char *buf = (char *)buf_; |
382 | char *pos = buf; |
386 | char *pos = buf; |
383 | int amt = 0; |
387 | int amt = 0; |
384 | |
388 | |
385 | if (destroyed || !buf) |
389 | if (destroyed () || !buf) |
386 | return; |
390 | return; |
387 | |
391 | |
388 | if ((len + outputbuffer.len) > SOCKETBUFSIZE) |
392 | if (len + outputbuffer.len > SOCKETBUFSIZE) |
389 | { |
393 | { |
390 | LOG (llevDebug, "socket on fd %d has overrun internal buffer - marking as dead\n", fd); |
394 | LOG (llevDebug, "socket on fd %d has overrun internal buffer - marking as dead\n", fd); |
391 | destroy (); |
395 | destroy (); |
392 | return; |
396 | return; |
393 | } |
397 | } |
… | |
… | |
421 | * shortcut function. |
425 | * shortcut function. |
422 | */ |
426 | */ |
423 | void |
427 | void |
424 | client::send_packet (packet &sl) |
428 | client::send_packet (packet &sl) |
425 | { |
429 | { |
426 | if (destroyed) |
430 | if (destroyed ()) |
427 | return; |
431 | return; |
428 | |
432 | |
429 | if (sl.length () >= MAXSOCKBUF) |
433 | if (sl.length () >= MAXSOCKBUF) |
430 | { |
434 | { |
431 | LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", sl.length ()); |
435 | LOG (llevError, "Trying to send a buffer beyond properly size, len =%d\n", sl.length ()); |