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 |
… | |
… | |
42 | #include <netinet/ip.h> |
43 | #include <netinet/ip.h> |
43 | #include <netdb.h> |
44 | #include <netdb.h> |
44 | |
45 | |
45 | #include <unistd.h> |
46 | #include <unistd.h> |
46 | #include <arpa/inet.h> |
47 | #include <arpa/inet.h> |
|
|
48 | |
|
|
49 | #include <algorithm> |
47 | |
50 | |
48 | sockvec clients; |
51 | sockvec clients; |
49 | |
52 | |
50 | /** |
53 | /** |
51 | * Initializes a connection. Really, it just sets up the data structure, |
54 | * Initializes a connection. Really, it just sets up the data structure, |
… | |
… | |
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->ns = 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 | { |
|
|
181 | attachable::do_destroy (); |
|
|
182 | |
|
|
183 | if (pl) |
|
|
184 | pl->disconnect (); |
|
|
185 | |
|
|
186 | if (fd >= 0) |
|
|
187 | close (fd); |
|
|
188 | |
176 | state = ST_DEAD; |
189 | state = ST_DEAD; |
177 | |
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) |
… | |
… | |
202 | void |
218 | void |
203 | free_all_newserver (void) |
219 | free_all_newserver (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 | } |
|
|
208 | |
|
|
209 | /** Sends the 'goodbye' command to the player, and closes connection. */ |
|
|
210 | void |
|
|
211 | final_free_player (player *pl) |
|
|
212 | { |
|
|
213 | free_player (pl); |
|
|
214 | } |
223 | } |
215 | |
224 | |
216 | client * |
225 | client * |
217 | client::create (int fd, const char *peername) |
226 | client::create (int fd, const char *peername) |
218 | { |
227 | { |
… | |
… | |
222 | return 0; |
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); |
|
|
228 | |
236 | |
229 | ns->instantiate (); // effectively a nop right now |
237 | ns->instantiate (); // effectively a nop right now |
230 | |
238 | |
231 | INVOKE_CLIENT (CONNECT, ns); |
239 | INVOKE_CLIENT (CONNECT, ns); |
232 | |
240 | |