ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/init.C
Revision: 1.76
Committed: Wed Nov 16 23:42:03 2016 UTC (7 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.75: +1 -1 lines
Log Message:
copyright update 2016

File Contents

# Content
1 /*
2 * This file is part of Deliantra, the Roguelike Realtime MMORPG.
3 *
4 * Copyright (©) 2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015,2016 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 mapx = 11;
134 mapy = 11;
135 itemcmd = 1; /* Default is version item1 command */
136 max_rate = 100000 / (1000000 / MAX_TIME); // ~1mbit is assumed per default
137
138 /* Do this so we don't send a face command for the client for
139 * this face. Face 0 is sent to the client to set meta data
140 * for following faces in fx.
141 */
142 faces_sent[0] = true;
143 fx_want [FT_IMAGE] = true; // all clients must support image faces
144
145 socket_ev.set (fd, EV_READ);
146 socket_ev.prio (2); // one higher than the ticker priority
147 socket_ev.start ();
148
149 reset_stats ();
150
151 clients.insert (this);
152 }
153
154 client::~client ()
155 {
156 clients.erase (this);
157
158 while (!ixface.empty ())
159 ix_pop ();
160
161 mapinfo_queue_clear ();
162 free (stats.range);
163 free (stats.title);
164 free (host);
165 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 }
176
177 void
178 client::do_destroy ()
179 {
180 INVOKE_CLIENT (CLIENT_DESTROY, this);
181 attachable::do_destroy ();
182
183 if (pl)
184 pl->disconnect ();
185
186 if (fd >= 0)
187 {
188 send_packet ("goodbye");
189 flush ();
190
191 close (fd);
192 }
193
194 state = ST_DEAD;
195
196 socket_ev.stop ();
197
198 refcnt_dec (); // socket no longer open
199 }
200
201 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 for (int i = 0; i < array_length (last_skill_exp); i++)
210 last_skill_exp[i] = -1;
211
212 for (int i = 0; i < array_length (last_resist); i++)
213 last_resist[i] = -1;
214
215 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 last_path_repelled = 0;
223 last_path_denied = 0;
224 last_speed = 0;
225 last_flags = 0;
226
227 static living zero_living;
228 last_stats = zero_living;
229 }
230
231 client *
232 client::create (int fd, const char *peername)
233 {
234 client *ns = new client (dup (fd), peername);
235
236 ns->instantiate (); // effectively a nop right now
237
238 return ns;
239 }
240