… | |
… | |
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> |
… | |
… | |
70 | { |
69 | { |
71 | //TODO: just disconnect here? |
70 | //TODO: just disconnect here? |
72 | reset_state (); |
71 | reset_state (); |
73 | send_packet_printf ("drawinfo %d command queue overflow, ignoring.", NDI_RED); |
72 | send_packet_printf ("drawinfo %d command queue overflow, ignoring.", NDI_RED); |
74 | } |
73 | } |
75 | else if (!cmd_queue.empty () && cmd_queue.front ().stamp + MAX_QUEUE_BACKLOG < stamp) |
|
|
76 | { |
|
|
77 | reset_state (); |
|
|
78 | send_packet_printf ("drawinfo %d too many commands in queue, ignoring.", NDI_RED); |
|
|
79 | } |
|
|
80 | else |
74 | else |
|
|
75 | { |
81 | cmd_queue.push_back (command ( |
76 | cmd_queue.push_back (command ()); |
82 | stamp, handler, (char *)salloc (datalen + 1, data), datalen |
77 | command &cmd = cmd_queue.back (); |
83 | )); |
78 | cmd.stamp = stamp; |
|
|
79 | cmd.handler = handler; |
|
|
80 | cmd.data = salloc<char> (datalen + 1, data); |
|
|
81 | cmd.datalen = datalen; |
|
|
82 | } |
84 | } |
83 | } |
85 | |
84 | |
86 | bool |
85 | bool |
87 | client::handle_command () |
86 | client::handle_command () |
88 | { |
87 | { |
|
|
88 | bool skipping = false; |
|
|
89 | |
|
|
90 | while (!cmd_queue.empty () |
89 | 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)) |
90 | return false; |
92 | { |
91 | |
|
|
92 | if (cmd_queue.empty ()) |
|
|
93 | return false; |
|
|
94 | |
|
|
95 | command &cmd = cmd_queue.front (); |
93 | command &cmd = cmd_queue.front (); |
96 | |
94 | |
97 | if (cmd.stamp + MAX_QUEUE_BACKLOG < now ()) |
95 | if (cmd.stamp + MAX_QUEUE_BACKLOG < now ()) |
98 | { |
96 | { |
|
|
97 | if (!skipping) |
|
|
98 | { |
|
|
99 | skipping = true; |
99 | reset_state (); |
100 | reset_state (); |
100 | send_packet_printf ("drawinfo %d ignoring delayed command.", NDI_RED); |
101 | send_packet_printf ("drawinfo %d ignoring delayed commands.", NDI_RED); |
101 | } |
102 | } |
|
|
103 | |
|
|
104 | cmd_queue.pop_front (); |
|
|
105 | } |
102 | else |
106 | else |
|
|
107 | { |
103 | execute (cmd.handler, cmd.data, cmd.datalen); |
108 | execute (cmd.handler, cmd.data, cmd.datalen); |
104 | |
|
|
105 | sfree (cmd.data, cmd.datalen); |
|
|
106 | cmd_queue.pop_front (); |
109 | cmd_queue.pop_front (); |
107 | |
|
|
108 | return true; |
110 | return true; |
|
|
111 | } |
|
|
112 | } |
|
|
113 | |
|
|
114 | return false; |
109 | } |
115 | } |
110 | |
116 | |
111 | void |
117 | void |
112 | flush_sockets (void) |
118 | flush_sockets (void) |
113 | { |
119 | { |
114 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
120 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
115 | if ((*i)->status != Ns_Dead) |
121 | if (!(*i)->destroyed) |
116 | (*i)->flush (); |
122 | (*i)->flush (); |
117 | } |
123 | } |
118 | |
124 | |
119 | /** |
125 | /** |
120 | * This checks the sockets for input, does the right thing. |
126 | * This checks the sockets for input, does the right thing. |
… | |
… | |
138 | for (player *pl = first_player; pl; ) |
144 | for (player *pl = first_player; pl; ) |
139 | { |
145 | { |
140 | player *npl = pl->next; |
146 | player *npl = pl->next; |
141 | |
147 | |
142 | //TODO: must be handled cleanly elsewhere |
148 | //TODO: must be handled cleanly elsewhere |
143 | if (!pl->socket || pl->socket->status == Ns_Dead) |
149 | if (!pl->socket || pl->socket->destroyed) |
144 | { |
150 | { |
145 | save_player (pl->ob, 0); |
151 | save_player (pl->ob, 0); |
146 | |
152 | |
147 | if (!QUERY_FLAG (pl->ob, FLAG_REMOVED)) |
153 | if (!QUERY_FLAG (pl->ob, FLAG_REMOVED)) |
148 | { |
154 | { |
… | |
… | |
159 | |
165 | |
160 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ) |
166 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ) |
161 | { |
167 | { |
162 | client *s = *i; |
168 | client *s = *i; |
163 | |
169 | |
164 | if (s->status == Ns_Dead) |
170 | if (s->destroyed) |
165 | { |
171 | { |
166 | clients.erase (i); |
172 | clients.erase (i); |
167 | delete s; |
173 | delete s; |
168 | } |
174 | } |
169 | else |
175 | else |
… | |
… | |
176 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
182 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
177 | { |
183 | { |
178 | client *s = *i; |
184 | client *s = *i; |
179 | player *pl = s->pl; |
185 | player *pl = s->pl; |
180 | |
186 | |
181 | s->handle_packet (); |
|
|
182 | s->handle_command (); |
|
|
183 | |
|
|
184 | //TODO: should not be done here, either |
187 | //TODO: should not be done here, either |
185 | if (s->status != Ns_Dead && pl) |
188 | if (!s->destroyed && pl) |
186 | { |
189 | { |
187 | /* Update the players stats once per tick. More efficient than |
190 | /* Update the players stats once per tick. More efficient than |
188 | * sending them whenever they change, and probably just as useful |
191 | * sending them whenever they change, and probably just as useful |
189 | */ |
192 | */ |
190 | esrv_update_stats (pl); |
193 | esrv_update_stats (pl); |