1 |
elmex |
1.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 |
root |
1.7 |
The author can be reached via e-mail to <crossfire@schmorp.de> |
22 |
elmex |
1.1 |
*/ |
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 |
root |
1.26 |
* maintanance (checking for lost connections and if data has arrived.) |
32 |
elmex |
1.1 |
*/ |
33 |
|
|
|
34 |
|
|
|
35 |
|
|
#include <global.h> |
36 |
root |
1.10 |
#include <sproto.h> |
37 |
|
|
#include <sockproto.h> |
38 |
elmex |
1.1 |
|
39 |
pippijn |
1.8 |
#include <sys/types.h> |
40 |
|
|
#include <sys/time.h> |
41 |
|
|
#include <sys/socket.h> |
42 |
|
|
#include <netinet/in.h> |
43 |
|
|
#include <netdb.h> |
44 |
elmex |
1.1 |
|
45 |
root |
1.16 |
#include <unistd.h> |
46 |
|
|
#include <arpa/inet.h> |
47 |
elmex |
1.1 |
|
48 |
|
|
#include <loader.h> |
49 |
|
|
|
50 |
root |
1.20 |
#define MAX_QUEUE_DEPTH 500 //TODO |
51 |
root |
1.23 |
#define MAX_QUEUE_BACKLOG 5. //TODO |
52 |
elmex |
1.1 |
|
53 |
root |
1.21 |
void |
54 |
|
|
client::reset_state () |
55 |
|
|
{ |
56 |
|
|
if (!pl) |
57 |
|
|
return; |
58 |
|
|
|
59 |
|
|
pl->run_on = 0; |
60 |
|
|
pl->fire_on = 0; |
61 |
|
|
} |
62 |
elmex |
1.1 |
|
63 |
root |
1.20 |
void |
64 |
|
|
client::queue_command (packet_type *handler, char *data, int datalen) |
65 |
root |
1.6 |
{ |
66 |
root |
1.20 |
tstamp stamp = now (); |
67 |
elmex |
1.1 |
|
68 |
root |
1.20 |
if (cmd_queue.size () >= MAX_QUEUE_DEPTH) |
69 |
root |
1.21 |
{ |
70 |
|
|
//TODO: just disconnect here? |
71 |
|
|
reset_state (); |
72 |
|
|
send_packet_printf ("drawinfo %d command queue overflow, ignoring.", NDI_RED); |
73 |
|
|
} |
74 |
root |
1.20 |
else |
75 |
root |
1.24 |
{ |
76 |
|
|
cmd_queue.push_back (command ()); |
77 |
|
|
command &cmd = cmd_queue.back (); |
78 |
|
|
cmd.stamp = stamp; |
79 |
|
|
cmd.handler = handler; |
80 |
root |
1.25 |
cmd.data = salloc<char> (datalen + 1, data); |
81 |
root |
1.24 |
cmd.datalen = datalen; |
82 |
|
|
} |
83 |
root |
1.20 |
} |
84 |
elmex |
1.1 |
|
85 |
root |
1.20 |
bool |
86 |
|
|
client::handle_command () |
87 |
elmex |
1.1 |
{ |
88 |
root |
1.26 |
bool skipping = false; |
89 |
elmex |
1.1 |
|
90 |
root |
1.26 |
while (!cmd_queue.empty () |
91 |
root |
1.31 |
&& !(state == ST_PLAYING && pl->ob && pl->ob->speed_left < 0)) |
92 |
root |
1.26 |
{ |
93 |
|
|
command &cmd = cmd_queue.front (); |
94 |
root |
1.6 |
|
95 |
root |
1.26 |
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 |
root |
1.11 |
|
104 |
root |
1.26 |
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 |
root |
1.21 |
} |
113 |
root |
1.6 |
|
114 |
root |
1.26 |
return false; |
115 |
elmex |
1.1 |
} |
116 |
|
|
|
117 |
root |
1.6 |
void |
118 |
|
|
flush_sockets (void) |
119 |
elmex |
1.1 |
{ |
120 |
root |
1.19 |
for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
121 |
root |
1.31 |
(*i)->flush (); |
122 |
elmex |
1.1 |
} |
123 |
|
|
|
124 |
|
|
/** |
125 |
root |
1.10 |
* This checks the sockets for input, does the right thing. |
126 |
elmex |
1.1 |
* |
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 |
root |
1.6 |
void |
132 |
|
|
doeric_server (void) |
133 |
elmex |
1.1 |
{ |
134 |
|
|
#ifdef CS_LOGSTATS |
135 |
root |
1.6 |
if ((time (NULL) - cst_lst.time_start) >= CS_LOGTIME) |
136 |
|
|
write_cs_stats (); |
137 |
elmex |
1.1 |
#endif |
138 |
|
|
|
139 |
root |
1.19 |
for (sockvec::iterator i = clients.begin (); i != clients.end (); ) |
140 |
root |
1.6 |
{ |
141 |
root |
1.19 |
client *s = *i; |
142 |
elmex |
1.1 |
|
143 |
root |
1.31 |
if (s->destroyed ()) |
144 |
root |
1.6 |
{ |
145 |
root |
1.19 |
clients.erase (i); |
146 |
root |
1.14 |
delete s; |
147 |
root |
1.6 |
} |
148 |
|
|
else |
149 |
root |
1.20 |
++i; |
150 |
elmex |
1.1 |
} |
151 |
|
|
|
152 |
root |
1.14 |
/* We need to do some of the processing below regardless */ |
153 |
|
|
|
154 |
root |
1.10 |
/* Check for any input on the sockets */ |
155 |
root |
1.19 |
for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
156 |
root |
1.6 |
{ |
157 |
root |
1.19 |
client *s = *i; |
158 |
root |
1.14 |
player *pl = s->pl; |
159 |
elmex |
1.1 |
|
160 |
root |
1.14 |
//TODO: should not be done here, either |
161 |
root |
1.31 |
if (!s->destroyed () && pl) |
162 |
root |
1.6 |
{ |
163 |
root |
1.10 |
/* Update the players stats once per tick. More efficient than |
164 |
|
|
* sending them whenever they change, and probably just as useful |
165 |
root |
1.6 |
*/ |
166 |
root |
1.10 |
esrv_update_stats (pl); |
167 |
root |
1.20 |
|
168 |
root |
1.10 |
if (pl->last_weight != -1 && pl->last_weight != WEIGHT (pl->ob)) |
169 |
root |
1.6 |
{ |
170 |
root |
1.10 |
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 |
root |
1.6 |
} |
175 |
|
|
|
176 |
root |
1.10 |
draw_client_map (pl->ob); |
177 |
root |
1.20 |
|
178 |
root |
1.14 |
if (s->update_look) |
179 |
root |
1.10 |
esrv_draw_look (pl->ob); |
180 |
root |
1.3 |
} |
181 |
elmex |
1.1 |
} |
182 |
|
|
} |
183 |
root |
1.10 |
|