… | |
… | |
68 | * current state. we set up our on buffers for sending/receiving, so we can |
68 | * current state. we set up our on buffers for sending/receiving, so we can |
69 | * handle some higher level functions. fd is the actual file descriptor we |
69 | * handle some higher level functions. fd is the actual file descriptor we |
70 | * are using. |
70 | * are using. |
71 | */ |
71 | */ |
72 | |
72 | |
73 | enum Sock_Status { Ns_Add, Ns_Dead }; |
|
|
74 | |
|
|
75 | /* Only one map mode can actually be used, so lets make it a switch |
73 | /* Only one map mode can actually be used, so lets make it a switch |
76 | * instead of having a bunch of different fields that needed to |
74 | * instead of having a bunch of different fields that needed to |
77 | * get toggled. |
75 | * get toggled. |
78 | */ |
76 | */ |
79 | enum MapMode { Map0Cmd = 0, Map1Cmd = 1, Map1aCmd = 2 }; |
77 | enum MapMode { Map0Cmd = 0, Map1Cmd = 1, Map1aCmd = 2 }; |
… | |
… | |
85 | struct Buffer |
83 | struct Buffer |
86 | { |
84 | { |
87 | char data[SOCKETBUFSIZE]; |
85 | char data[SOCKETBUFSIZE]; |
88 | int start; |
86 | int start; |
89 | int len; |
87 | int len; |
|
|
88 | }; |
|
|
89 | |
|
|
90 | // states the socket can be in |
|
|
91 | enum { |
|
|
92 | ST_DEAD, // socket is dead |
|
|
93 | ST_SETUP, // initial handshake / setup / login |
|
|
94 | ST_PLAYING, // logged in an playing |
|
|
95 | ST_CUSTOM, // waiting for custom reply |
|
|
96 | |
|
|
97 | ST_PLAY_AGAIN, |
|
|
98 | ST_ROLL_STAT, |
|
|
99 | ST_CHANGE_CLASS, |
|
|
100 | ST_CONFIRM_QUIT, |
|
|
101 | ST_CONFIGURE, |
|
|
102 | ST_GET_NAME, |
|
|
103 | ST_GET_PASSWORD, |
|
|
104 | ST_CONFIRM_PASSWORD, |
|
|
105 | ST_GET_PARTY_PASSWORD, |
90 | }; |
106 | }; |
91 | |
107 | |
92 | // a handler for a specific type of packet |
108 | // a handler for a specific type of packet |
93 | enum { |
109 | enum { |
94 | PF_PLAYER = 0x01, // must have valid player / will by synchronised |
110 | PF_PLAYER = 0x01, // must have valid player / will by synchronised |
… | |
… | |
154 | |
170 | |
155 | /* how many times we are allowed to give the wrong password before being kicked. */ |
171 | /* how many times we are allowed to give the wrong password before being kicked. */ |
156 | #define MAX_PASSWORD_FAILURES 5 |
172 | #define MAX_PASSWORD_FAILURES 5 |
157 | |
173 | |
158 | ACC_CLASS (client) // should become player when newsocket is a baseclass of player |
174 | ACC_CLASS (client) // should become player when newsocket is a baseclass of player |
159 | struct client : zero_initialised, attachable_base |
175 | struct client : zero_initialised, attachable<client> |
160 | { |
176 | { |
161 | enum Sock_Status status; |
|
|
162 | int ACC (RW, fd); |
177 | int ACC (RW, fd); |
163 | unsigned int inbuf_len; // number of bytes valid in inbuf |
178 | unsigned int inbuf_len; // number of bytes valid in inbuf |
164 | uint8 *faces_sent; // This is a bitmap on sent face status |
179 | uint8 *faces_sent; // This is a bitmap on sent face status |
165 | statsinfo stats; |
180 | statsinfo stats; |
166 | |
181 | |
167 | client_container cc_inv, cc_other; |
182 | client_container cc_inv, cc_other; |
|
|
183 | Buffer outputbuffer; |
168 | |
184 | |
169 | char *ACC (RW, host); /* Which host it is connected from (ip address) */ |
185 | char *ACC (RW, host); /* Which host it is connected from (ip address) */ |
|
|
186 | uint8 ACC (RW, state); /* Input state of the player (name, password, etc */ |
170 | uint8 ACC (RW, password_fails); /* how many times the player has failed to give the right password */ |
187 | uint8 ACC (RW, password_fails); /* how many times the player has failed to give the right password */ |
171 | Buffer outputbuffer; |
|
|
172 | |
188 | |
173 | bool ACC (RW, facecache); /* If true, client is caching images */ |
189 | bool ACC (RW, facecache); /* If true, client is caching images */ |
174 | bool ACC (RW, sent_scroll); |
190 | bool ACC (RW, sent_scroll); |
175 | bool ACC (RW, sound); /* does the client want sound */ |
191 | bool ACC (RW, sound); /* does the client want sound */ |
176 | bool ACC (RW, exp64); /* Client wants 64 bit exp data, as well as skill data */ |
192 | bool ACC (RW, exp64); /* Client wants 64 bit exp data, as well as skill data */ |
… | |
… | |
208 | int ACC (RW, rtt), ACC (RW, rttvar); /* round-trip time and -variance, if known */ |
224 | int ACC (RW, rtt), ACC (RW, rttvar); /* round-trip time and -variance, if known */ |
209 | |
225 | |
210 | client (int fd, const char *from_ip); |
226 | client (int fd, const char *from_ip); |
211 | ~client (); |
227 | ~client (); |
212 | void destroy (); |
228 | void destroy (); |
|
|
229 | bool destroyed () const { return state == ST_DEAD; } |
213 | |
230 | |
214 | iw cmd_ev; void cmd_cb (iw &w); |
231 | iw cmd_ev; void cmd_cb (iw &w); |
215 | iow socket_ev; void socket_cb (iow &w, int got); |
232 | iow socket_ev; void socket_cb (iow &w, int got); |
216 | |
233 | |
217 | std::deque< command, slice_allocator<command> > cmd_queue; |
234 | std::deque< command, slice_allocator<command> > cmd_queue; |
218 | |
|
|
219 | // resets movement state |
|
|
220 | bool may_execute (const packet_type *pkt) const; |
|
|
221 | void execute (const packet_type *pkt, char *data, int datalen); |
|
|
222 | |
|
|
223 | void queue_command (packet_type *handler, char *data, int datalen); |
|
|
224 | bool handle_command (); |
|
|
225 | void reset_state (); |
|
|
226 | |
|
|
227 | bool handle_packet (); |
|
|
228 | int next_packet (); // returns length of packet or 0 |
|
|
229 | void skip_packet (int len); // we have processed the packet, skip it |
|
|
230 | |
|
|
231 | void flush (); |
|
|
232 | void write_outputbuffer (); |
|
|
233 | void send (void *buf_, int len); |
|
|
234 | |
|
|
235 | void send_packet (const char *buf); |
|
|
236 | void send_packet (const char *buf, int len); |
|
|
237 | void send_packet_printf (const char *format, ...); |
|
|
238 | void send_packet (packet &sl); |
|
|
239 | |
|
|
240 | // called when something under the player changes |
|
|
241 | void floorbox_update () { update_look = 1; } |
|
|
242 | // called when the player has been moved |
|
|
243 | void floorbox_reset () { look_position = 0; floorbox_update (); } |
|
|
244 | |
235 | |
245 | // large structures at the end please |
236 | // large structures at the end please |
246 | struct Map lastmap; |
237 | struct Map lastmap; |
247 | std::bitset<MAXANIMNUM> anims_sent; |
238 | std::bitset<MAXANIMNUM> anims_sent; |
248 | // if we get an incomplete packet, this is used to hold the data. |
239 | // if we get an incomplete packet, this is used to hold the data. |
249 | // we add 2 byte for the header, one for the trailing 0 byte |
240 | // we add 2 byte for the header, one for the trailing 0 byte |
250 | uint8 inbuf[MAXSOCKBUF + 2 + 1]; |
241 | uint8 inbuf[MAXSOCKBUF + 2 + 1]; |
|
|
242 | |
|
|
243 | bool may_execute (const packet_type *pkt) const; |
|
|
244 | void execute (const packet_type *pkt, char *data, int datalen); |
|
|
245 | |
|
|
246 | void queue_command (packet_type *handler, char *data, int datalen); |
|
|
247 | bool handle_command (); |
|
|
248 | // resets movement state |
|
|
249 | void reset_state (); |
|
|
250 | |
|
|
251 | bool handle_packet (); |
|
|
252 | int next_packet (); // returns length of packet or 0 |
|
|
253 | void skip_packet (int len); // we have processed the packet, skip it |
|
|
254 | |
|
|
255 | void flush (); |
|
|
256 | void write_outputbuffer (); |
|
|
257 | void send (void *buf_, int len); |
|
|
258 | |
|
|
259 | void send_packet (const char *buf); |
|
|
260 | void send_packet (const char *buf, int len); |
|
|
261 | void send_packet_printf (const char *format, ...); |
|
|
262 | void send_packet (packet &sl); |
|
|
263 | |
|
|
264 | // called when something under the player changes |
|
|
265 | void floorbox_update () { update_look = 1; } |
|
|
266 | // called when the player has been moved |
|
|
267 | void floorbox_reset () { look_position = 0; floorbox_update (); } |
|
|
268 | |
|
|
269 | static client *create (int fd, const char *peername); |
251 | }; |
270 | }; |
252 | |
271 | |
253 | #define CLIENT_SUPPORT_READABLES(__sockPtr,__type)\ |
272 | #define CLIENT_SUPPORT_READABLES(__sockPtr,__type)\ |
254 | ( ((__type)>0) &&\ |
273 | ( ((__type)>0) &&\ |
255 | ((__sockPtr)->has_readable_type) && \ |
274 | ((__sockPtr)->has_readable_type) && \ |