--- deliantra/server/socket/lowlevel.C 2006/12/21 23:37:06 1.29 +++ deliantra/server/socket/lowlevel.C 2007/04/02 19:56:11 1.37 @@ -1,25 +1,26 @@ /* - CrossFire, A Multiplayer game for X-windows - - Copyright (C) 1992 Frank Tore Johansen - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 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 GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - The author can be reached via e-mail to mark@pyramid.com -*/ - + * CrossFire, A Multiplayer game for X-windows + * + * Copyright (C) 2005, 2006, 2007 Marc Lehmann & Crossfire+ Development Team + * Copyright (C) 1992 Frank Tore Johansen + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * The author can be reached via e-mail to mark@pyramid.com + */ + /** * \file * Low-level socket-related functions. @@ -187,8 +188,8 @@ * Dispatch table for the server. */ static struct packet_type packets[] = { - {"ncom", PC(NewPlayerCmd) PF_PLAYING }, - {"command", PC(PlayerCmd) PF_PLAYING }, + {"ncom", PC(NewPlayerCmd) PF_PLAYING | PF_COMMAND6 }, + {"command", PC(PlayerCmd) PF_PLAYING | PF_COMMAND0 }, {"examine", PC(ExamineCmd) PF_PLAYING }, {"apply", PC(ApplyCmd) PF_PLAYING }, @@ -203,7 +204,7 @@ {"reply", SC(ReplyCmd) 0 }, {"exti", SC(ExtiCmd) 0 }, /* CF+ */ {"addme", SC(AddMeCmd) 0 }, - {"askface", SC(SendFaceCmd) 0 }, /* Added: phil */ + {"askface", SC(AskFaceCmd) 0 }, {"requestinfo", SC(RequestInfo) 0 }, {"setfacemode", SC(SetFaceMode) 0 }, {"setsound", SC(SetSound) 0 }, @@ -221,10 +222,38 @@ && (!(pkt->flags & PF_PLAYING) || state == ST_PLAYING); } +// HACK: some commands currently should be executed +// even when the player is frozen. this hack detects +// those commands. it should be folded into may_execute, +// but kept seperate to emphasise the hack aspect, i.e. +// do it better, then remove. +static bool +always_immediate (const client *ns, const packet_type *pkt, const char *data, int len) +{ + if (!(pkt->flags & (PF_COMMAND0 | PF_COMMAND6))) + return false; + + if (!ns->pl || !ns->pl->ob || !ns->pl->ob->map) + return false; + + if (pkt->flags & PF_COMMAND6) + { + data += 6; + len -= 6; + } + + if (len > 4 && !strncmp (data, "say " , 4)) + return true; + if (len > 5 && !strncmp (data, "chat ", 5)) + return true; + + return false; +} + void client::execute (const packet_type *pkt, char *data, int datalen) { - if (may_execute (pkt)) + if (may_execute (pkt) || always_immediate (this, pkt, data, datalen)) { //TODO: only one format if (pkt->flags & PF_PLAYER) @@ -275,7 +304,7 @@ for (packet_type *pkt = packets; pkt < packets + (sizeof (packets) / sizeof (packets[0])); ++pkt) if (!strcmp ((char *)inbuf + 2, pkt->name)) { - if (pkt->flags & PF_PLAYER) + if (pkt->flags & PF_PLAYER && !always_immediate (this, pkt, data, datalen)) queue_command (pkt, data, datalen); else execute (pkt, data, datalen); @@ -382,16 +411,16 @@ client::send (void *buf_, int len) { char *buf = (char *)buf_; - char *pos = buf; - int amt = 0; if (destroyed () || !buf) return; - if ((len + outputbuffer.len) > SOCKETBUFSIZE) + if (len + outputbuffer.len > SOCKETBUFSIZE) { LOG (llevDebug, "socket on fd %d has overrun internal buffer - marking as dead\n", fd); - destroy (); + // shutdown the socket, this is safer than destroying it immediately + // as lots of code in the callchain might still access the map etc. + shutdown (fd, SHUT_RDWR); return; } @@ -477,6 +506,12 @@ send_packet (sl); } +void +client::send_drawinfo (const char *msg, int flags) +{ + send_packet_printf ("drawinfo %d %s", flags, msg); +} + /*********************************************************************** * * packet functions/utilities @@ -492,6 +527,24 @@ *cur++ = ' '; } +packet &packet::operator <<(const ber32 v) +{ + enum { maxlen = 32 / 7 + 1}; + uint8 buf[maxlen]; + uint8 *p = buf + maxlen; + uint32 val = v.val; + + while (val > 0x7F) + { + *--p = (val & 0x7F) | 0x80; + val >>= 7; + } + + *--p = val; + + return *this << data (p, buf + maxlen - p); +} + packet &packet::operator <<(const data &v) { if (room () < v.len)