ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/init.C
Revision: 1.78
Committed: Sat Dec 1 20:22:13 2018 UTC (5 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 1.77: +2 -2 lines
Log Message:
slight cleanup

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2017,2018 Marc Alexander Lehmann / the Deliantra team
5 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
6 *
7 * 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 *
12 * 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 *
17 * 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 *
21 * The authors can be reached via e-mail to <support@deliantra.net>
22 */
23
24 /*
25 * Socket general functions
26 */
27
28 #include <global.h>
29 #include <sproto.h>
30 #include <sys/types.h>
31 #include <sys/time.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <netinet/tcp.h>
35 #include <netinet/ip.h>
36 #include <netdb.h>
37
38 #include <unistd.h>
39 #include <arpa/inet.h>
40
41 #include <algorithm>
42
43 // 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 sockvec clients;
50
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 client::client (int fd, const char *peername)
57 : fd (fd), host (strdup (peername)),
58 socket_ev (this, &client::socket_cb)
59 {
60 refcnt_inc (); // the socket is an external reference
61
62 mss = 1500 - 52; // 1500 typical ethernet frame, 66 typical tcp header overhead
63
64 socket_timeout = 16.;
65
66 {
67 struct linger linger_opt;
68
69 linger_opt.l_onoff = 0;
70 linger_opt.l_linger = 0;
71
72 if (setsockopt (fd, SOL_SOCKET, SO_LINGER, &linger_opt, sizeof (struct linger)))
73 LOG (llevError, "SO_LINGER: %s\n", strerror (errno));
74 }
75
76 {
77 int val;
78
79 #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 #ifdef IP_TOS
92 val = IPTOS_LOWDELAY;
93 if (setsockopt (fd, IPPROTO_IP, IP_TOS, &val, sizeof (val)))
94 LOG (llevError, "IP_TOS: %s\n", strerror (errno));
95 #endif
96
97 #ifdef TCP_NODELAY
98 val = 1;
99 if (setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val)))
100 LOG (llevError, "TCP_NODELAY: %s\n", strerror (errno));
101 #endif
102
103 // set some very aggressive keepalive parameters
104 #ifdef TCP_KEEPIDLE
105 val = 1;
106 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 val = 3;
112 if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof (val)))
113 LOG (llevError, "TCP_KEEPCNT: %s\n", strerror (errno));
114 #endif
115
116 #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
122 // 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 }
129
130 if (fcntl (fd, F_SETFL, O_NONBLOCK) == -1)
131 LOG (llevError, "InitConnection: Error on fcntl.\n");
132
133 state = ST_SETUP;
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 set meta data
141 * for following faces in fx.
142 */
143 faces_sent[0] = true;
144 fx_want [FT_IMAGE] = 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 reset_stats ();
151
152 clients.insert (this);
153 }
154
155 client::~client ()
156 {
157 clients.erase (this);
158
159 while (!ixface.empty ())
160 ix_pop ();
161
162 mapinfo_queue_clear ();
163 free (stats.range);
164 free (stats.title);
165 free (host);
166 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 }
177
178 void
179 client::do_destroy ()
180 {
181 INVOKE_CLIENT (CLIENT_DESTROY, this);
182 attachable::do_destroy ();
183
184 if (pl)
185 pl->disconnect ();
186
187 if (fd >= 0)
188 {
189 send_packet ("goodbye");
190 flush ();
191
192 close (fd);
193 }
194
195 state = ST_DEAD;
196
197 socket_ev.stop ();
198
199 refcnt_dec (); // socket no longer open
200 }
201
202 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 for (int i = 0; i < ecb_array_length (last_skill_exp); i++)
211 last_skill_exp[i] = -1;
212
213 for (int i = 0; i < ecb_array_length (last_resist); i++)
214 last_resist[i] = -1;
215
216 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 last_path_repelled = 0;
224 last_path_denied = 0;
225 last_speed = 0;
226 last_flags = 0;
227
228 static living zero_living;
229 last_stats = zero_living;
230 }
231
232 client *
233 client::create (int fd, const char *peername)
234 {
235 client *ns = new client (dup (fd), peername);
236
237 ns->instantiate (); // effectively a nop right now
238
239 return ns;
240 }
241