… | |
… | |
45 | |
45 | |
46 | #include <unistd.h> |
46 | #include <unistd.h> |
47 | #include <arpa/inet.h> |
47 | #include <arpa/inet.h> |
48 | |
48 | |
49 | #include <loader.h> |
49 | #include <loader.h> |
50 | #include <newserver.h> |
|
|
51 | |
50 | |
52 | /***************************************************************************** |
51 | /***************************************************************************** |
53 | * Start of command dispatch area. |
52 | * Start of command dispatch area. |
54 | * The commands here are protocol commands. |
53 | * The commands here are protocol commands. |
55 | ****************************************************************************/ |
54 | ****************************************************************************/ |
… | |
… | |
72 | { |
71 | { |
73 | const char *name; |
72 | const char *name; |
74 | void *cb; |
73 | void *cb; |
75 | int flags; |
74 | int flags; |
76 | |
75 | |
77 | bool may_execute (client_socket *ns) |
76 | bool may_execute (client *ns) |
78 | { |
77 | { |
79 | return (!(flags & PF_PLAYER) || ns->pl) |
78 | return (!(flags & PF_PLAYER) || ns->pl) |
80 | && (!(flags & PF_PLAYING) || (ns->pl && ns->pl->state == ST_PLAYING)); |
79 | && (!(flags & PF_PLAYING) || (ns->pl && ns->pl->state == ST_PLAYING)); |
81 | } |
80 | } |
82 | |
81 | |
83 | void execute (client_socket *ns, char *data, int datalen) |
82 | void execute (client *ns, char *data, int datalen) |
84 | { |
83 | { |
85 | //TODO: only one format |
84 | //TODO: only one format |
86 | if (flags & PF_PLAYER) |
85 | if (flags & PF_PLAYER) |
87 | ((void (*)(char *, int, player * ))cb)((char *)data, datalen, ns->pl); |
86 | ((void (*)(char *, int, player * ))cb)((char *)data, datalen, ns->pl); |
88 | else |
87 | else |
89 | ((void (*)(char *, int, client_socket *))cb)((char *)data, datalen, ns); |
88 | ((void (*)(char *, int, client *))cb)((char *)data, datalen, ns); |
90 | } |
89 | } |
91 | }; |
90 | }; |
92 | |
91 | |
93 | // SocketCommand, PlayingCommand, should not exist with those ugly casts |
92 | // SocketCommand, PlayingCommand, should not exist with those ugly casts |
94 | #define SC(cb) (void *)static_cast<void (*)(char *, int, client_socket *)>(cb), |
93 | #define SC(cb) (void *)static_cast<void (*)(char *, int, client *)>(cb), |
95 | #define PC(cb) (void *)static_cast<void (*)(char *, int, player *)>(cb), PF_PLAYER | |
94 | #define PC(cb) (void *)static_cast<void (*)(char *, int, player *)>(cb), PF_PLAYER | |
96 | |
95 | |
97 | /** |
96 | /** |
98 | * Dispatch table for the server. |
97 | * Dispatch table for the server. |
99 | */ |
98 | */ |
… | |
… | |
131 | * there is input, so we don't do exception or other stuff here. |
130 | * there is input, so we don't do exception or other stuff here. |
132 | * sock is the output socket information. pl is the player associated |
131 | * sock is the output socket information. pl is the player associated |
133 | * with this socket, null if no player (one of the init_sockets for just |
132 | * with this socket, null if no player (one of the init_sockets for just |
134 | * starting a connection) |
133 | * starting a connection) |
135 | */ |
134 | */ |
136 | |
|
|
137 | void |
135 | void |
138 | HandleClient (client_socket *ns, player *pl) |
136 | HandleClient (client *ns, player *pl) |
139 | { |
137 | { |
140 | /* Loop through this - maybe we have several complete packets here. */ |
138 | /* Loop through this - maybe we have several complete packets here. */ |
141 | // limit to a few commands only, though, as to not monopolise the server |
139 | // limit to a few commands only, though, as to not monopolise the server |
142 | for (int repeat = 16; repeat--;) |
140 | for (int repeat = 16; repeat--;) |
143 | { |
141 | { |
… | |
… | |
207 | } |
205 | } |
208 | |
206 | |
209 | void |
207 | void |
210 | flush_sockets (void) |
208 | flush_sockets (void) |
211 | { |
209 | { |
212 | for (sockvec::iterator i = client_sockets.begin (); i != client_sockets.end (); ++i) |
210 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
213 | if ((*i)->status != Ns_Dead) |
211 | if ((*i)->status != Ns_Dead) |
214 | (*i)->flush (); |
212 | (*i)->flush (); |
215 | } |
213 | } |
216 | |
214 | |
217 | /** |
215 | /** |
… | |
… | |
262 | pl = npl; |
260 | pl = npl; |
263 | } |
261 | } |
264 | |
262 | |
265 | FD_ZERO (&tmp_read); |
263 | FD_ZERO (&tmp_read); |
266 | |
264 | |
267 | for (sockvec::iterator i = client_sockets.begin (); i != client_sockets.end (); ) |
265 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ) |
268 | { |
266 | { |
269 | client_socket *s = *i; |
267 | client *s = *i; |
270 | |
268 | |
271 | if (s->status == Ns_Dead) |
269 | if (s->status == Ns_Dead) |
272 | { |
270 | { |
273 | client_sockets.erase (i); |
271 | clients.erase (i); |
274 | delete s; |
272 | delete s; |
275 | } |
273 | } |
276 | else |
274 | else |
277 | { |
275 | { |
278 | if (s->fd > maxfd) maxfd = s->fd; |
276 | if (s->fd > maxfd) maxfd = s->fd; |
… | |
… | |
299 | } |
297 | } |
300 | |
298 | |
301 | /* We need to do some of the processing below regardless */ |
299 | /* We need to do some of the processing below regardless */ |
302 | |
300 | |
303 | /* Check for any input on the sockets */ |
301 | /* Check for any input on the sockets */ |
304 | for (sockvec::iterator i = client_sockets.begin (); i != client_sockets.end (); ++i) |
302 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
305 | { |
303 | { |
306 | client_socket *s = *i; |
304 | client *s = *i; |
307 | player *pl = s->pl; |
305 | player *pl = s->pl; |
308 | |
306 | |
309 | //TODO: disassociate handleclient from socket readin |
307 | //TODO: disassociate handleclient from socket readin |
310 | if (s->inbuf_len || FD_ISSET (s->fd, &tmp_read)) |
308 | if (s->inbuf_len || FD_ISSET (s->fd, &tmp_read)) |
311 | HandleClient (s, pl); |
309 | HandleClient (s, pl); |