1 | /* |
1 | /* |
2 | CrossFire, A Multiplayer game for X-windows |
2 | CrossFire, A Multiplayer game for X-windows |
3 | |
3 | |
|
|
4 | Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team |
4 | Copyright (C) 2001 Mark Wedel |
5 | Copyright (C) 2001 Mark Wedel |
5 | Copyright (C) 1992 Frank Tore Johansen |
6 | Copyright (C) 1992 Frank Tore Johansen |
6 | |
7 | |
7 | This program is free software; you can redistribute it and/or modify |
8 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by |
9 | it under the terms of the GNU General Public License as published by |
… | |
… | |
29 | * |
30 | * |
30 | * Mainly deals with initialization and higher level socket |
31 | * Mainly deals with initialization and higher level socket |
31 | * maintenance (checking for lost connections and if data has arrived.) |
32 | * maintenance (checking for lost connections and if data has arrived.) |
32 | * The reading of data is handled in ericserver.c |
33 | * The reading of data is handled in ericserver.c |
33 | */ |
34 | */ |
34 | |
|
|
35 | |
35 | |
36 | #include <global.h> |
36 | #include <global.h> |
37 | #include <sproto.h> |
37 | #include <sproto.h> |
38 | #include <sys/types.h> |
38 | #include <sys/types.h> |
39 | #include <sys/time.h> |
39 | #include <sys/time.h> |
… | |
… | |
44 | #include <netdb.h> |
44 | #include <netdb.h> |
45 | |
45 | |
46 | #include <unistd.h> |
46 | #include <unistd.h> |
47 | #include <arpa/inet.h> |
47 | #include <arpa/inet.h> |
48 | |
48 | |
|
|
49 | #include <algorithm> |
|
|
50 | |
49 | sockvec clients; |
51 | sockvec clients; |
50 | |
52 | |
51 | /** |
53 | /** |
52 | * Initializes a connection. Really, it just sets up the data structure, |
54 | * Initializes a connection. Really, it just sets up the data structure, |
53 | * socket setup is handled elsewhere. We do send a version to the |
55 | * socket setup is handled elsewhere. We do send a version to the |
54 | * client. |
56 | * client. |
55 | */ |
57 | */ |
56 | client::client (int fd, const char *peername) |
58 | client::client (int fd, const char *peername) |
57 | : fd (fd), host (strdup (peername)), |
59 | : fd (fd), host (strdup (peername)), |
58 | socket_ev (this, &client::socket_cb), |
60 | socket_ev (this, &client::socket_cb), |
59 | cmd_ev (this, &client::cmd_cb) |
61 | cmd_ev (this, &client::cmd_cb), |
|
|
62 | cc_inv (this), cc_other (this) |
60 | { |
63 | { |
61 | { |
64 | { |
62 | struct linger linger_opt; |
65 | struct linger linger_opt; |
63 | |
66 | |
64 | linger_opt.l_onoff = 0; |
67 | linger_opt.l_onoff = 0; |
… | |
… | |
118 | |
121 | |
119 | buflen = sizeof (oldbufsize); |
122 | buflen = sizeof (oldbufsize); |
120 | getsockopt (fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen); |
123 | getsockopt (fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen); |
121 | } |
124 | } |
122 | |
125 | |
|
|
126 | state = ST_SETUP; |
123 | mapmode = Map0Cmd; |
127 | mapmode = Map0Cmd; |
124 | darkness = 1; |
128 | darkness = 1; |
125 | status = Ns_Add; |
|
|
126 | mapx = 11; |
129 | mapx = 11; |
127 | mapy = 11; |
130 | mapy = 11; |
128 | itemcmd = 1; /* Default is version item1 command */ |
131 | itemcmd = 1; /* Default is version item1 command */ |
129 | |
132 | |
130 | faces_sent = salloc<uint8> (nrofpixmaps, 0); |
133 | faces_sent = salloc0<uint8> (nrofpixmaps); |
131 | |
134 | |
132 | /* Do this so we don't send a face command for the client for |
135 | /* Do this so we don't send a face command for the client for |
133 | * this face. Face 0 is sent to the client to say clear |
136 | * this face. Face 0 is sent to the client to say clear |
134 | * face information. |
137 | * face information. |
135 | */ |
138 | */ |
… | |
… | |
147 | |
150 | |
148 | // initialisation done, kick it! |
151 | // initialisation done, kick it! |
149 | send_packet_printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); |
152 | send_packet_printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); |
150 | flush (); |
153 | flush (); |
151 | |
154 | |
|
|
155 | // clients are externally referenced by the socket |
|
|
156 | refcnt_inc (); |
|
|
157 | clients.push_back (this); |
|
|
158 | |
152 | #if 0//TODO |
159 | #if 0//TODO |
153 | socket_info.nconns++; |
160 | socket_info.nconns++; |
154 | if (socket_info.nconns > cst_tot.max_conn) cst_tot.max_conn = socket_info.nconns; |
161 | if (socket_info.nconns > cst_tot.max_conn) cst_tot.max_conn = socket_info.nconns; |
155 | if (socket_info.nconns > cst_lst.max_conn) cst_lst.max_conn = socket_info.nconns; |
162 | if (socket_info.nconns > cst_lst.max_conn) cst_lst.max_conn = socket_info.nconns; |
156 | #endif |
163 | #endif |
157 | } |
164 | } |
158 | |
165 | |
159 | client::~client () |
166 | client::~client () |
160 | { |
167 | { |
161 | if (pl) |
168 | sockvec::iterator i = find (clients.begin (), clients.end (), this); |
162 | pl->socket = 0; |
169 | if (i != clients.end ()) |
163 | |
170 | clients.erase (i); |
164 | if (fd >= 0) |
|
|
165 | close (fd); |
|
|
166 | |
171 | |
167 | sfree<uint8> (faces_sent, nrofpixmaps); |
172 | sfree<uint8> (faces_sent, nrofpixmaps); |
168 | free (stats.range); |
173 | free (stats.range); |
169 | free (stats.title); |
174 | free (stats.title); |
170 | free (host); |
175 | free (host); |
171 | } |
176 | } |
172 | |
177 | |
173 | void |
178 | void |
174 | client::destroy () |
179 | client::do_destroy () |
175 | { |
180 | { |
176 | status = Ns_Dead; |
181 | attachable::do_destroy (); |
|
|
182 | |
|
|
183 | if (pl) |
|
|
184 | pl->disconnect (); |
|
|
185 | |
|
|
186 | if (fd >= 0) |
|
|
187 | close (fd); |
|
|
188 | |
|
|
189 | state = ST_DEAD; |
|
|
190 | |
177 | socket_ev.suspend (); |
191 | socket_ev.suspend (); |
178 | cmd_ev.suspend (); |
192 | cmd_ev.suspend (); |
|
|
193 | |
|
|
194 | // socket now longer references us |
|
|
195 | refcnt_dec (); |
179 | } |
196 | } |
180 | |
197 | |
181 | /** This sets up the socket and reads all the image information into memory. */ |
198 | /** This sets up the socket and reads all the image information into memory. */ |
182 | void |
199 | void |
183 | init_ericserver (void) |
200 | init_ericserver (void) |
… | |
… | |
203 | { |
220 | { |
204 | LOG (llevDebug, "Freeing all new client/server information.\n"); |
221 | LOG (llevDebug, "Freeing all new client/server information.\n"); |
205 | free_socket_images (); |
222 | free_socket_images (); |
206 | } |
223 | } |
207 | |
224 | |
208 | /** Sends the 'goodbye' command to the player, and closes connection. */ |
225 | client * |
209 | void |
|
|
210 | final_free_player (player *pl) |
|
|
211 | { |
|
|
212 | free_player (pl); |
|
|
213 | } |
|
|
214 | |
|
|
215 | bool |
|
|
216 | add_client (int fd, const char *peername) |
226 | client::create (int fd, const char *peername) |
217 | { |
227 | { |
218 | if (checkbanned (NULL, peername)) |
228 | if (checkbanned (NULL, peername)) |
219 | { |
229 | { |
220 | LOG (llevInfo, "Banned host tried to connect: [%s]\n", peername); |
230 | LOG (llevInfo, "Banned host tried to connect: [%s]\n", peername); |
221 | return false; |
231 | return 0; |
222 | } |
232 | } |
223 | else |
233 | else |
224 | { |
234 | { |
225 | client *ns = new client (dup (fd), peername); |
235 | client *ns = new client (dup (fd), peername); |
226 | clients.push_back (ns); |
236 | |
|
|
237 | ns->instantiate (); // effectively a nop right now |
|
|
238 | |
|
|
239 | INVOKE_CLIENT (CONNECT, ns); |
|
|
240 | |
227 | return true; |
241 | return ns; |
228 | } |
242 | } |
229 | } |
243 | } |
230 | |
244 | |