… | |
… | |
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 | { |
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)) |
81 | |
92 | { |
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 | { |
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 | * |
… | |
… | |
123 | for (player *pl = first_player; pl; ) |
143 | for (player *pl = first_player; pl; ) |
124 | { |
144 | { |
125 | player *npl = pl->next; |
145 | player *npl = pl->next; |
126 | |
146 | |
127 | //TODO: must be handled cleanly elsewhere |
147 | //TODO: must be handled cleanly elsewhere |
128 | if (!pl->socket || pl->socket->status == Ns_Dead) |
148 | if (!pl->ns || pl->ns->destroyed ()) |
129 | { |
149 | { |
130 | save_player (pl->ob, 0); |
150 | save_player (pl->ob, 0); |
131 | |
151 | |
132 | if (!QUERY_FLAG (pl->ob, FLAG_REMOVED)) |
152 | if (!QUERY_FLAG (pl->ob, FLAG_REMOVED)) |
133 | { |
153 | { |
… | |
… | |
144 | |
164 | |
145 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ) |
165 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ) |
146 | { |
166 | { |
147 | client *s = *i; |
167 | client *s = *i; |
148 | |
168 | |
149 | if (s->status == Ns_Dead) |
169 | if (s->destroyed ()) |
150 | { |
170 | { |
151 | clients.erase (i); |
171 | clients.erase (i); |
152 | delete s; |
172 | delete s; |
153 | } |
173 | } |
154 | else |
174 | else |
… | |
… | |
161 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
181 | for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
162 | { |
182 | { |
163 | client *s = *i; |
183 | client *s = *i; |
164 | player *pl = s->pl; |
184 | player *pl = s->pl; |
165 | |
185 | |
166 | s->handle_packet (); |
|
|
167 | s->handle_command (); |
|
|
168 | |
|
|
169 | //TODO: should not be done here, either |
186 | //TODO: should not be done here, either |
170 | if (s->status != Ns_Dead && pl) |
187 | if (!s->destroyed () && pl) |
171 | { |
188 | { |
172 | /* Update the players stats once per tick. More efficient than |
189 | /* Update the players stats once per tick. More efficient than |
173 | * sending them whenever they change, and probably just as useful |
190 | * sending them whenever they change, and probably just as useful |
174 | */ |
191 | */ |
175 | esrv_update_stats (pl); |
192 | esrv_update_stats (pl); |