ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/init.C
Revision: 1.72
Committed: Mon Oct 29 23:55:57 2012 UTC (11 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.71: +5 -5 lines
Log Message:
trailing space removal

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 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 say clear
140 * face information.
141 */
142 faces_sent[0] = true;
143 fx_want [FT_FACE] = 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 // initialisation done, kick it!
150 INVOKE_CLIENT (CONNECT, this);
151
152 flush ();
153
154 reset_stats ();
155
156 clients.insert (this);
157 }
158
159 client::~client ()
160 {
161 clients.erase (this);
162
163 mapinfo_queue_clear ();
164 free (stats.range);
165 free (stats.title);
166 free (host);
167 }
168
169 void
170 client::do_destroy ()
171 {
172 INVOKE_CLIENT (CLIENT_DESTROY, this);
173 attachable::do_destroy ();
174
175 if (pl)
176 pl->disconnect ();
177
178 if (fd >= 0)
179 {
180 send_packet ("goodbye");
181 flush ();
182
183 close (fd);
184 }
185
186 state = ST_DEAD;
187
188 socket_ev.stop ();
189
190 refcnt_dec (); // socket no longer open
191 }
192
193 void
194 client::reset_stats ()
195 {
196 /* we need to clear these to -1 and not zero - otherwise,
197 * if a player quits and starts a new character, we wont
198 * send new values to the client, as things like exp start
199 * at zero.
200 */
201 for (int i = 0; i < array_length (last_skill_exp); i++)
202 last_skill_exp[i] = -1;
203
204 for (int i = 0; i < array_length (last_resist); i++)
205 last_resist[i] = -1;
206
207 last_weapon_sp = -1;
208 last_level = -1;
209 last_stats.exp = -1;
210 last_flags = 0;
211 last_weight = -1;
212 last_weight_limit = 0;
213 last_path_attuned = 0;
214 last_path_repelled = 0;
215 last_path_denied = 0;
216 last_speed = 0;
217 last_flags = 0;
218
219 static living zero_living;
220 last_stats = zero_living;
221 }
222
223 client *
224 client::create (int fd, const char *peername)
225 {
226 client *ns = new client (dup (fd), peername);
227
228 ns->instantiate (); // effectively a nop right now
229
230 return ns;
231 }
232