1 | /* |
1 | /* |
2 | * CrossFire, A Multiplayer game for X-windows |
2 | * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game. |
3 | * |
3 | * |
4 | * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
4 | * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team |
5 | * Copyright (C) 2001 Mark Wedel |
5 | * Copyright (©) 2001,2007 Mark Wedel |
6 | * Copyright (C) 1992 Frank Tore Johansen |
6 | * Copyright (©) 1992,2007 Frank Tore Johansen |
7 | * |
7 | * |
8 | * This program is free software; you can redistribute it and/or modify |
8 | * Crossfire TRT is free software; you can redistribute it and/or modify it |
9 | * it under the terms of the GNU General Public License as published by |
9 | * under the terms of the GNU General Public License as published by the Free |
10 | * the Free Software Foundation; either version 2 of the License, or |
10 | * Software Foundation; either version 2 of the License, or (at your option) |
11 | * (at your option) any later version. |
11 | * any later version. |
12 | * |
12 | * |
13 | * This program is distributed in the hope that it will be useful, |
13 | * This program is distributed in the hope that it will be useful, but |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
16 | * GNU General Public License for more details. |
16 | * for more details. |
17 | * |
17 | * |
18 | * You should have received a copy of the GNU General Public License |
18 | * You should have received a copy of the GNU General Public License along |
19 | * along with this program; if not, write to the Free Software |
19 | * with Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 |
20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 | * |
21 | * |
22 | * The author can be reached via e-mail to <crossfire@schmorp.de> |
22 | * The authors can be reached via e-mail to <crossfire@schmorp.de> |
23 | */ |
23 | */ |
24 | |
24 | |
25 | /** |
25 | /** |
26 | * \file |
26 | * \file |
27 | * Socket general functions |
27 | * Socket general functions |
… | |
… | |
46 | #include <unistd.h> |
46 | #include <unistd.h> |
47 | #include <arpa/inet.h> |
47 | #include <arpa/inet.h> |
48 | |
48 | |
49 | #include <algorithm> |
49 | #include <algorithm> |
50 | |
50 | |
|
|
51 | // use relatively small values, as we do not expect to receive much, |
|
|
52 | // and we do userspace write buffering |
|
|
53 | // 8kb limits throughput to roughly 66kb/s |
|
|
54 | #define SOCKET_RCVBUF 8192 |
|
|
55 | #define SOCKET_SNDBUF 16384 |
|
|
56 | |
51 | sockvec clients; |
57 | sockvec clients; |
52 | |
58 | |
53 | /** |
59 | /** |
54 | * Initializes a connection. Really, it just sets up the data structure, |
60 | * Initializes a connection. Really, it just sets up the data structure, |
55 | * socket setup is handled elsewhere. We do send a version to the |
61 | * socket setup is handled elsewhere. We do send a version to the |
56 | * client. |
62 | * client. |
57 | */ |
63 | */ |
58 | client::client (int fd, const char *peername) |
64 | client::client (int fd, const char *peername) |
59 | : fd (fd), host (strdup (peername)), |
65 | : fd (fd), host (strdup (peername)), |
60 | socket_ev (this, &client::socket_cb), |
66 | socket_ev (this, &client::socket_cb), |
61 | cmd_ev (this, &client::cmd_cb), |
67 | cmd_ev (this, &client::cmd_cb) |
62 | cc_inv (this), cc_other (this) |
|
|
63 | { |
68 | { |
64 | refcnt_inc (); // the socket is an external reference |
69 | refcnt_inc (); // the socket is an external reference |
65 | |
70 | |
66 | mss = 1500 - 66; // 1500 typical ethernet frame, 66 typical tcp header overhead |
71 | mss = 1500 - 52; // 1500 typical ethernet frame, 66 typical tcp header overhead |
67 | |
72 | |
68 | { |
73 | { |
69 | struct linger linger_opt; |
74 | struct linger linger_opt; |
70 | |
75 | |
71 | linger_opt.l_onoff = 0; |
76 | linger_opt.l_onoff = 0; |
72 | linger_opt.l_linger = 0; |
77 | linger_opt.l_linger = 0; |
73 | |
78 | |
74 | if (setsockopt (fd, SOL_SOCKET, SO_LINGER, &linger_opt, sizeof (struct linger))) |
79 | if (setsockopt (fd, SOL_SOCKET, SO_LINGER, &linger_opt, sizeof (struct linger))) |
75 | LOG (llevError, "SO_LINGER: %s\n", strerror (errno)); |
80 | LOG (llevError, "SO_LINGER: %s\n", strerror (errno)); |
76 | } |
81 | } |
77 | |
82 | |
78 | { |
83 | { |
79 | int val; |
84 | int val; |
|
|
85 | |
|
|
86 | #ifdef SO_RCVBUF |
|
|
87 | val = SOCKET_RCVBUF; |
|
|
88 | if (setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &val, sizeof (val))) |
|
|
89 | LOG (llevError, "SO_RCVBUF: %s\n", strerror (errno)); |
|
|
90 | #endif |
|
|
91 | |
|
|
92 | #ifdef SO_SNDBUF |
|
|
93 | val = SOCKET_SNDBUF; |
|
|
94 | if (setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &val, sizeof (val))) |
|
|
95 | LOG (llevError, "SO_SNDBUF: %s\n", strerror (errno)); |
|
|
96 | #endif |
80 | |
97 | |
81 | #ifdef IP_TOS |
98 | #ifdef IP_TOS |
82 | val = IPTOS_LOWDELAY; |
99 | val = IPTOS_LOWDELAY; |
83 | if (setsockopt (fd, IPPROTO_IP, IP_TOS, &val, sizeof (val))) |
100 | if (setsockopt (fd, IPPROTO_IP, IP_TOS, &val, sizeof (val))) |
84 | LOG (llevError, "IP_TOS: %s\n", strerror (errno)); |
101 | LOG (llevError, "IP_TOS: %s\n", strerror (errno)); |
… | |
… | |
90 | LOG (llevError, "TCP_NODELAY: %s\n", strerror (errno)); |
107 | LOG (llevError, "TCP_NODELAY: %s\n", strerror (errno)); |
91 | #endif |
108 | #endif |
92 | |
109 | |
93 | // set some very aggressive keepalive parameters |
110 | // set some very aggressive keepalive parameters |
94 | #ifdef TCP_KEEPIDLE |
111 | #ifdef TCP_KEEPIDLE |
95 | val = 10; |
112 | val = 1; |
96 | if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof (val))) |
113 | if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof (val))) |
97 | LOG (llevError, "TCP_KEEPIDLE: %s\n", strerror (errno)); |
114 | LOG (llevError, "TCP_KEEPIDLE: %s\n", strerror (errno)); |
98 | #endif |
115 | #endif |
99 | |
116 | |
100 | #ifdef TCP_KEEPCNT |
117 | #ifdef TCP_KEEPCNT |
101 | val = 5; |
118 | val = 3; |
102 | if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof (val))) |
119 | if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof (val))) |
103 | LOG (llevError, "TCP_KEEPCNT: %s\n", strerror (errno)); |
120 | LOG (llevError, "TCP_KEEPCNT: %s\n", strerror (errno)); |
104 | #endif |
121 | #endif |
105 | |
122 | |
106 | #ifdef TCP_KEEPINTVL |
123 | #ifdef TCP_KEEPINTVL |
… | |
… | |
221 | last_weight_limit = 0; |
238 | last_weight_limit = 0; |
222 | last_path_attuned = 0; |
239 | last_path_attuned = 0; |
223 | last_path_repelled = 0; |
240 | last_path_repelled = 0; |
224 | last_path_denied = 0; |
241 | last_path_denied = 0; |
225 | last_speed = 0; |
242 | last_speed = 0; |
226 | flags = 0; |
243 | last_flags = 0; |
227 | |
244 | |
228 | static living zero_living; |
245 | static living zero_living; |
229 | last_stats = zero_living; |
246 | last_stats = zero_living; |
230 | } |
247 | } |
231 | |
|
|
232 | /** This sets up the socket and reads all the image information into memory. */ |
|
|
233 | void |
|
|
234 | init_ericserver (void) |
|
|
235 | { |
|
|
236 | cst_tot.time_start = time (NULL); |
|
|
237 | cst_lst.time_start = time (NULL); |
|
|
238 | |
|
|
239 | LOG (llevDebug, "Initialize new client/server data\n"); |
|
|
240 | } |
|
|
241 | |
|
|
242 | |
248 | |
243 | /******************************************************************************* |
249 | /******************************************************************************* |
244 | * |
250 | * |
245 | * Start of functions dealing with freeing of the data. |
251 | * Start of functions dealing with freeing of the data. |
246 | * |
252 | * |