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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines