1 |
/* |
2 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
* |
4 |
* Copyright (©) 2005,2006,2007,2008,2009 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 |
* |
6 |
* Deliantra is free software: you can redistribute it and/or modify it under |
7 |
* the terms of the Affero GNU General Public License as published by the |
8 |
* Free Software Foundation, either version 3 of the License, or (at your |
9 |
* option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the Affero GNU General Public License |
17 |
* and the GNU General Public License along with this program. If not, see |
18 |
* <http://www.gnu.org/licenses/>. |
19 |
* |
20 |
* The authors can be reached via e-mail to <support@deliantra.net> |
21 |
*/ |
22 |
|
23 |
/* |
24 |
* Socket general functions |
25 |
*/ |
26 |
|
27 |
#include <global.h> |
28 |
#include <sproto.h> |
29 |
#include <sys/types.h> |
30 |
#include <sys/time.h> |
31 |
#include <sys/socket.h> |
32 |
#include <netinet/in.h> |
33 |
#include <netinet/tcp.h> |
34 |
#include <netinet/ip.h> |
35 |
#include <netdb.h> |
36 |
|
37 |
#include <unistd.h> |
38 |
#include <arpa/inet.h> |
39 |
|
40 |
#include <algorithm> |
41 |
|
42 |
// use relatively small values, as we do not expect to receive much, |
43 |
// and we do userspace write buffering |
44 |
// 8kb limits throughput to roughly 66kb/s |
45 |
#define SOCKET_RCVBUF 8192 |
46 |
#define SOCKET_SNDBUF 16384 |
47 |
|
48 |
sockvec clients; |
49 |
|
50 |
/** |
51 |
* Initializes a connection. Really, it just sets up the data structure, |
52 |
* socket setup is handled elsewhere. We do send a version to the |
53 |
* client. |
54 |
*/ |
55 |
client::client (int fd, const char *peername) |
56 |
: fd (fd), host (strdup (peername)), |
57 |
socket_ev (this, &client::socket_cb) |
58 |
{ |
59 |
refcnt_inc (); // the socket is an external reference |
60 |
|
61 |
mss = 1500 - 52; // 1500 typical ethernet frame, 66 typical tcp header overhead |
62 |
|
63 |
socket_timeout = 16.; |
64 |
|
65 |
{ |
66 |
struct linger linger_opt; |
67 |
|
68 |
linger_opt.l_onoff = 0; |
69 |
linger_opt.l_linger = 0; |
70 |
|
71 |
if (setsockopt (fd, SOL_SOCKET, SO_LINGER, &linger_opt, sizeof (struct linger))) |
72 |
LOG (llevError, "SO_LINGER: %s\n", strerror (errno)); |
73 |
} |
74 |
|
75 |
{ |
76 |
int val; |
77 |
|
78 |
#ifdef SO_RCVBUF |
79 |
val = SOCKET_RCVBUF; |
80 |
if (setsockopt (fd, SOL_SOCKET, SO_RCVBUF, &val, sizeof (val))) |
81 |
LOG (llevError, "SO_RCVBUF: %s\n", strerror (errno)); |
82 |
#endif |
83 |
|
84 |
#ifdef SO_SNDBUF |
85 |
val = SOCKET_SNDBUF; |
86 |
if (setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &val, sizeof (val))) |
87 |
LOG (llevError, "SO_SNDBUF: %s\n", strerror (errno)); |
88 |
#endif |
89 |
|
90 |
#ifdef IP_TOS |
91 |
val = IPTOS_LOWDELAY; |
92 |
if (setsockopt (fd, IPPROTO_IP, IP_TOS, &val, sizeof (val))) |
93 |
LOG (llevError, "IP_TOS: %s\n", strerror (errno)); |
94 |
#endif |
95 |
|
96 |
#ifdef TCP_NODELAY |
97 |
val = 1; |
98 |
if (setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val))) |
99 |
LOG (llevError, "TCP_NODELAY: %s\n", strerror (errno)); |
100 |
#endif |
101 |
|
102 |
// set some very aggressive keepalive parameters |
103 |
#ifdef TCP_KEEPIDLE |
104 |
val = 1; |
105 |
if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof (val))) |
106 |
LOG (llevError, "TCP_KEEPIDLE: %s\n", strerror (errno)); |
107 |
#endif |
108 |
|
109 |
#ifdef TCP_KEEPCNT |
110 |
val = 3; |
111 |
if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof (val))) |
112 |
LOG (llevError, "TCP_KEEPCNT: %s\n", strerror (errno)); |
113 |
#endif |
114 |
|
115 |
#ifdef TCP_KEEPINTVL |
116 |
val = 1; |
117 |
if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof (val))) |
118 |
LOG (llevError, "TCP_KEEPINTVL: %s\n", strerror (errno)); |
119 |
#endif |
120 |
|
121 |
// try to find the mss value in use |
122 |
#ifdef TCP_MAXSEG |
123 |
socklen_t sl = sizeof (val); |
124 |
if (!getsockopt (fd, IPPROTO_TCP, TCP_MAXSEG, &val, &sl) && sl == sizeof (val)) |
125 |
mss = val; |
126 |
#endif |
127 |
} |
128 |
|
129 |
if (fcntl (fd, F_SETFL, O_NONBLOCK) == -1) |
130 |
LOG (llevError, "InitConnection: Error on fcntl.\n"); |
131 |
|
132 |
state = ST_SETUP; |
133 |
mapmode = Map1aCmd; |
134 |
mapx = 11; |
135 |
mapy = 11; |
136 |
itemcmd = 1; /* Default is version item1 command */ |
137 |
max_rate = 100000 / (1000000 / MAX_TIME); // ~1mbit is assumed per default |
138 |
|
139 |
/* Do this so we don't send a face command for the client for |
140 |
* this face. Face 0 is sent to the client to say clear |
141 |
* face information. |
142 |
*/ |
143 |
faces_sent[0] = true; |
144 |
fx_want [FT_FACE] = true; // all clients must support image faces |
145 |
|
146 |
socket_ev.set (fd, EV_READ); |
147 |
socket_ev.prio (2); // one higher than the ticker priority |
148 |
socket_ev.start (); |
149 |
|
150 |
// initialisation done, kick it! |
151 |
send_packet_printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); |
152 |
flush (); |
153 |
|
154 |
reset_stats (); |
155 |
|
156 |
clients.insert (this); |
157 |
} |
158 |
|
159 |
client::~client () |
160 |
{ |
161 |
clients.erase (this); |
162 |
|
163 |
mapinfo_queue_clear (); |
164 |
free (stats.range); |
165 |
free (stats.title); |
166 |
free (host); |
167 |
} |
168 |
|
169 |
void |
170 |
client::do_destroy () |
171 |
{ |
172 |
attachable::do_destroy (); |
173 |
|
174 |
if (pl) |
175 |
pl->disconnect (); |
176 |
|
177 |
if (fd >= 0) |
178 |
{ |
179 |
send_packet ("goodbye"); |
180 |
flush (); |
181 |
|
182 |
close (fd); |
183 |
} |
184 |
|
185 |
state = ST_DEAD; |
186 |
|
187 |
socket_ev.stop (); |
188 |
|
189 |
refcnt_dec (); // socket no longer open |
190 |
} |
191 |
|
192 |
void |
193 |
client::reset_stats () |
194 |
{ |
195 |
/* we need to clear these to -1 and not zero - otherwise, |
196 |
* if a player quits and starts a new character, we wont |
197 |
* send new values to the client, as things like exp start |
198 |
* at zero. |
199 |
*/ |
200 |
for (int i = 0; i < NUM_SKILLS; i++) |
201 |
last_skill_exp[i] = -1; |
202 |
|
203 |
for (int i = 0; i < NROFATTACKS; i++) |
204 |
last_resist[i] = -1; |
205 |
|
206 |
last_weapon_sp = -1; |
207 |
last_level = -1; |
208 |
last_stats.exp = -1; |
209 |
last_flags = 0; |
210 |
last_weight = -1; |
211 |
last_weight_limit = 0; |
212 |
last_path_attuned = 0; |
213 |
last_path_repelled = 0; |
214 |
last_path_denied = 0; |
215 |
last_speed = 0; |
216 |
last_flags = 0; |
217 |
|
218 |
static living zero_living; |
219 |
last_stats = zero_living; |
220 |
} |
221 |
|
222 |
client * |
223 |
client::create (int fd, const char *peername) |
224 |
{ |
225 |
client *ns = new client (dup (fd), peername); |
226 |
ns->instantiate (); // effectively a nop right now |
227 |
INVOKE_CLIENT (CONNECT, ns); |
228 |
return ns; |
229 |
} |
230 |
|