… | |
… | |
42 | #include <netinet/ip.h> |
42 | #include <netinet/ip.h> |
43 | #include <netdb.h> |
43 | #include <netdb.h> |
44 | |
44 | |
45 | #include <unistd.h> |
45 | #include <unistd.h> |
46 | #include <arpa/inet.h> |
46 | #include <arpa/inet.h> |
|
|
47 | |
|
|
48 | #include <algorithm> |
47 | |
49 | |
48 | sockvec clients; |
50 | sockvec clients; |
49 | |
51 | |
50 | /** |
52 | /** |
51 | * Initializes a connection. Really, it just sets up the data structure, |
53 | * Initializes a connection. Really, it just sets up the data structure, |
… | |
… | |
147 | |
149 | |
148 | // initialisation done, kick it! |
150 | // initialisation done, kick it! |
149 | send_packet_printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); |
151 | send_packet_printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); |
150 | flush (); |
152 | flush (); |
151 | |
153 | |
|
|
154 | // clients are externally referenced by the socket |
|
|
155 | refcnt_inc (); |
|
|
156 | clients.push_back (this); |
|
|
157 | |
152 | #if 0//TODO |
158 | #if 0//TODO |
153 | socket_info.nconns++; |
159 | socket_info.nconns++; |
154 | if (socket_info.nconns > cst_tot.max_conn) cst_tot.max_conn = socket_info.nconns; |
160 | 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; |
161 | if (socket_info.nconns > cst_lst.max_conn) cst_lst.max_conn = socket_info.nconns; |
156 | #endif |
162 | #endif |
157 | } |
163 | } |
158 | |
164 | |
159 | client::~client () |
165 | client::~client () |
160 | { |
166 | { |
161 | if (pl) |
167 | sockvec::iterator i = find (clients.begin (), clients.end (), this); |
162 | pl->ns = 0; |
168 | if (i != clients.end ()) |
163 | |
169 | clients.erase (i); |
164 | if (fd >= 0) |
|
|
165 | close (fd); |
|
|
166 | |
170 | |
167 | sfree<uint8> (faces_sent, nrofpixmaps); |
171 | sfree<uint8> (faces_sent, nrofpixmaps); |
168 | free (stats.range); |
172 | free (stats.range); |
169 | free (stats.title); |
173 | free (stats.title); |
170 | free (host); |
174 | free (host); |
171 | } |
175 | } |
172 | |
176 | |
173 | void |
177 | void |
174 | client::destroy () |
178 | client::do_destroy () |
175 | { |
179 | { |
176 | INVOKE_CLIENT (DESTROY, this); |
180 | attachable::do_destroy (); |
|
|
181 | |
|
|
182 | if (pl) |
|
|
183 | pl->disconnect (); |
|
|
184 | |
|
|
185 | if (fd >= 0) |
|
|
186 | close (fd); |
177 | |
187 | |
178 | state = ST_DEAD; |
188 | state = ST_DEAD; |
179 | |
189 | |
180 | socket_ev.suspend (); |
190 | socket_ev.suspend (); |
181 | cmd_ev.suspend (); |
191 | cmd_ev.suspend (); |
|
|
192 | |
|
|
193 | // socket now longer references us |
|
|
194 | refcnt_dec (); |
182 | } |
195 | } |
183 | |
196 | |
184 | /** This sets up the socket and reads all the image information into memory. */ |
197 | /** This sets up the socket and reads all the image information into memory. */ |
185 | void |
198 | void |
186 | init_ericserver (void) |
199 | init_ericserver (void) |
… | |
… | |
204 | void |
217 | void |
205 | free_all_newserver (void) |
218 | free_all_newserver (void) |
206 | { |
219 | { |
207 | LOG (llevDebug, "Freeing all new client/server information.\n"); |
220 | LOG (llevDebug, "Freeing all new client/server information.\n"); |
208 | free_socket_images (); |
221 | 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 | } |
222 | } |
217 | |
223 | |
218 | client * |
224 | client * |
219 | client::create (int fd, const char *peername) |
225 | client::create (int fd, const char *peername) |
220 | { |
226 | { |
… | |
… | |
224 | return 0; |
230 | return 0; |
225 | } |
231 | } |
226 | else |
232 | else |
227 | { |
233 | { |
228 | client *ns = new client (dup (fd), peername); |
234 | client *ns = new client (dup (fd), peername); |
229 | clients.push_back (ns); |
|
|
230 | |
235 | |
231 | ns->instantiate (); // effectively a nop right now |
236 | ns->instantiate (); // effectively a nop right now |
232 | |
237 | |
233 | INVOKE_CLIENT (CONNECT, ns); |
238 | INVOKE_CLIENT (CONNECT, ns); |
234 | |
239 | |