1 |
elmex |
1.1 |
/* |
2 |
root |
1.53 |
* This file is part of Deliantra, the Roguelike Realtime MMORPG. |
3 |
pippijn |
1.36 |
* |
4 |
root |
1.64 |
* Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 |
pippijn |
1.36 |
* |
6 |
root |
1.62 |
* 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 |
pippijn |
1.36 |
* |
11 |
root |
1.49 |
* 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 |
pippijn |
1.36 |
* |
16 |
root |
1.62 |
* 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 |
root |
1.46 |
* |
20 |
root |
1.53 |
* The authors can be reached via e-mail to <support@deliantra.net> |
21 |
pippijn |
1.36 |
*/ |
22 |
elmex |
1.1 |
|
23 |
root |
1.63 |
/* |
24 |
elmex |
1.1 |
* Socket general functions |
25 |
|
|
*/ |
26 |
|
|
|
27 |
|
|
#include <global.h> |
28 |
root |
1.11 |
#include <sproto.h> |
29 |
pippijn |
1.10 |
#include <sys/types.h> |
30 |
|
|
#include <sys/time.h> |
31 |
|
|
#include <sys/socket.h> |
32 |
|
|
#include <netinet/in.h> |
33 |
root |
1.23 |
#include <netinet/tcp.h> |
34 |
|
|
#include <netinet/ip.h> |
35 |
pippijn |
1.10 |
#include <netdb.h> |
36 |
elmex |
1.1 |
|
37 |
root |
1.19 |
#include <unistd.h> |
38 |
|
|
#include <arpa/inet.h> |
39 |
elmex |
1.1 |
|
40 |
root |
1.31 |
#include <algorithm> |
41 |
|
|
|
42 |
root |
1.48 |
// 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 |
root |
1.20 |
sockvec clients; |
49 |
elmex |
1.1 |
|
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 |
root |
1.20 |
client::client (int fd, const char *peername) |
56 |
root |
1.17 |
: fd (fd), host (strdup (peername)), |
57 |
root |
1.52 |
socket_ev (this, &client::socket_cb) |
58 |
elmex |
1.1 |
{ |
59 |
root |
1.34 |
refcnt_inc (); // the socket is an external reference |
60 |
|
|
|
61 |
root |
1.43 |
mss = 1500 - 52; // 1500 typical ethernet frame, 66 typical tcp header overhead |
62 |
root |
1.42 |
|
63 |
root |
1.61 |
socket_timeout = 16.; |
64 |
|
|
|
65 |
root |
1.16 |
{ |
66 |
|
|
struct linger linger_opt; |
67 |
|
|
|
68 |
root |
1.44 |
linger_opt.l_onoff = 0; |
69 |
root |
1.16 |
linger_opt.l_linger = 0; |
70 |
|
|
|
71 |
|
|
if (setsockopt (fd, SOL_SOCKET, SO_LINGER, &linger_opt, sizeof (struct linger))) |
72 |
root |
1.23 |
LOG (llevError, "SO_LINGER: %s\n", strerror (errno)); |
73 |
root |
1.16 |
} |
74 |
|
|
|
75 |
|
|
{ |
76 |
root |
1.42 |
int val; |
77 |
root |
1.23 |
|
78 |
root |
1.48 |
#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 |
root |
1.42 |
#ifdef IP_TOS |
91 |
|
|
val = IPTOS_LOWDELAY; |
92 |
root |
1.23 |
if (setsockopt (fd, IPPROTO_IP, IP_TOS, &val, sizeof (val))) |
93 |
|
|
LOG (llevError, "IP_TOS: %s\n", strerror (errno)); |
94 |
root |
1.42 |
#endif |
95 |
root |
1.23 |
|
96 |
root |
1.42 |
#ifdef TCP_NODELAY |
97 |
|
|
val = 1; |
98 |
root |
1.23 |
if (setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val))) |
99 |
|
|
LOG (llevError, "TCP_NODELAY: %s\n", strerror (errno)); |
100 |
root |
1.42 |
#endif |
101 |
|
|
|
102 |
|
|
// set some very aggressive keepalive parameters |
103 |
|
|
#ifdef TCP_KEEPIDLE |
104 |
root |
1.44 |
val = 1; |
105 |
root |
1.42 |
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 |
root |
1.44 |
val = 3; |
111 |
root |
1.42 |
if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof (val))) |
112 |
|
|
LOG (llevError, "TCP_KEEPCNT: %s\n", strerror (errno)); |
113 |
|
|
#endif |
114 |
root |
1.23 |
|
115 |
root |
1.42 |
#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 |
root |
1.23 |
|
121 |
root |
1.42 |
// 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 |
root |
1.23 |
} |
128 |
|
|
|
129 |
root |
1.55 |
if (fcntl (fd, F_SETFL, O_NONBLOCK) == -1) |
130 |
|
|
LOG (llevError, "InitConnection: Error on fcntl.\n"); |
131 |
elmex |
1.1 |
|
132 |
root |
1.28 |
state = ST_SETUP; |
133 |
root |
1.60 |
mapmode = Map1aCmd; |
134 |
root |
1.15 |
mapx = 11; |
135 |
|
|
mapy = 11; |
136 |
|
|
itemcmd = 1; /* Default is version item1 command */ |
137 |
root |
1.66 |
max_rate = 200000 / (1000000 / MAX_TIME); // ~2mbit is assumed per default |
138 |
root |
1.12 |
|
139 |
root |
1.7 |
/* 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 |
root |
1.40 |
faces_sent[0] = true; |
144 |
root |
1.50 |
fx_want [FT_FACE] = true; // all clients must support image faces |
145 |
root |
1.7 |
|
146 |
root |
1.54 |
socket_ev.set (fd, EV_READ); |
147 |
root |
1.56 |
socket_ev.prio (2); // one higher than the ticker priority |
148 |
root |
1.54 |
socket_ev.start (); |
149 |
root |
1.21 |
|
150 |
root |
1.16 |
// initialisation done, kick it! |
151 |
root |
1.66 |
INVOKE_CLIENT (CONNECT, this); |
152 |
|
|
|
153 |
root |
1.21 |
flush (); |
154 |
root |
1.13 |
|
155 |
root |
1.35 |
reset_stats (); |
156 |
|
|
|
157 |
root |
1.34 |
clients.insert (this); |
158 |
elmex |
1.1 |
} |
159 |
|
|
|
160 |
root |
1.20 |
client::~client () |
161 |
root |
1.16 |
{ |
162 |
root |
1.34 |
clients.erase (this); |
163 |
root |
1.32 |
|
164 |
root |
1.51 |
mapinfo_queue_clear (); |
165 |
root |
1.22 |
free (stats.range); |
166 |
|
|
free (stats.title); |
167 |
|
|
free (host); |
168 |
root |
1.21 |
} |
169 |
|
|
|
170 |
|
|
void |
171 |
root |
1.31 |
client::do_destroy () |
172 |
root |
1.21 |
{ |
173 |
root |
1.65 |
INVOKE_CLIENT (CLIENT_DESTROY, this); |
174 |
root |
1.31 |
attachable::do_destroy (); |
175 |
|
|
|
176 |
|
|
if (pl) |
177 |
|
|
pl->disconnect (); |
178 |
|
|
|
179 |
|
|
if (fd >= 0) |
180 |
root |
1.34 |
{ |
181 |
|
|
send_packet ("goodbye"); |
182 |
|
|
flush (); |
183 |
|
|
|
184 |
|
|
close (fd); |
185 |
|
|
} |
186 |
root |
1.29 |
|
187 |
root |
1.28 |
state = ST_DEAD; |
188 |
root |
1.27 |
|
189 |
root |
1.54 |
socket_ev.stop (); |
190 |
root |
1.31 |
|
191 |
root |
1.34 |
refcnt_dec (); // socket no longer open |
192 |
root |
1.16 |
} |
193 |
elmex |
1.1 |
|
194 |
root |
1.35 |
void |
195 |
|
|
client::reset_stats () |
196 |
|
|
{ |
197 |
|
|
/* we need to clear these to -1 and not zero - otherwise, |
198 |
|
|
* if a player quits and starts a new character, we wont |
199 |
|
|
* send new values to the client, as things like exp start |
200 |
|
|
* at zero. |
201 |
|
|
*/ |
202 |
|
|
for (int i = 0; i < NUM_SKILLS; i++) |
203 |
|
|
last_skill_exp[i] = -1; |
204 |
|
|
|
205 |
|
|
for (int i = 0; i < NROFATTACKS; i++) |
206 |
|
|
last_resist[i] = -1; |
207 |
|
|
|
208 |
|
|
last_weapon_sp = -1; |
209 |
|
|
last_level = -1; |
210 |
|
|
last_stats.exp = -1; |
211 |
|
|
last_flags = 0; |
212 |
root |
1.58 |
last_weight = -1; |
213 |
root |
1.35 |
last_weight_limit = 0; |
214 |
|
|
last_path_attuned = 0; |
215 |
|
|
last_path_repelled = 0; |
216 |
|
|
last_path_denied = 0; |
217 |
|
|
last_speed = 0; |
218 |
root |
1.45 |
last_flags = 0; |
219 |
root |
1.35 |
|
220 |
|
|
static living zero_living; |
221 |
|
|
last_stats = zero_living; |
222 |
|
|
} |
223 |
|
|
|
224 |
root |
1.27 |
client * |
225 |
|
|
client::create (int fd, const char *peername) |
226 |
root |
1.16 |
{ |
227 |
root |
1.38 |
client *ns = new client (dup (fd), peername); |
228 |
root |
1.66 |
|
229 |
root |
1.38 |
ns->instantiate (); // effectively a nop right now |
230 |
root |
1.66 |
|
231 |
root |
1.38 |
return ns; |
232 |
root |
1.16 |
} |
233 |
|
|
|