From 381684059a6481d7a507ae8c690a873aabe6a4cb Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Mon, 23 Jun 2014 11:50:21 -0700 Subject: Remove vomit --- src/net/packets.cpp | 37 ++++--- src/net/packets.hpp | 37 ------- src/net/socket.cpp | 29 +----- src/net/vomit.cpp | 21 ---- src/net/vomit.hpp | 281 ---------------------------------------------------- 5 files changed, 27 insertions(+), 378 deletions(-) delete mode 100644 src/net/vomit.cpp delete mode 100644 src/net/vomit.hpp diff --git a/src/net/packets.cpp b/src/net/packets.cpp index 112bed0..fbec8c4 100644 --- a/src/net/packets.cpp +++ b/src/net/packets.cpp @@ -21,32 +21,42 @@ #include "../io/cxxstdio.hpp" #include "../io/write.hpp" -#include "vomit.hpp" - #include "../poison.hpp" size_t packet_avail(Session *s) { - return RFIFOREST(s); + return s->rdata_size - s->rdata_pos; } bool packet_fetch(Session *s, size_t offset, Byte *data, size_t sz) { - if (RFIFOREST(s) < offset + sz) + if (packet_avail(s) < offset + sz) return false; - const Byte *start = reinterpret_cast(RFIFOP(s, offset)); + const Byte *start = reinterpret_cast(&s->rdata[s->rdata_pos + offset]); const Byte *end = start + sz; std::copy(start, end, data); return true; } void packet_discard(Session *s, size_t sz) { - RFIFOSKIP(s, sz); + s->rdata_pos += sz; + + assert (s->rdata_size >= s->rdata_pos); } bool packet_send(Session *s, const Byte *data, size_t sz) { - WFIFOSET(s, sz); - Byte *end = reinterpret_cast(WFIFOP(s, 0)); + if (s->wdata_size + sz > s->max_wdata) + { + realloc_fifo(s, s->max_rdata, s->max_wdata << 1); + PRINTF("socket: %d wdata expanded to %zu bytes.\n"_fmt, s, s->max_wdata); + } + if (!s->max_wdata || !s->wdata) + { + return false; + } + s->wdata_size += sz; + + Byte *end = reinterpret_cast(&s->wdata[s->wdata_size + 0]); Byte *start = end - sz; std::copy(data, data + sz, start); return true; @@ -58,13 +68,16 @@ void packet_dump(io::WriteFile& logfp, Session *s) "---- 00-01-02-03-04-05-06-07 08-09-0A-0B-0C-0D-0E-0F\n"_fmt); char tmpstr[16 + 1] {}; int i; - for (i = 0; i < RFIFOREST(s); i++) + for (i = 0; i < packet_avail(s); i++) { if ((i & 15) == 0) FPRINTF(logfp, "%04X "_fmt, i); - FPRINTF(logfp, "%02x "_fmt, RFIFOB(s, i)); - if (RFIFOB(s, i) > 0x1f) - tmpstr[i % 16] = RFIFOB(s, i); + Byte rfifob_ib; + packet_fetch(s, i, &rfifob_ib, 1); + uint8_t rfifob_i = rfifob_ib.value; + FPRINTF(logfp, "%02x "_fmt, rfifob_i); + if (rfifob_i > 0x1f) + tmpstr[i % 16] = rfifob_i; else tmpstr[i % 16] = '.'; if ((i - 7) % 16 == 0) // -8 + 1 diff --git a/src/net/packets.hpp b/src/net/packets.hpp index 4bb412f..6d37478 100644 --- a/src/net/packets.hpp +++ b/src/net/packets.hpp @@ -82,43 +82,6 @@ void send_buffer(Session *s, const Buffer& buffer) s->set_eof(); } -template -__attribute__((warn_unused_result)) -SendResult net_send_fpacket(Session *s, const NetPacket_Fixed& fixed) -{ - bool ok = packet_send(s, reinterpret_cast(&fixed), sizeof(NetPacket_Fixed)); - return ok ? SendResult::Success : SendResult::Fail; -} - -template -__attribute__((warn_unused_result)) -SendResult net_send_ppacket(Session *s, const NetPacket_Payload& payload) -{ - bool ok = packet_send(s, reinterpret_cast(&payload), sizeof(NetPacket_Payload)); - return ok ? SendResult::Success : SendResult::Fail; -} - -template -__attribute__((warn_unused_result)) -SendResult net_send_vpacket(Session *s, const NetPacket_Head& head, const std::vector>& repeat) -{ - bool ok = packet_send(s, reinterpret_cast(&head), sizeof(NetPacket_Head)); - ok &= packet_send(s, reinterpret_cast(repeat.data()), repeat.size() * sizeof(NetPacket_Repeat)); - return ok ? SendResult::Success : SendResult::Fail; -} - -template -__attribute__((warn_unused_result)) -SendResult net_send_opacket(Session *s, const NetPacket_Head& head, bool has_opt, const NetPacket_Option& opt) -{ - bool ok = packet_send(s, reinterpret_cast(&head), sizeof(NetPacket_Head)); - if (has_opt) - { - ok &= packet_send(s, reinterpret_cast(&opt), sizeof(NetPacket_Option)); - } - return ok ? SendResult::Success : SendResult::Fail; -} - template __attribute__((warn_unused_result)) RecvResult net_recv_fpacket(Session *s, NetPacket_Fixed& fixed) diff --git a/src/net/socket.cpp b/src/net/socket.cpp index e9e819e..54f9822 100644 --- a/src/net/socket.cpp +++ b/src/net/socket.cpp @@ -1,5 +1,4 @@ #include "socket.hpp" -#include "vomit.hpp" // for remaining FIFO functions // socket.cpp - Network event system. // // Copyright © ????-2004 Athena Dev Teams @@ -121,8 +120,8 @@ IteratorPair> iter_fds() inline void RFIFOFLUSH(Session *s) { - really_memmove(&s->rdata[0], &s->rdata[s->rdata_pos], RFIFOREST(s)); - s->rdata_size = RFIFOREST(s); + really_memmove(&s->rdata[0], &s->rdata[s->rdata_pos], s->rdata_size - s->rdata_pos); + s->rdata_size -= s->rdata_pos; s->rdata_pos = 0; } @@ -401,19 +400,6 @@ void realloc_fifo(Session *s, size_t rfifo_size, size_t wfifo_size) } } -void WFIFOSET(Session *s, size_t len) -{ - if (s->wdata_size + len + 16384 > s->max_wdata) - { - realloc_fifo(s, s->max_rdata, s->max_wdata << 1); - PRINTF("socket: %d wdata expanded to %zu bytes.\n"_fmt, s, s->max_wdata); - } - if (s->wdata_size + len + 2048 < s->max_wdata) - s->wdata_size += len; - else - FPRINTF(stderr, "socket: %d wdata lost !!\n"_fmt, s), abort(); -} - void do_sendrecv(interval_t next_ms) { bool any = false; @@ -499,14 +485,3 @@ void do_parsepacket(void) RFIFOFLUSH(s); } } - -void RFIFOSKIP(Session *s, size_t len) -{ - s->rdata_pos += len; - - if (s->rdata_size < s->rdata_pos) - { - FPRINTF(stderr, "too many skip\n"_fmt); - abort(); - } -} diff --git a/src/net/vomit.cpp b/src/net/vomit.cpp deleted file mode 100644 index 2057022..0000000 --- a/src/net/vomit.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "vomit.hpp" -// vomit.cpp - sickening socket buffer accessors -// -// Copyright © 2014 Ben Longbons -// -// This file is part of The Mana World (Athena server) -// -// 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 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 GNU General Public License -// along with this program. If not, see . - -#include "../poison.hpp" diff --git a/src/net/vomit.hpp b/src/net/vomit.hpp deleted file mode 100644 index 84c54ac..0000000 --- a/src/net/vomit.hpp +++ /dev/null @@ -1,281 +0,0 @@ -#ifndef TMWA_NET_VOMIT_HPP -#define TMWA_NET_VOMIT_HPP -// vomit.hpp - sickening socket accessors -// -// Copyright © ????-2004 Athena Dev Teams -// Copyright © 2004-2011 The Mana World Development Team -// Copyright © 2011-2014 Ben Longbons -// -// This file is part of The Mana World (Athena server) -// -// 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 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 GNU General Public License -// along with this program. If not, see . - -# include "fwd.hpp" - -# include "socket.hpp" - -// these first three are really in socket.cpp -// but here for cleanliness - -/// Check how much can be read -inline -size_t RFIFOREST(Session *s) -{ - return s->rdata_size - s->rdata_pos; -} - -/// Done reading -void RFIFOSKIP(Session *s, size_t len); - -/// Finish writing -void WFIFOSET(Session *s, size_t len); - - -template -uint8_t *pod_addressof_m(T& structure) -{ - static_assert(is_trivially_copyable::value, "Can only byte-copy POD-ish structs"); - return &reinterpret_cast(structure); -} - -template -const uint8_t *pod_addressof_c(const T& structure) -{ - static_assert(is_trivially_copyable::value, "Can only byte-copy POD-ish structs"); - return &reinterpret_cast(structure); -} - -/// Read from the queue -inline -const void *RFIFOP(Session *s, size_t pos) -{ - return &s->rdata[s->rdata_pos + pos]; -} -inline -uint8_t RFIFOB(Session *s, size_t pos) -{ - return *static_cast(RFIFOP(s, pos)); -} -inline -uint16_t RFIFOW(Session *s, size_t pos) -{ - return *static_cast(RFIFOP(s, pos)); -} -inline -uint32_t RFIFOL(Session *s, size_t pos) -{ - return *static_cast(RFIFOP(s, pos)); -} -template -void RFIFO_STRUCT(Session *s, size_t pos, T& structure) -{ - really_memcpy(pod_addressof_m(structure), static_cast(RFIFOP(s, pos)), sizeof(T)); -} -inline -IP4Address RFIFOIP(Session *s, size_t pos) -{ - IP4Address o; - RFIFO_STRUCT(s, pos, o); - return o; -} -template -inline -VString RFIFO_STRING(Session *s, size_t pos) -{ - const char *const begin = static_cast(RFIFOP(s, pos)); - const char *const end = begin + len-1; - const char *const mid = std::find(begin, end, '\0'); - return XString(begin, mid, nullptr); -} -inline -AString RFIFO_STRING(Session *s, size_t pos, size_t len) -{ - const char *const begin = static_cast(RFIFOP(s, pos)); - const char *const end = begin + len; - const char *const mid = std::find(begin, end, '\0'); - return XString(begin, mid, nullptr); -} -inline -void RFIFO_BUF_CLONE(Session *s, uint8_t *buf, size_t len) -{ - really_memcpy(buf, static_cast(RFIFOP(s, 0)), len); -} - -/// Read from an arbitrary buffer -inline -const void *RBUFP(const uint8_t *p, size_t pos) -{ - return p + pos; -} -inline -uint8_t RBUFB(const uint8_t *p, size_t pos) -{ - return *static_cast(RBUFP(p, pos)); -} -inline -uint16_t RBUFW(const uint8_t *p, size_t pos) -{ - return *static_cast(RBUFP(p, pos)); -} -inline -uint32_t RBUFL(const uint8_t *p, size_t pos) -{ - return *static_cast(RBUFP(p, pos)); -} -template -void RBUF_STRUCT(const uint8_t *p, size_t pos, T& structure) -{ - really_memcpy(pod_addressof_m(structure), p + pos, sizeof(T)); -} -inline -IP4Address RBUFIP(const uint8_t *p, size_t pos) -{ - IP4Address o; - RBUF_STRUCT(p, pos, o); - return o; -} -template -inline -VString RBUF_STRING(const uint8_t *p, size_t pos) -{ - const char *const begin = static_cast(RBUFP(p, pos)); - const char *const end = begin + len-1; - const char *const mid = std::find(begin, end, '\0'); - return XString(begin, mid, nullptr); -} -inline -AString RBUF_STRING(const uint8_t *p, size_t pos, size_t len) -{ - const char *const begin = static_cast(RBUFP(p, pos)); - const char *const end = begin + len; - const char *const mid = std::find(begin, end, '\0'); - return XString(begin, mid, nullptr); -} - - -/// Unused - check how much data can be written -// the existence of this seems scary -inline -size_t WFIFOSPACE(Session *s) -{ - return s->max_wdata - s->wdata_size; -} -/// Write to the queue -inline -void *WFIFOP(Session *s, size_t pos) -{ - return &s->wdata[s->wdata_size + pos]; -} -inline -uint8_t& WFIFOB(Session *s, size_t pos) -{ - return *static_cast(WFIFOP(s, pos)); -} -inline -uint16_t& WFIFOW(Session *s, size_t pos) -{ - return *static_cast(WFIFOP(s, pos)); -} -inline -uint32_t& WFIFOL(Session *s, size_t pos) -{ - return *static_cast(WFIFOP(s, pos)); -} -template -void WFIFO_STRUCT(Session *s, size_t pos, T& structure) -{ - really_memcpy(static_cast(WFIFOP(s, pos)), pod_addressof_c(structure), sizeof(T)); -} -inline -IP4Address& WFIFOIP(Session *s, size_t pos) -{ - static_assert(is_trivially_copyable::value, "That was the whole point"); - return *static_cast(WFIFOP(s, pos)); -} -inline -void WFIFO_STRING(Session *s, size_t pos, XString str, size_t len) -{ - char *const begin = static_cast(WFIFOP(s, pos)); - char *const end = begin + len; - char *const mid = std::copy(str.begin(), str.end(), begin); - std::fill(mid, end, '\0'); -} -inline -void WFIFO_ZERO(Session *s, size_t pos, size_t len) -{ - uint8_t *b = static_cast(WFIFOP(s, pos)); - uint8_t *e = b + len; - std::fill(b, e, '\0'); -} -inline -void WFIFO_BUF_CLONE(Session *s, const uint8_t *buf, size_t len) -{ - really_memcpy(static_cast(WFIFOP(s, 0)), buf, len); -} - -/// Write to an arbitrary buffer -inline -void *WBUFP(uint8_t *p, size_t pos) -{ - return p + pos; -} -inline -uint8_t& WBUFB(uint8_t *p, size_t pos) -{ - return *static_cast(WBUFP(p, pos)); -} -inline -uint16_t& WBUFW(uint8_t *p, size_t pos) -{ - return *static_cast(WBUFP(p, pos)); -} -inline -uint32_t& WBUFL(uint8_t *p, size_t pos) -{ - return *static_cast(WBUFP(p, pos)); -} -template -void WBUF_STRUCT(uint8_t *p, size_t pos, T& structure) -{ - really_memcpy(p + pos, pod_addressof_c(structure), sizeof(T)); -} -inline -IP4Address& WBUFIP(uint8_t *p, size_t pos) -{ - return *static_cast(WBUFP(p, pos)); -} -inline -void WBUF_STRING(uint8_t *p, size_t pos, XString s, size_t len) -{ - char *const begin = static_cast(WBUFP(p, pos)); - char *const end = begin + len; - char *const mid = std::copy(s.begin(), s.end(), begin); - std::fill(mid, end, '\0'); -} -inline -void WBUF_ZERO(uint8_t *p, size_t pos, size_t len) -{ - uint8_t *b = static_cast(WBUFP(p, pos)); - uint8_t *e = b + len; - std::fill(b, e, '\0'); -} - -inline -void RFIFO_WFIFO_CLONE(Session *rs, Session *ws, size_t len) -{ - really_memcpy(static_cast(WFIFOP(ws, 0)), - static_cast(RFIFOP(rs, 0)), len); -} - -#endif // TMWA_NET_VOMIT_HPP -- cgit v1.2.3-60-g2f50