/*
* This file is part of Deliantra, the Roguelike Realtime MMORPG.
*
* Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team
*
* Deliantra is free software: you can redistribute it and/or modify it under
* the terms of the Affero GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the Affero GNU General Public License
* and the GNU General Public License along with this program. If not, see
* .
*
* The authors can be reached via e-mail to
*/
/**
* \file
* Main client/server loops.
*
* \date 2003-12-02
*
* loop.c mainly deals with initialization and higher level socket
* maintanance (checking for lost connections and if data has arrived.)
*/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define BG_SCRUB_RATE 4 // how often to send a face in the background
#define MAX_QUEUE_DEPTH 50
#define MAX_QUEUE_BACKLOG 3.
void
client::reset_state ()
{
if (!pl)
return;
pl->run_on = 0;
pl->fire_on = 0;
}
void
client::queue_command (packet_type *handler, char *data, int datalen)
{
tstamp stamp = NOW;
if (cmd_queue.size () >= MAX_QUEUE_DEPTH)
{
//TODO: just disconnect here?
reset_state ();
send_packet_printf ("drawinfo %d command queue overflow, ignoring.", NDI_RED);
}
else
{
cmd_queue.resize (cmd_queue.size () + 1);
command &cmd = cmd_queue.back ();
cmd.stamp = stamp;
cmd.handler = handler;
cmd.data = salloc (datalen + 1, data);
cmd.datalen = datalen;
}
}
bool
client::handle_command ()
{
if (!cmd_queue.empty ()
&& state == ST_PLAYING
&& pl->ob->speed_left > 0.f)
{
command &cmd = cmd_queue.front ();
if (cmd.stamp + MAX_QUEUE_BACKLOG < NOW)
{
reset_state (); // the command might actually reset some movement state etc.
if (pl)
pl->failmsg (
"Cannot keep up with your commands, ignoring them! "
"H. "
"Try issuing commands slower, or, if G is incapacitated (paralyzed and so on), "
"wait till your character can act again.\n\nIf G is permanently stuck, then "
"try the B command (or use B to ask somebody to B you out).>"
);
}
else
execute (cmd.handler, cmd.data, cmd.datalen);
sfree (cmd.data, cmd.datalen + 1);
cmd_queue.pop_front ();
return true;
}
else
return false;
}
void
client::tick ()
{
if (!pl || destroyed ())
return;
pl->dirty = true;
/* Update the players stats once per tick. More efficient than
* sending them whenever they change, and probably just as useful
*/
pl->need_updated_stats ();
esrv_update_stats (pl);
if (pl->ns->update_spells)
esrv_update_spells (pl);
sint32 weight = pl->ob->client_weight ();
if (last_weight != weight)
{
pl->ob->update_stats ();
esrv_update_item (UPD_WEIGHT, pl->ob, pl->ob);
}
draw_client_map (pl);
if (update_look)
esrv_draw_look (pl);
mapinfo_queue_run ();
#if HAVE_TCP_INFO
// check time of last ack, and, if too old, kill connection
socklen_t len = sizeof (tcpi);
if (!getsockopt (fd, IPPROTO_TCP, TCP_INFO, &tcpi, &len) && len == sizeof (tcpi))
{
if (tcpi.tcpi_snd_mss)
mss = tcpi.tcpi_snd_mss;
#if 0
fprintf (stderr, "uack %d ack %d lost %d ret %d fack %d sst %d cwnd %d mss %d pmtu %d advmss %d EXC %d\n",
tcpi.tcpi_unacked,
tcpi.tcpi_sacked,
tcpi.tcpi_lost,
tcpi.tcpi_retrans,
tcpi.tcpi_fackets,
tcpi.tcpi_snd_ssthresh, tcpi.tcpi_snd_cwnd, tcpi.tcpi_advmss, tcpi.tcpi_pmtu, tcpi.tcpi_advmss,
tcpi.tcpi_snd_cwnd - (tcpi.tcpi_unacked - tcpi.tcpi_sacked));
#endif
// fast-time-out a player by checking for missing acks
// do this only when player is active
if (pl && pl->active
&& tcpi.tcpi_last_ack_recv > int (socket_timeout * 1000))
{
send_msg (NDI_RED | NDI_REPLY, "connection-timeout", "safety disconnect due to tcp/ip timeout (no packets received)");
write_outputbuffer ();
LOG (llevDebug, "connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd,
(unsigned)tcpi.tcpi_last_ack_recv, (unsigned)tcpi.tcpi_last_data_sent, (unsigned)tcpi.tcpi_unacked);
destroy ();
}
}
#endif
rate_avail = min (max_rate + mss, rate_avail + max_rate);
int max_send = rate_avail;
#if HAVE_TCP_INFO
// further restrict the available bandwidth by the excess bandwidth available
max_send = min (max_send, (tcpi.tcpi_snd_cwnd - tcpi.tcpi_unacked + tcpi.tcpi_sacked) * mss);
#endif
// round to next-lowest mss
max_send -= max_send % mss;
if (ixface.empty ())
{
// regularly send a new face when queue is empty
if (bg_scrub && !--bg_scrub)
while (scrub_idx < faces.size () - 1)
{
++scrub_idx;
if (!faces_sent [scrub_idx])
if (faceinfo *f = face_info (scrub_idx))
if (f->type == FT_FACE || f->type == FT_SOUND) // only scrub faces and sounds for now
{
send_face (scrub_idx, -120);
flush_fx ();
bg_scrub = 1; // send up to one fx per tick, unless an image was requested
break;
}
}
}
else
{
bg_scrub = BG_SCRUB_RATE;
for (;;)
{
int avail = max_send - outputbuffer_len ();
if (avail <= 0)
break;
ixsend &ix = ixface.back ();
if (facedata *d = face_data (ix.idx, faceset))
{
// estimate the packet header overhead "ix " + idx + (new)ofs
int pktlen = 3 + ber32::encoded_size (ix.idx) + ber32::encoded_size (ix.ofs);
int chunk = min (avail - packet::hdrlen, MAXSOCKBUF) - pktlen;
// only transfer something if the amount of data transferred
// has a healthy relation to the header overhead
if (chunk < 64)
break;
chunk = min (chunk, (int)ix.ofs);
ix.ofs -= chunk;
//fprintf (stderr, "i%dx %6d: %5d+%4d (%4d)\n", fxix, ix.idx,ix.ofs,chunk, ixface.size());//D
packet sl ("ix");
sl << ber32 (ix.idx)
<< ber32 (ix.ofs)
<< data (d->data.data () + ix.ofs, chunk);
send_packet (sl);
}
else
ix.ofs = 0;
if (!ix.ofs)
{
ixface.pop_back ();
if (ixface.empty ())
break;
}
}
}
rate_avail -= outputbuffer_len ();
}
void
client::flush_sockets ()
{
for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i)
(*i)->flush ();
}
void
client::clock ()
{
for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i)
(*i)->tick ();
// give them all the same chances
flush_sockets ();
//TODO: should not be done here, either
for (unsigned i = 0; i < clients.size (); ++i)
clients[i]->refcnt_chk ();
}