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