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> |
… | |
… | |
43 | #include <netinet/ip.h> |
43 | #include <netinet/ip.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 | |
|
|
49 | #include <algorithm> |
48 | |
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, |
… | |
… | |
119 | |
121 | |
120 | buflen = sizeof (oldbufsize); |
122 | buflen = sizeof (oldbufsize); |
121 | getsockopt (fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen); |
123 | getsockopt (fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen); |
122 | } |
124 | } |
123 | |
125 | |
|
|
126 | state = ST_SETUP; |
124 | mapmode = Map0Cmd; |
127 | mapmode = Map0Cmd; |
125 | darkness = 1; |
128 | darkness = 1; |
126 | status = Ns_Add; |
|
|
127 | mapx = 11; |
129 | mapx = 11; |
128 | mapy = 11; |
130 | mapy = 11; |
129 | itemcmd = 1; /* Default is version item1 command */ |
131 | itemcmd = 1; /* Default is version item1 command */ |
130 | |
132 | |
131 | faces_sent = salloc<uint8> (nrofpixmaps, 0); |
133 | faces_sent = salloc0<uint8> (nrofpixmaps); |
132 | |
134 | |
133 | /* 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 |
134 | * 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 |
135 | * face information. |
137 | * face information. |
136 | */ |
138 | */ |
… | |
… | |
148 | |
150 | |
149 | // initialisation done, kick it! |
151 | // initialisation done, kick it! |
150 | 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); |
151 | flush (); |
153 | flush (); |
152 | |
154 | |
|
|
155 | // clients are externally referenced by the socket |
|
|
156 | refcnt_inc (); |
|
|
157 | clients.push_back (this); |
|
|
158 | |
153 | #if 0//TODO |
159 | #if 0//TODO |
154 | socket_info.nconns++; |
160 | socket_info.nconns++; |
155 | 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; |
156 | 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; |
157 | #endif |
163 | #endif |
158 | } |
164 | } |
159 | |
165 | |
160 | client::~client () |
166 | client::~client () |
161 | { |
167 | { |
162 | if (pl) |
168 | sockvec::iterator i = find (clients.begin (), clients.end (), this); |
163 | pl->socket = 0; |
169 | if (i != clients.end ()) |
164 | |
170 | clients.erase (i); |
165 | if (fd >= 0) |
|
|
166 | close (fd); |
|
|
167 | |
171 | |
168 | sfree<uint8> (faces_sent, nrofpixmaps); |
172 | sfree<uint8> (faces_sent, nrofpixmaps); |
169 | free (stats.range); |
173 | free (stats.range); |
170 | free (stats.title); |
174 | free (stats.title); |
171 | free (host); |
175 | free (host); |
172 | } |
176 | } |
173 | |
177 | |
174 | void |
178 | void |
175 | client::destroy () |
179 | client::do_destroy () |
176 | { |
180 | { |
177 | 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 | |
178 | socket_ev.suspend (); |
191 | socket_ev.suspend (); |
179 | cmd_ev.suspend (); |
192 | cmd_ev.suspend (); |
|
|
193 | |
|
|
194 | // socket now longer references us |
|
|
195 | refcnt_dec (); |
180 | } |
196 | } |
181 | |
197 | |
182 | /** 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. */ |
183 | void |
199 | void |
184 | init_ericserver (void) |
200 | init_ericserver (void) |
… | |
… | |
204 | { |
220 | { |
205 | LOG (llevDebug, "Freeing all new client/server information.\n"); |
221 | LOG (llevDebug, "Freeing all new client/server information.\n"); |
206 | free_socket_images (); |
222 | free_socket_images (); |
207 | } |
223 | } |
208 | |
224 | |
209 | /** Sends the 'goodbye' command to the player, and closes connection. */ |
225 | client * |
210 | void |
|
|
211 | final_free_player (player *pl) |
|
|
212 | { |
|
|
213 | free_player (pl); |
|
|
214 | } |
|
|
215 | |
|
|
216 | bool |
|
|
217 | add_client (int fd, const char *peername) |
226 | client::create (int fd, const char *peername) |
218 | { |
227 | { |
219 | if (checkbanned (NULL, peername)) |
228 | if (checkbanned (NULL, peername)) |
220 | { |
229 | { |
221 | LOG (llevInfo, "Banned host tried to connect: [%s]\n", peername); |
230 | LOG (llevInfo, "Banned host tried to connect: [%s]\n", peername); |
222 | return false; |
231 | return 0; |
223 | } |
232 | } |
224 | else |
233 | else |
225 | { |
234 | { |
226 | client *ns = new client (dup (fd), peername); |
235 | client *ns = new client (dup (fd), peername); |
227 | clients.push_back (ns); |
236 | |
|
|
237 | ns->instantiate (); // effectively a nop right now |
|
|
238 | |
|
|
239 | INVOKE_CLIENT (CONNECT, ns); |
|
|
240 | |
228 | return true; |
241 | return ns; |
229 | } |
242 | } |
230 | } |
243 | } |
231 | |
244 | |