ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/loop.C
(Generate patch)

Comparing deliantra/server/socket/loop.C (file contents):
Revision 1.20 by root, Sat Dec 16 03:08:26 2006 UTC vs.
Revision 1.29 by root, Tue Dec 19 04:58:05 2006 UTC

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
53void
54client::reset_state ()
55{
56 if (!pl)
57 return;
58
59 pl->run_on = 0;
60 pl->fire_on = 0;
61}
55 62
56void 63void
57client::queue_command (packet_type *handler, char *data, int datalen) 64client::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
73bool 85bool
74client::handle_command () 86client::handle_command ()
75{ 87{
88 bool skipping = false;
89
90 while (!cmd_queue.empty ()
76 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))
77 return false; 92 {
78
79 if (cmd_queue.empty ())
80 return false;
81
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
96void 117void
97flush_sockets (void) 118flush_sockets (void)
98{ 119{
161 for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) 182 for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i)
162 { 183 {
163 client *s = *i; 184 client *s = *i;
164 player *pl = s->pl; 185 player *pl = s->pl;
165 186
166 s->handle_packet ();
167 s->handle_command ();
168
169 //TODO: should not be done here, either 187 //TODO: should not be done here, either
170 if (s->status != Ns_Dead && pl) 188 if (s->status != Ns_Dead && pl)
171 { 189 {
172 /* Update the players stats once per tick. More efficient than 190 /* Update the players stats once per tick. More efficient than
173 * sending them whenever they change, and probably just as useful 191 * sending them whenever they change, and probably just as useful

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines