… | |
… | |
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 |
81 | { |
75 | { |
82 | cmd_queue.push_back (command ()); |
76 | cmd_queue.push_back (command ()); |
83 | command &cmd = cmd_queue.back (); |
77 | command &cmd = cmd_queue.back (); |
84 | cmd.stamp = stamp; |
78 | cmd.stamp = stamp; |
85 | cmd.handler = handler; |
79 | cmd.handler = handler; |
86 | cmd.data = (char *)salloc (datalen + 1, data); |
80 | cmd.data = salloc<char> (datalen + 1, data); |
87 | cmd.datalen = datalen; |
81 | cmd.datalen = datalen; |
88 | } |
82 | } |
89 | } |
83 | } |
90 | |
84 | |
91 | bool |
85 | bool |
92 | client::handle_command () |
86 | client::handle_command () |
93 | { |
87 | { |
94 | if (pl && pl->state == ST_PLAYING && pl->ob && pl->ob->speed_left < 0) |
88 | bool skipping = false; |
95 | return false; |
|
|
96 | |
89 | |
97 | if (cmd_queue.empty ()) |
90 | while (!cmd_queue.empty () |
98 | return false; |
91 | && !(state == ST_PLAYING && pl->ob && pl->ob->speed_left < 0)) |
|
|
92 | { |
|
|
93 | command &cmd = cmd_queue.front (); |
99 | |
94 | |
100 | command &cmd = cmd_queue.front (); |
95 | if (cmd.stamp + MAX_QUEUE_BACKLOG < now ()) |
|
|
96 | { |
|
|
97 | if (!skipping) |
|
|
98 | { |
|
|
99 | skipping = true; |
|
|
100 | reset_state (); |
|
|
101 | send_packet_printf ("drawinfo %d ignoring delayed commands.", NDI_RED); |
|
|
102 | } |
101 | |
103 | |
102 | if (cmd.stamp + MAX_QUEUE_BACKLOG < now ()) |
104 | cmd_queue.pop_front (); |
103 | { |
105 | } |
104 | reset_state (); |
106 | else |
105 | send_packet_printf ("drawinfo %d ignoring delayed command.", NDI_RED); |
107 | { |
|
|
108 | execute (cmd.handler, cmd.data, cmd.datalen); |
|
|
109 | cmd_queue.pop_front (); |
|
|
110 | return true; |
|
|
111 | } |
106 | } |
112 | } |
107 | else |
|
|
108 | execute (cmd.handler, cmd.data, cmd.datalen); |
|
|
109 | |
113 | |
110 | cmd_queue.pop_front (); |
|
|
111 | |
|
|
112 | return true; |
114 | return false; |
113 | } |
115 | } |
114 | |
116 | |
115 | void |
117 | void |
116 | flush_sockets (void) |
118 | flush_sockets (void) |
117 | { |
119 | { |
118 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
120 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
119 | if ((*i)->status != Ns_Dead) |
|
|
120 | (*i)->flush (); |
121 | (*i)->flush (); |
121 | } |
122 | } |
122 | |
123 | |
123 | /** |
124 | /** |
124 | * This checks the sockets for input, does the right thing. |
125 | * This checks the sockets for input, does the right thing. |
125 | * |
126 | * |
… | |
… | |
133 | #ifdef CS_LOGSTATS |
134 | #ifdef CS_LOGSTATS |
134 | if ((time (NULL) - cst_lst.time_start) >= CS_LOGTIME) |
135 | if ((time (NULL) - cst_lst.time_start) >= CS_LOGTIME) |
135 | write_cs_stats (); |
136 | write_cs_stats (); |
136 | #endif |
137 | #endif |
137 | |
138 | |
138 | /* Go through the players. Let the loop set the next pl value, |
|
|
139 | * since we may remove some |
|
|
140 | */ |
|
|
141 | //TODO: must be handled cleanly elsewhere |
|
|
142 | for (player *pl = first_player; pl; ) |
|
|
143 | { |
|
|
144 | player *npl = pl->next; |
|
|
145 | |
|
|
146 | //TODO: must be handled cleanly elsewhere |
|
|
147 | if (!pl->socket || pl->socket->status == Ns_Dead) |
|
|
148 | { |
|
|
149 | save_player (pl->ob, 0); |
|
|
150 | |
|
|
151 | if (!QUERY_FLAG (pl->ob, FLAG_REMOVED)) |
|
|
152 | { |
|
|
153 | terminate_all_pets (pl->ob); |
|
|
154 | pl->ob->remove (); |
|
|
155 | } |
|
|
156 | |
|
|
157 | leave (pl, 1); |
|
|
158 | final_free_player (pl); |
|
|
159 | } |
|
|
160 | |
|
|
161 | pl = npl; |
|
|
162 | } |
|
|
163 | |
|
|
164 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ) |
139 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ) |
165 | { |
140 | { |
166 | client *s = *i; |
141 | client *s = *i; |
167 | |
142 | |
168 | if (s->status == Ns_Dead) |
143 | if (s->destroyed ()) |
169 | { |
144 | { |
170 | clients.erase (i); |
145 | clients.erase (i); |
171 | delete s; |
146 | delete s; |
172 | } |
147 | } |
173 | else |
148 | else |
… | |
… | |
180 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
155 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
181 | { |
156 | { |
182 | client *s = *i; |
157 | client *s = *i; |
183 | player *pl = s->pl; |
158 | player *pl = s->pl; |
184 | |
159 | |
185 | s->handle_packet (); |
|
|
186 | s->handle_command (); |
|
|
187 | |
|
|
188 | //TODO: should not be done here, either |
160 | //TODO: should not be done here, either |
189 | if (s->status != Ns_Dead && pl) |
161 | if (!s->destroyed () && pl) |
190 | { |
162 | { |
191 | /* Update the players stats once per tick. More efficient than |
163 | /* Update the players stats once per tick. More efficient than |
192 | * sending them whenever they change, and probably just as useful |
164 | * sending them whenever they change, and probably just as useful |
193 | */ |
165 | */ |
194 | esrv_update_stats (pl); |
166 | esrv_update_stats (pl); |