ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/loop.C
Revision: 1.32
Committed: Fri Dec 22 16:34:00 2006 UTC (17 years, 5 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.31: +0 -26 lines
Log Message:
- preliminary check in, stuff is rudimentarily working
- moved most of the player creation process into a perl coroutine
- changed internal design of player management to not reuse
  and morph the object in funny ways. should be safer and much
  nicer to handle.
- got rid of some annoying hacks, such as clear()
  (TODO: get rid of player_pod and other stuff now unnecessary?)

File Contents

# Content
1 /*
2 CrossFire, A Multiplayer game for X-windows
3
4 Copyright (C) 2002-2003 Mark Wedel & The Crossfire Development Team
5 Copyright (C) 1992 Frank Tore Johansen
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 The author can be reached via e-mail to <crossfire@schmorp.de>
22 */
23
24 /**
25 * \file
26 * Main client/server loops.
27 *
28 * \date 2003-12-02
29 *
30 * loop.c mainly deals with initialization and higher level socket
31 * maintanance (checking for lost connections and if data has arrived.)
32 */
33
34
35 #include <global.h>
36 #include <sproto.h>
37 #include <sockproto.h>
38
39 #include <sys/types.h>
40 #include <sys/time.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <netdb.h>
44
45 #include <unistd.h>
46 #include <arpa/inet.h>
47
48 #include <loader.h>
49
50 #define MAX_QUEUE_DEPTH 500 //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 }
62
63 void
64 client::queue_command (packet_type *handler, char *data, int datalen)
65 {
66 tstamp stamp = now ();
67
68 if (cmd_queue.size () >= MAX_QUEUE_DEPTH)
69 {
70 //TODO: just disconnect here?
71 reset_state ();
72 send_packet_printf ("drawinfo %d command queue overflow, ignoring.", NDI_RED);
73 }
74 else
75 {
76 cmd_queue.push_back (command ());
77 command &cmd = cmd_queue.back ();
78 cmd.stamp = stamp;
79 cmd.handler = handler;
80 cmd.data = salloc<char> (datalen + 1, data);
81 cmd.datalen = datalen;
82 }
83 }
84
85 bool
86 client::handle_command ()
87 {
88 bool skipping = false;
89
90 while (!cmd_queue.empty ()
91 && !(state == ST_PLAYING && pl->ob && pl->ob->speed_left < 0))
92 {
93 command &cmd = cmd_queue.front ();
94
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 }
103
104 cmd_queue.pop_front ();
105 }
106 else
107 {
108 execute (cmd.handler, cmd.data, cmd.datalen);
109 cmd_queue.pop_front ();
110 return true;
111 }
112 }
113
114 return false;
115 }
116
117 void
118 flush_sockets (void)
119 {
120 for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i)
121 (*i)->flush ();
122 }
123
124 /**
125 * This checks the sockets for input, does the right thing.
126 *
127 * A bit of this code is grabbed out of socket.c
128 * There are 2 lists we need to look through - init_sockets is a list
129 *
130 */
131 void
132 doeric_server (void)
133 {
134 #ifdef CS_LOGSTATS
135 if ((time (NULL) - cst_lst.time_start) >= CS_LOGTIME)
136 write_cs_stats ();
137 #endif
138
139 for (sockvec::iterator i = clients.begin (); i != clients.end (); )
140 {
141 client *s = *i;
142
143 if (s->destroyed ())
144 {
145 clients.erase (i);
146 delete s;
147 }
148 else
149 ++i;
150 }
151
152 /* We need to do some of the processing below regardless */
153
154 /* Check for any input on the sockets */
155 for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i)
156 {
157 client *s = *i;
158 player *pl = s->pl;
159
160 //TODO: should not be done here, either
161 if (!s->destroyed () && pl)
162 {
163 /* Update the players stats once per tick. More efficient than
164 * sending them whenever they change, and probably just as useful
165 */
166 esrv_update_stats (pl);
167
168 if (pl->last_weight != -1 && pl->last_weight != WEIGHT (pl->ob))
169 {
170 esrv_update_item (UPD_WEIGHT, pl->ob, pl->ob);
171 if (pl->last_weight != WEIGHT (pl->ob))
172 LOG (llevError, "esrv_update_item(UPD_WEIGHT) did not set player weight: is %lu, should be %lu\n",
173 (unsigned long) pl->last_weight, WEIGHT (pl->ob));
174 }
175
176 draw_client_map (pl->ob);
177
178 if (s->update_look)
179 esrv_draw_look (pl->ob);
180 }
181 }
182 }
183