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