… | |
… | |
26 | * Main client/server loops. |
26 | * Main client/server loops. |
27 | * |
27 | * |
28 | * \date 2003-12-02 |
28 | * \date 2003-12-02 |
29 | * |
29 | * |
30 | * loop.c mainly deals with initialization and higher level socket |
30 | * loop.c mainly deals with initialization and higher level socket |
31 | * maintenance (checking for lost connections and if data has arrived.) |
31 | * maintanance (checking for lost connections and if data has arrived.) |
32 | * The reading of data is handled in ericserver.c |
|
|
33 | */ |
32 | */ |
34 | |
33 | |
35 | |
34 | |
36 | #include <global.h> |
35 | #include <global.h> |
37 | #include <sproto.h> |
36 | #include <sproto.h> |
… | |
… | |
47 | #include <arpa/inet.h> |
46 | #include <arpa/inet.h> |
48 | |
47 | |
49 | #include <loader.h> |
48 | #include <loader.h> |
50 | |
49 | |
51 | #define MAX_QUEUE_DEPTH 500 //TODO |
50 | #define MAX_QUEUE_DEPTH 500 //TODO |
52 | |
|
|
53 | //TODO: cannot use this, as it might ignore vital commands such as fire_stop |
|
|
54 | #define MAX_QUEUE_BACKLOG 1e99 //TODO |
51 | #define MAX_QUEUE_BACKLOG 5. //TODO |
|
|
52 | |
|
|
53 | void |
|
|
54 | client::reset_state () |
|
|
55 | { |
|
|
56 | if (!pl) |
|
|
57 | return; |
|
|
58 | |
|
|
59 | pl->run_on = 0; |
|
|
60 | pl->fire_on = 0; |
|
|
61 | } |
55 | |
62 | |
56 | void |
63 | void |
57 | client::queue_command (packet_type *handler, char *data, int datalen) |
64 | client::queue_command (packet_type *handler, char *data, int datalen) |
58 | { |
65 | { |
59 | tstamp stamp = now (); |
66 | tstamp stamp = now (); |
60 | |
67 | |
61 | if (cmd_queue.size () >= MAX_QUEUE_DEPTH) |
68 | if (cmd_queue.size () >= MAX_QUEUE_DEPTH) |
|
|
69 | { |
62 | //TODO: just disconnect here? |
70 | //TODO: just disconnect here? |
|
|
71 | reset_state (); |
63 | send_packet_printf ("drawinfo %d command queue overflow, ignoring.", NDI_RED); |
72 | send_packet_printf ("drawinfo %d command queue overflow, ignoring.", NDI_RED); |
64 | else if (!cmd_queue.empty () && cmd_queue.front ().stamp + MAX_QUEUE_BACKLOG < stamp) |
73 | } |
65 | //TODO: do fire_stop and other things that might be necessary |
|
|
66 | send_packet_printf ("drawinfo %d too many commands in queue, ignoring.", NDI_RED); |
|
|
67 | else |
74 | else |
|
|
75 | { |
68 | cmd_queue.push_back (command ( |
76 | cmd_queue.push_back (command ()); |
69 | stamp, handler, (char *)salloc (datalen, data), datalen |
77 | command &cmd = cmd_queue.back (); |
70 | )); |
78 | cmd.stamp = stamp; |
|
|
79 | cmd.handler = handler; |
|
|
80 | cmd.data = salloc<char> (datalen + 1, data); |
|
|
81 | cmd.datalen = datalen; |
|
|
82 | } |
71 | } |
83 | } |
72 | |
84 | |
73 | bool |
85 | bool |
74 | client::handle_command () |
86 | client::handle_command () |
75 | { |
87 | { |
|
|
88 | bool skipping = false; |
|
|
89 | |
|
|
90 | while (!cmd_queue.empty () |
76 | if (pl && pl->state == ST_PLAYING && pl->ob && pl->ob->speed_left < 0) |
91 | && !(pl && pl->state == ST_PLAYING && pl->ob && pl->ob->speed_left < 0)) |
77 | return false; |
92 | { |
78 | |
|
|
79 | if (cmd_queue.empty ()) |
|
|
80 | return false; |
|
|
81 | |
|
|
82 | command &cmd = cmd_queue.front (); |
93 | command &cmd = cmd_queue.front (); |
83 | |
94 | |
84 | if (cmd.stamp + MAX_QUEUE_BACKLOG < now ()) |
95 | if (cmd.stamp + MAX_QUEUE_BACKLOG < now ()) |
85 | //TODO: do fire_stop and other things that might be necessary |
96 | { |
|
|
97 | if (!skipping) |
|
|
98 | { |
|
|
99 | skipping = true; |
|
|
100 | reset_state (); |
86 | send_packet_printf ("drawinfo %d ignoring delayed command.", NDI_RED); |
101 | send_packet_printf ("drawinfo %d ignoring delayed commands.", NDI_RED); |
|
|
102 | } |
|
|
103 | |
|
|
104 | cmd_queue.pop_front (); |
|
|
105 | } |
87 | else |
106 | else |
|
|
107 | { |
88 | execute (cmd.handler, cmd.data, cmd.datalen); |
108 | execute (cmd.handler, cmd.data, cmd.datalen); |
89 | |
|
|
90 | sfree (cmd.data, cmd.datalen); |
|
|
91 | cmd_queue.pop_front (); |
109 | cmd_queue.pop_front (); |
92 | |
|
|
93 | return true; |
110 | return true; |
|
|
111 | } |
|
|
112 | } |
|
|
113 | |
|
|
114 | return false; |
94 | } |
115 | } |
95 | |
116 | |
96 | void |
117 | void |
97 | flush_sockets (void) |
118 | flush_sockets (void) |
98 | { |
119 | { |
… | |
… | |
162 | { |
183 | { |
163 | client *s = *i; |
184 | client *s = *i; |
164 | player *pl = s->pl; |
185 | player *pl = s->pl; |
165 | |
186 | |
166 | s->handle_packet (); |
187 | s->handle_packet (); |
|
|
188 | |
|
|
189 | for (int i = 16; --i; ) //D |
|
|
190 | if (!//D |
167 | s->handle_command (); |
191 | s->handle_command () |
|
|
192 | ) break;//D |
168 | |
193 | |
169 | //TODO: should not be done here, either |
194 | //TODO: should not be done here, either |
170 | if (s->status != Ns_Dead && pl) |
195 | if (s->status != Ns_Dead && pl) |
171 | { |
196 | { |
172 | /* Update the players stats once per tick. More efficient than |
197 | /* Update the players stats once per tick. More efficient than |