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.35 by root, Tue Jan 2 11:08:36 2007 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
51#define MAX_QUEUE_BACKLOG 3. //TODO
52 52
53//TODO: cannot use this, as it might ignore vital commands such as fire_stop 53void
54#define MAX_QUEUE_BACKLOG 1e99 //TODO 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{
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
96void 117void
97flush_sockets (void) 118flush_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 140 for (int i = 0; i < clients.size (); ++i)
121 */
122 //TODO: must be handled cleanly elsewhere
123 for (player *pl = first_player; pl; )
124 { 141 {
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 (); )
146 {
147 client *s = *i; 142 client *s = clients [i];
148
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; 143 player *pl = s->pl;
165 144
166 s->handle_packet (); 145 if (pl && pl->ns && !pl->ns->destroyed ())
167 s->handle_command ();
168
169 //TODO: should not be done here, either
170 if (s->status != Ns_Dead && pl)
171 { 146 {
172 /* Update the players stats once per tick. More efficient than 147 /* Update the players stats once per tick. More efficient than
173 * sending them whenever they change, and probably just as useful 148 * sending them whenever they change, and probably just as useful
174 */ 149 */
175 esrv_update_stats (pl); 150 esrv_update_stats (pl);
185 draw_client_map (pl->ob); 160 draw_client_map (pl->ob);
186 161
187 if (s->update_look) 162 if (s->update_look)
188 esrv_draw_look (pl->ob); 163 esrv_draw_look (pl->ob);
189 } 164 }
165
166 s->refcnt_chk ();
190 } 167 }
191} 168}
192 169

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines