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