… | |
… | |
29 | * |
29 | * |
30 | * Mainly deals with initialization and higher level socket |
30 | * Mainly deals with initialization and higher level socket |
31 | * maintenance (checking for lost connections and if data has arrived.) |
31 | * maintenance (checking for lost connections and if data has arrived.) |
32 | * The reading of data is handled in ericserver.c |
32 | * The reading of data is handled in ericserver.c |
33 | */ |
33 | */ |
34 | |
|
|
35 | |
34 | |
36 | #include <global.h> |
35 | #include <global.h> |
37 | #include <sproto.h> |
36 | #include <sproto.h> |
38 | #include <sys/types.h> |
37 | #include <sys/types.h> |
39 | #include <sys/time.h> |
38 | #include <sys/time.h> |
… | |
… | |
43 | #include <netinet/ip.h> |
42 | #include <netinet/ip.h> |
44 | #include <netdb.h> |
43 | #include <netdb.h> |
45 | |
44 | |
46 | #include <unistd.h> |
45 | #include <unistd.h> |
47 | #include <arpa/inet.h> |
46 | #include <arpa/inet.h> |
|
|
47 | |
|
|
48 | #include <algorithm> |
48 | |
49 | |
49 | sockvec clients; |
50 | sockvec clients; |
50 | |
51 | |
51 | /** |
52 | /** |
52 | * Initializes a connection. Really, it just sets up the data structure, |
53 | * Initializes a connection. Really, it just sets up the data structure, |
… | |
… | |
119 | |
120 | |
120 | buflen = sizeof (oldbufsize); |
121 | buflen = sizeof (oldbufsize); |
121 | getsockopt (fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen); |
122 | getsockopt (fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen); |
122 | } |
123 | } |
123 | |
124 | |
|
|
125 | state = ST_SETUP; |
124 | mapmode = Map0Cmd; |
126 | mapmode = Map0Cmd; |
125 | darkness = 1; |
127 | darkness = 1; |
126 | status = Ns_Add; |
|
|
127 | mapx = 11; |
128 | mapx = 11; |
128 | mapy = 11; |
129 | mapy = 11; |
129 | itemcmd = 1; /* Default is version item1 command */ |
130 | itemcmd = 1; /* Default is version item1 command */ |
130 | |
131 | |
131 | faces_sent = salloc<uint8> (nrofpixmaps, 0); |
132 | faces_sent = salloc0<uint8> (nrofpixmaps); |
132 | |
133 | |
133 | /* Do this so we don't send a face command for the client for |
134 | /* Do this so we don't send a face command for the client for |
134 | * this face. Face 0 is sent to the client to say clear |
135 | * this face. Face 0 is sent to the client to say clear |
135 | * face information. |
136 | * face information. |
136 | */ |
137 | */ |
… | |
… | |
148 | |
149 | |
149 | // initialisation done, kick it! |
150 | // initialisation done, kick it! |
150 | 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); |
151 | flush (); |
152 | flush (); |
152 | |
153 | |
|
|
154 | // clients are externally referenced by the socket |
|
|
155 | refcnt_inc (); |
|
|
156 | clients.push_back (this); |
|
|
157 | |
153 | #if 0//TODO |
158 | #if 0//TODO |
154 | socket_info.nconns++; |
159 | socket_info.nconns++; |
155 | 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; |
156 | 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; |
157 | #endif |
162 | #endif |
158 | } |
163 | } |
159 | |
164 | |
160 | client::~client () |
165 | client::~client () |
161 | { |
166 | { |
162 | if (pl) |
|
|
163 | pl->socket = 0; |
|
|
164 | |
|
|
165 | if (fd >= 0) |
|
|
166 | close (fd); |
|
|
167 | |
|
|
168 | sfree<uint8> (faces_sent, nrofpixmaps); |
167 | sfree<uint8> (faces_sent, nrofpixmaps); |
169 | free (stats.range); |
168 | free (stats.range); |
170 | free (stats.title); |
169 | free (stats.title); |
171 | free (host); |
170 | free (host); |
172 | } |
171 | } |
173 | |
172 | |
174 | void |
173 | void |
175 | client::destroy () |
174 | client::do_destroy () |
176 | { |
175 | { |
177 | status = Ns_Dead; |
176 | attachable::do_destroy (); |
|
|
177 | |
|
|
178 | sockvec::iterator i = find (clients.begin (), clients.end (), this); |
|
|
179 | if (i != clients.end ()) |
|
|
180 | clients.erase (i); |
|
|
181 | |
|
|
182 | if (pl) |
|
|
183 | pl->disconnect (); |
|
|
184 | |
|
|
185 | if (fd >= 0) |
|
|
186 | close (fd); |
|
|
187 | |
|
|
188 | state = ST_DEAD; |
|
|
189 | |
178 | socket_ev.suspend (); |
190 | socket_ev.suspend (); |
179 | cmd_ev.suspend (); |
191 | cmd_ev.suspend (); |
|
|
192 | |
|
|
193 | // socket now longer references us |
|
|
194 | refcnt_dec (); |
180 | } |
195 | } |
181 | |
196 | |
182 | /** 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. */ |
183 | void |
198 | void |
184 | init_ericserver (void) |
199 | init_ericserver (void) |
… | |
… | |
204 | { |
219 | { |
205 | LOG (llevDebug, "Freeing all new client/server information.\n"); |
220 | LOG (llevDebug, "Freeing all new client/server information.\n"); |
206 | free_socket_images (); |
221 | free_socket_images (); |
207 | } |
222 | } |
208 | |
223 | |
209 | /** Sends the 'goodbye' command to the player, and closes connection. */ |
224 | client * |
210 | void |
|
|
211 | final_free_player (player *pl) |
|
|
212 | { |
|
|
213 | free_player (pl); |
|
|
214 | } |
|
|
215 | |
|
|
216 | bool |
|
|
217 | add_client (int fd, const char *peername) |
225 | client::create (int fd, const char *peername) |
218 | { |
226 | { |
219 | if (checkbanned (NULL, peername)) |
227 | if (checkbanned (NULL, peername)) |
220 | { |
228 | { |
221 | LOG (llevInfo, "Banned host tried to connect: [%s]\n", peername); |
229 | LOG (llevInfo, "Banned host tried to connect: [%s]\n", peername); |
222 | return false; |
230 | return 0; |
223 | } |
231 | } |
224 | else |
232 | else |
225 | { |
233 | { |
226 | client *ns = new client (dup (fd), peername); |
234 | client *ns = new client (dup (fd), peername); |
227 | clients.push_back (ns); |
235 | |
|
|
236 | ns->instantiate (); // effectively a nop right now |
|
|
237 | |
|
|
238 | INVOKE_CLIENT (CONNECT, ns); |
|
|
239 | |
228 | return true; |
240 | return ns; |
229 | } |
241 | } |
230 | } |
242 | } |
231 | |
243 | |