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.71 |
* Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
5 |
root |
1.72 |
* |
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 |
root |
1.72 |
* |
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 |
root |
1.72 |
* |
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.72 |
* |
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.15 |
mapx = 11; |
134 |
|
|
mapy = 11; |
135 |
|
|
itemcmd = 1; /* Default is version item1 command */ |
136 |
root |
1.67 |
max_rate = 100000 / (1000000 / MAX_TIME); // ~1mbit is assumed per default |
137 |
root |
1.12 |
|
138 |
root |
1.7 |
/* Do this so we don't send a face command for the client for |
139 |
root |
1.73 |
* this face. Face 0 is sent to the client to set meta data |
140 |
|
|
* for following faces in fx. |
141 |
root |
1.7 |
*/ |
142 |
root |
1.40 |
faces_sent[0] = true; |
143 |
root |
1.75 |
fx_want [FT_IMAGE] = true; // all clients must support image faces |
144 |
root |
1.7 |
|
145 |
root |
1.54 |
socket_ev.set (fd, EV_READ); |
146 |
root |
1.56 |
socket_ev.prio (2); // one higher than the ticker priority |
147 |
root |
1.54 |
socket_ev.start (); |
148 |
root |
1.21 |
|
149 |
root |
1.35 |
reset_stats (); |
150 |
|
|
|
151 |
root |
1.34 |
clients.insert (this); |
152 |
elmex |
1.1 |
} |
153 |
|
|
|
154 |
root |
1.20 |
client::~client () |
155 |
root |
1.16 |
{ |
156 |
root |
1.34 |
clients.erase (this); |
157 |
root |
1.32 |
|
158 |
root |
1.74 |
while (!ixface.empty ()) |
159 |
|
|
ix_pop (); |
160 |
|
|
|
161 |
root |
1.51 |
mapinfo_queue_clear (); |
162 |
root |
1.22 |
free (stats.range); |
163 |
|
|
free (stats.title); |
164 |
|
|
free (host); |
165 |
root |
1.73 |
free (ws_inbuf); |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
void |
169 |
|
|
client::run () |
170 |
|
|
{ |
171 |
|
|
// initialisation done, kick it! |
172 |
|
|
INVOKE_CLIENT (CONNECT, this); |
173 |
|
|
|
174 |
|
|
flush (); |
175 |
root |
1.21 |
} |
176 |
|
|
|
177 |
|
|
void |
178 |
root |
1.31 |
client::do_destroy () |
179 |
root |
1.21 |
{ |
180 |
root |
1.65 |
INVOKE_CLIENT (CLIENT_DESTROY, this); |
181 |
root |
1.31 |
attachable::do_destroy (); |
182 |
|
|
|
183 |
|
|
if (pl) |
184 |
|
|
pl->disconnect (); |
185 |
|
|
|
186 |
|
|
if (fd >= 0) |
187 |
root |
1.34 |
{ |
188 |
|
|
send_packet ("goodbye"); |
189 |
|
|
flush (); |
190 |
|
|
|
191 |
|
|
close (fd); |
192 |
|
|
} |
193 |
root |
1.29 |
|
194 |
root |
1.28 |
state = ST_DEAD; |
195 |
root |
1.27 |
|
196 |
root |
1.54 |
socket_ev.stop (); |
197 |
root |
1.31 |
|
198 |
root |
1.34 |
refcnt_dec (); // socket no longer open |
199 |
root |
1.16 |
} |
200 |
elmex |
1.1 |
|
201 |
root |
1.35 |
void |
202 |
|
|
client::reset_stats () |
203 |
|
|
{ |
204 |
|
|
/* we need to clear these to -1 and not zero - otherwise, |
205 |
|
|
* if a player quits and starts a new character, we wont |
206 |
|
|
* send new values to the client, as things like exp start |
207 |
|
|
* at zero. |
208 |
|
|
*/ |
209 |
root |
1.69 |
for (int i = 0; i < array_length (last_skill_exp); i++) |
210 |
root |
1.35 |
last_skill_exp[i] = -1; |
211 |
|
|
|
212 |
root |
1.69 |
for (int i = 0; i < array_length (last_resist); i++) |
213 |
root |
1.35 |
last_resist[i] = -1; |
214 |
|
|
|
215 |
root |
1.68 |
last_weapon_sp = -1; |
216 |
|
|
last_level = -1; |
217 |
|
|
last_stats.exp = -1; |
218 |
|
|
last_flags = 0; |
219 |
|
|
last_weight = -1; |
220 |
|
|
last_weight_limit = 0; |
221 |
|
|
last_path_attuned = 0; |
222 |
root |
1.35 |
last_path_repelled = 0; |
223 |
root |
1.68 |
last_path_denied = 0; |
224 |
|
|
last_speed = 0; |
225 |
|
|
last_flags = 0; |
226 |
root |
1.35 |
|
227 |
|
|
static living zero_living; |
228 |
|
|
last_stats = zero_living; |
229 |
|
|
} |
230 |
|
|
|
231 |
root |
1.27 |
client * |
232 |
|
|
client::create (int fd, const char *peername) |
233 |
root |
1.16 |
{ |
234 |
root |
1.38 |
client *ns = new client (dup (fd), peername); |
235 |
root |
1.66 |
|
236 |
root |
1.38 |
ns->instantiate (); // effectively a nop right now |
237 |
root |
1.66 |
|
238 |
root |
1.38 |
return ns; |
239 |
root |
1.16 |
} |
240 |
|
|
|