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, |
… | |
… | |
56 | : fd (fd), host (strdup (peername)), |
59 | : fd (fd), host (strdup (peername)), |
57 | socket_ev (this, &client::socket_cb), |
60 | socket_ev (this, &client::socket_cb), |
58 | cmd_ev (this, &client::cmd_cb), |
61 | cmd_ev (this, &client::cmd_cb), |
59 | cc_inv (this), cc_other (this) |
62 | cc_inv (this), cc_other (this) |
60 | { |
63 | { |
|
|
64 | refcnt_inc (); // the socket is an external reference |
|
|
65 | |
61 | { |
66 | { |
62 | struct linger linger_opt; |
67 | struct linger linger_opt; |
63 | |
68 | |
64 | linger_opt.l_onoff = 0; |
69 | linger_opt.l_onoff = 0; |
65 | linger_opt.l_linger = 0; |
70 | linger_opt.l_linger = 0; |
… | |
… | |
147 | |
152 | |
148 | // initialisation done, kick it! |
153 | // initialisation done, kick it! |
149 | send_packet_printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); |
154 | send_packet_printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); |
150 | flush (); |
155 | flush (); |
151 | |
156 | |
152 | #if 0//TODO |
157 | clients.insert (this); |
153 | socket_info.nconns++; |
|
|
154 | 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; |
|
|
156 | #endif |
|
|
157 | } |
158 | } |
158 | |
159 | |
159 | client::~client () |
160 | client::~client () |
160 | { |
161 | { |
161 | if (pl) |
162 | clients.erase (this); |
162 | pl->ns = 0; |
|
|
163 | |
|
|
164 | if (fd >= 0) |
|
|
165 | close (fd); |
|
|
166 | |
163 | |
167 | sfree<uint8> (faces_sent, nrofpixmaps); |
164 | sfree<uint8> (faces_sent, nrofpixmaps); |
168 | free (stats.range); |
165 | free (stats.range); |
169 | free (stats.title); |
166 | free (stats.title); |
170 | free (host); |
167 | free (host); |
171 | } |
168 | } |
172 | |
169 | |
173 | void |
170 | void |
174 | client::destroy () |
171 | client::do_destroy () |
175 | { |
172 | { |
176 | INVOKE_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 | } |
177 | |
185 | |
178 | state = ST_DEAD; |
186 | state = ST_DEAD; |
179 | |
187 | |
180 | socket_ev.suspend (); |
188 | socket_ev.suspend (); |
181 | cmd_ev.suspend (); |
189 | cmd_ev.suspend (); |
|
|
190 | |
|
|
191 | refcnt_dec (); // socket no longer open |
182 | } |
192 | } |
183 | |
193 | |
184 | /** This sets up the socket and reads all the image information into memory. */ |
194 | /** This sets up the socket and reads all the image information into memory. */ |
185 | void |
195 | void |
186 | init_ericserver (void) |
196 | init_ericserver (void) |
… | |
… | |
204 | void |
214 | void |
205 | free_all_newserver (void) |
215 | free_all_newserver (void) |
206 | { |
216 | { |
207 | LOG (llevDebug, "Freeing all new client/server information.\n"); |
217 | LOG (llevDebug, "Freeing all new client/server information.\n"); |
208 | free_socket_images (); |
218 | free_socket_images (); |
209 | } |
|
|
210 | |
|
|
211 | /** Sends the 'goodbye' command to the player, and closes connection. */ |
|
|
212 | void |
|
|
213 | final_free_player (player *pl) |
|
|
214 | { |
|
|
215 | free_player (pl); |
|
|
216 | } |
219 | } |
217 | |
220 | |
218 | client * |
221 | client * |
219 | client::create (int fd, const char *peername) |
222 | client::create (int fd, const char *peername) |
220 | { |
223 | { |
… | |
… | |
224 | return 0; |
227 | return 0; |
225 | } |
228 | } |
226 | else |
229 | else |
227 | { |
230 | { |
228 | client *ns = new client (dup (fd), peername); |
231 | client *ns = new client (dup (fd), peername); |
229 | clients.push_back (ns); |
|
|
230 | |
232 | |
231 | ns->instantiate (); // effectively a nop right now |
233 | ns->instantiate (); // effectively a nop right now |
232 | |
234 | |
233 | INVOKE_CLIENT (CONNECT, ns); |
235 | INVOKE_CLIENT (CONNECT, ns); |
234 | |
236 | |