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