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 | { |
169 | if (i != clients.end ()) |
163 | pl->save (); |
170 | clients.erase (i); |
164 | leave (pl, 1); |
|
|
165 | pl->ns = 0; |
|
|
166 | |
|
|
167 | delete pl; |
|
|
168 | } |
|
|
169 | |
|
|
170 | if (fd >= 0) |
|
|
171 | close (fd); |
|
|
172 | |
171 | |
173 | sfree<uint8> (faces_sent, nrofpixmaps); |
172 | sfree<uint8> (faces_sent, nrofpixmaps); |
174 | free (stats.range); |
173 | free (stats.range); |
175 | free (stats.title); |
174 | free (stats.title); |
176 | free (host); |
175 | free (host); |
177 | } |
176 | } |
178 | |
177 | |
179 | void |
178 | void |
180 | client::destroy () |
179 | client::do_destroy () |
181 | { |
180 | { |
182 | INVOKE_CLIENT (DESTROY, this); |
181 | attachable::do_destroy (); |
|
|
182 | |
|
|
183 | if (pl) |
|
|
184 | pl->disconnect (); |
|
|
185 | |
|
|
186 | if (fd >= 0) |
|
|
187 | close (fd); |
183 | |
188 | |
184 | state = ST_DEAD; |
189 | state = ST_DEAD; |
185 | |
190 | |
186 | socket_ev.suspend (); |
191 | socket_ev.suspend (); |
187 | cmd_ev.suspend (); |
192 | cmd_ev.suspend (); |
|
|
193 | |
|
|
194 | // socket now longer references us |
|
|
195 | refcnt_dec (); |
188 | } |
196 | } |
189 | |
197 | |
190 | /** 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. */ |
191 | void |
199 | void |
192 | init_ericserver (void) |
200 | init_ericserver (void) |
… | |
… | |
223 | return 0; |
231 | return 0; |
224 | } |
232 | } |
225 | else |
233 | else |
226 | { |
234 | { |
227 | client *ns = new client (dup (fd), peername); |
235 | client *ns = new client (dup (fd), peername); |
228 | clients.push_back (ns); |
|
|
229 | |
236 | |
230 | ns->instantiate (); // effectively a nop right now |
237 | ns->instantiate (); // effectively a nop right now |
231 | |
238 | |
232 | INVOKE_CLIENT (CONNECT, ns); |
239 | INVOKE_CLIENT (CONNECT, ns); |
233 | |
240 | |