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