… | |
… | |
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 | */ |
… | |
… | |
123 | {"toggleextendedtext", SC(ToggleExtendedText) PF_IMMEDIATE }, /*Added: tchize */ |
122 | {"toggleextendedtext", SC(ToggleExtendedText) PF_IMMEDIATE }, /*Added: tchize */ |
124 | {"asksmooth", SC(AskSmooth) 0 }, /*Added: tchize (smoothing technologies) */ |
123 | {"asksmooth", SC(AskSmooth) 0 }, /*Added: tchize (smoothing technologies) */ |
125 | }; |
124 | }; |
126 | |
125 | |
127 | /** |
126 | /** |
128 | * RequestInfo is sort of a meta command. There is some specific |
|
|
129 | * request of information, but we call other functions to provide |
|
|
130 | * that information. |
|
|
131 | */ |
|
|
132 | void |
|
|
133 | RequestInfo (char *buf, int len, client_socket * ns) |
|
|
134 | { |
|
|
135 | char *params = NULL, *cp; |
|
|
136 | |
|
|
137 | /* No match */ |
|
|
138 | char bigbuf[MAX_BUF]; |
|
|
139 | int slen; |
|
|
140 | |
|
|
141 | /* Set up replyinfo before we modify any of the buffers - this is used |
|
|
142 | * if we don't find a match. |
|
|
143 | */ |
|
|
144 | strcpy (bigbuf, "replyinfo "); |
|
|
145 | slen = strlen (bigbuf); |
|
|
146 | safe_strcat (bigbuf, buf, &slen, MAX_BUF); |
|
|
147 | |
|
|
148 | /* find the first space, make it null, and update the |
|
|
149 | * params pointer. |
|
|
150 | */ |
|
|
151 | for (cp = buf; *cp != '\0'; cp++) |
|
|
152 | if (*cp == ' ') |
|
|
153 | { |
|
|
154 | *cp = '\0'; |
|
|
155 | params = cp + 1; |
|
|
156 | break; |
|
|
157 | } |
|
|
158 | |
|
|
159 | if (!strcmp (buf, "image_info")) |
|
|
160 | send_image_info (ns, params); |
|
|
161 | else if (!strcmp (buf, "image_sums")) |
|
|
162 | send_image_sums (ns, params); |
|
|
163 | else if (!strcmp (buf, "skill_info")) |
|
|
164 | send_skill_info (ns, params); |
|
|
165 | else if (!strcmp (buf, "spell_paths")) |
|
|
166 | send_spell_paths (ns, params); |
|
|
167 | else |
|
|
168 | ns->send_packet (bigbuf, len); |
|
|
169 | } |
|
|
170 | |
|
|
171 | /** |
|
|
172 | * Handle client input. |
127 | * Handle client input. |
173 | * |
128 | * |
174 | * HandleClient is actually not named really well - we only get here once |
129 | * HandleClient is actually not named really well - we only get here once |
175 | * 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. |
176 | * sock is the output socket information. pl is the player associated |
131 | * sock is the output socket information. pl is the player associated |
177 | * 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 |
178 | * starting a connection) |
133 | * starting a connection) |
179 | */ |
134 | */ |
180 | |
|
|
181 | void |
135 | void |
182 | HandleClient (client_socket *ns, player *pl) |
136 | HandleClient (client *ns, player *pl) |
183 | { |
137 | { |
184 | /* Loop through this - maybe we have several complete packets here. */ |
138 | /* Loop through this - maybe we have several complete packets here. */ |
185 | // 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 |
186 | for (int repeat = 16; repeat--;) |
140 | for (int repeat = 16; repeat--;) |
187 | { |
141 | { |
… | |
… | |
251 | } |
205 | } |
252 | |
206 | |
253 | void |
207 | void |
254 | flush_sockets (void) |
208 | flush_sockets (void) |
255 | { |
209 | { |
256 | for (sockvec::iterator i = client_sockets.begin (); i != client_sockets.end (); ++i) |
210 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
257 | if ((*i)->status != Ns_Dead) |
211 | if ((*i)->status != Ns_Dead) |
258 | (*i)->flush (); |
212 | (*i)->flush (); |
259 | } |
213 | } |
260 | |
214 | |
261 | /** |
215 | /** |
… | |
… | |
306 | pl = npl; |
260 | pl = npl; |
307 | } |
261 | } |
308 | |
262 | |
309 | FD_ZERO (&tmp_read); |
263 | FD_ZERO (&tmp_read); |
310 | |
264 | |
311 | for (sockvec::iterator i = client_sockets.begin (); i != client_sockets.end (); ) |
265 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ) |
312 | { |
266 | { |
313 | client_socket *s = *i; |
267 | client *s = *i; |
314 | |
268 | |
315 | if (s->status == Ns_Dead) |
269 | if (s->status == Ns_Dead) |
316 | { |
270 | { |
317 | client_sockets.erase (i); |
271 | clients.erase (i); |
318 | delete s; |
272 | delete s; |
319 | } |
273 | } |
320 | else |
274 | else |
321 | { |
275 | { |
322 | if (s->fd > maxfd) maxfd = s->fd; |
276 | if (s->fd > maxfd) maxfd = s->fd; |
… | |
… | |
343 | } |
297 | } |
344 | |
298 | |
345 | /* We need to do some of the processing below regardless */ |
299 | /* We need to do some of the processing below regardless */ |
346 | |
300 | |
347 | /* Check for any input on the sockets */ |
301 | /* Check for any input on the sockets */ |
348 | for (sockvec::iterator i = client_sockets.begin (); i != client_sockets.end (); ++i) |
302 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
349 | { |
303 | { |
350 | client_socket *s = *i; |
304 | client *s = *i; |
351 | player *pl = s->pl; |
305 | player *pl = s->pl; |
352 | |
306 | |
353 | //TODO: disassociate handleclient from socket readin |
307 | //TODO: disassociate handleclient from socket readin |
354 | if (s->inbuf_len || FD_ISSET (s->fd, &tmp_read)) |
308 | if (s->inbuf_len || FD_ISSET (s->fd, &tmp_read)) |
355 | HandleClient (s, pl); |
309 | HandleClient (s, pl); |