| 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 |
|
|
&& !(pl && pl->state == ST_PLAYING && pl->ob && pl->ob->speed_left < 0)) |
| 92 |
|
|
{ |
| 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.15 |
if ((*i)->status != Ns_Dead) |
| 122 |
|
|
(*i)->flush (); |
| 123 |
elmex |
1.1 |
} |
| 124 |
|
|
|
| 125 |
|
|
/** |
| 126 |
root |
1.10 |
* This checks the sockets for input, does the right thing. |
| 127 |
elmex |
1.1 |
* |
| 128 |
|
|
* A bit of this code is grabbed out of socket.c |
| 129 |
|
|
* There are 2 lists we need to look through - init_sockets is a list |
| 130 |
|
|
* |
| 131 |
|
|
*/ |
| 132 |
root |
1.6 |
void |
| 133 |
|
|
doeric_server (void) |
| 134 |
elmex |
1.1 |
{ |
| 135 |
|
|
#ifdef CS_LOGSTATS |
| 136 |
root |
1.6 |
if ((time (NULL) - cst_lst.time_start) >= CS_LOGTIME) |
| 137 |
|
|
write_cs_stats (); |
| 138 |
elmex |
1.1 |
#endif |
| 139 |
|
|
|
| 140 |
root |
1.6 |
/* Go through the players. Let the loop set the next pl value, |
| 141 |
|
|
* since we may remove some |
| 142 |
|
|
*/ |
| 143 |
root |
1.14 |
//TODO: must be handled cleanly elsewhere |
| 144 |
root |
1.20 |
for (player *pl = first_player; pl; ) |
| 145 |
root |
1.6 |
{ |
| 146 |
root |
1.14 |
player *npl = pl->next; |
| 147 |
|
|
|
| 148 |
|
|
//TODO: must be handled cleanly elsewhere |
| 149 |
root |
1.20 |
if (!pl->socket || pl->socket->status == Ns_Dead) |
| 150 |
root |
1.6 |
{ |
| 151 |
|
|
save_player (pl->ob, 0); |
| 152 |
root |
1.10 |
|
| 153 |
root |
1.6 |
if (!QUERY_FLAG (pl->ob, FLAG_REMOVED)) |
| 154 |
|
|
{ |
| 155 |
|
|
terminate_all_pets (pl->ob); |
| 156 |
root |
1.9 |
pl->ob->remove (); |
| 157 |
root |
1.3 |
} |
| 158 |
root |
1.10 |
|
| 159 |
root |
1.6 |
leave (pl, 1); |
| 160 |
|
|
final_free_player (pl); |
| 161 |
root |
1.3 |
} |
| 162 |
elmex |
1.1 |
|
| 163 |
root |
1.14 |
pl = npl; |
| 164 |
elmex |
1.1 |
} |
| 165 |
|
|
|
| 166 |
root |
1.19 |
for (sockvec::iterator i = clients.begin (); i != clients.end (); ) |
| 167 |
root |
1.6 |
{ |
| 168 |
root |
1.19 |
client *s = *i; |
| 169 |
elmex |
1.1 |
|
| 170 |
root |
1.14 |
if (s->status == Ns_Dead) |
| 171 |
root |
1.6 |
{ |
| 172 |
root |
1.19 |
clients.erase (i); |
| 173 |
root |
1.14 |
delete s; |
| 174 |
root |
1.6 |
} |
| 175 |
|
|
else |
| 176 |
root |
1.20 |
++i; |
| 177 |
elmex |
1.1 |
} |
| 178 |
|
|
|
| 179 |
root |
1.14 |
/* We need to do some of the processing below regardless */ |
| 180 |
|
|
|
| 181 |
root |
1.10 |
/* Check for any input on the sockets */ |
| 182 |
root |
1.19 |
for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) |
| 183 |
root |
1.6 |
{ |
| 184 |
root |
1.19 |
client *s = *i; |
| 185 |
root |
1.14 |
player *pl = s->pl; |
| 186 |
elmex |
1.1 |
|
| 187 |
root |
1.28 |
//TODO: this could probably be nuked completely |
| 188 |
root |
1.20 |
s->handle_packet (); |
| 189 |
root |
1.28 |
s->handle_command (); |
| 190 |
root |
1.10 |
|
| 191 |
root |
1.14 |
//TODO: should not be done here, either |
| 192 |
|
|
if (s->status != Ns_Dead && pl) |
| 193 |
root |
1.6 |
{ |
| 194 |
root |
1.10 |
/* Update the players stats once per tick. More efficient than |
| 195 |
|
|
* sending them whenever they change, and probably just as useful |
| 196 |
root |
1.6 |
*/ |
| 197 |
root |
1.10 |
esrv_update_stats (pl); |
| 198 |
root |
1.20 |
|
| 199 |
root |
1.10 |
if (pl->last_weight != -1 && pl->last_weight != WEIGHT (pl->ob)) |
| 200 |
root |
1.6 |
{ |
| 201 |
root |
1.10 |
esrv_update_item (UPD_WEIGHT, pl->ob, pl->ob); |
| 202 |
|
|
if (pl->last_weight != WEIGHT (pl->ob)) |
| 203 |
|
|
LOG (llevError, "esrv_update_item(UPD_WEIGHT) did not set player weight: is %lu, should be %lu\n", |
| 204 |
|
|
(unsigned long) pl->last_weight, WEIGHT (pl->ob)); |
| 205 |
root |
1.6 |
} |
| 206 |
|
|
|
| 207 |
root |
1.10 |
draw_client_map (pl->ob); |
| 208 |
root |
1.20 |
|
| 209 |
root |
1.14 |
if (s->update_look) |
| 210 |
root |
1.10 |
esrv_draw_look (pl->ob); |
| 211 |
root |
1.3 |
} |
| 212 |
elmex |
1.1 |
} |
| 213 |
|
|
} |
| 214 |
root |
1.10 |
|