From d2d7c82ca459b3f97aaab825ef10de4e42b330e4 Mon Sep 17 00:00:00 2001 From: Bubblegumdrop Date: Sun, 16 Jan 2022 17:55:26 -0500 Subject: [PATCH] Split SafeUDPpacket and SafeUDPpacketV. SafeUDPpacketV is much more complex due to the UDP packet splitting machinery. --- Makefile | 2 +- include/SafeUDPpacket.h | 56 ------------------------------- src/SafeUDPpacket.cpp | 80 --------------------------------------------- test/test_SafeUDPpacket.cpp | 2 +- 4 files changed, 2 insertions(+), 138 deletions(-) delete mode 100644 src/SafeUDPpacket.cpp diff --git a/Makefile b/Makefile index 366cb16..5b2c768 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,7 @@ COMMON_OBJS := \ $(OBJDIR)/src/lib_SDL_Log.o \ $(OBJDIR)/src/signal_common.o \ $(OBJDIR)/src/UDPbase.o \ - $(OBJDIR)/src/SafeUDPpacket.o \ + $(OBJDIR)/src/SafeUDPpacketV.o \ $(OBJDIR)/src/util.o SERVER_OBJS := \ diff --git a/include/SafeUDPpacket.h b/include/SafeUDPpacket.h index bd8ad86..ea522f9 100644 --- a/include/SafeUDPpacket.h +++ b/include/SafeUDPpacket.h @@ -86,59 +86,3 @@ class SafeUDPpacket Uint8 *data (Uint8* data) const { return get()->data = data; } Uint8 *data (void) const { return get()->data; } }; - -class SafeUDPpacketV -{ - std::vector m_packetV; - public: - SafeUDPpacketV (const void*, const int, const int mtu = 1450); - SafeUDPpacketV (int howmany, int size) - { - UDPpacket** p; - m_packetV.reserve (howmany); - if (NULL == (p = SDLNet_AllocPacketV (howmany, size))) - { - throw "SDLNet_AllocPacketV: " + std::string (SDLNet_GetError ()); - } - else - { - for (; (p != NULL) && (*p != NULL); p++) - { - m_packetV.push_back (SafeUDPpacket (*p)); - } - SDL_assert ((m_packetV.size () > 0) && (howmany == (int)m_packetV.size ())); - /* - * SDLNet_FreePacketV just calls SDLNet_FreePacket on each packet. - * The "packetV" itself is just something allocated on the heap. - * We don't need this any longer. - */ - SDL_free (p); - } - } - int Send (UDPsocket sock) - { - size_t size; - int numsent; - UDPpacket** pV; - size = m_packetV.size (); - if (NULL != (pV = (UDPpacket**)SDL_malloc (sizeof (UDPpacket*) * size))) - { - for (size_t i = 0; i < size; i++) - pV[i] = m_packetV[i].get (); - numsent = SDLNet_UDP_SendV (sock, pV, size); - SDL_free (pV); - } - return numsent; - } - - const SafeUDPpacket& get (size_t idx) { return m_packetV.at (idx); } - size_t size (void) const { return m_packetV.size (); } - - void channel (int channel) { for (auto& p : m_packetV) { p.channel (channel); } } - void len (int len) { for (auto& p : m_packetV) { p.len (len); } } - void maxlen (int maxlen) { for (auto& p : m_packetV) { p.maxlen (maxlen); } } - void status (int status) { for (auto& p : m_packetV) { p.status (status); } } - void address (IPaddress address) { for (auto& p : m_packetV) { p.address (address); } } - - std::string toString (void); -}; diff --git a/src/SafeUDPpacket.cpp b/src/SafeUDPpacket.cpp deleted file mode 100644 index a58bdd6..0000000 --- a/src/SafeUDPpacket.cpp +++ /dev/null @@ -1,80 +0,0 @@ -#include "SafeUDPpacket.h" - -SafeUDPpacketV::SafeUDPpacketV (const void* buf, const int size, const int mtu) -{ - int howmany, i, len, numsent; - const uint8_t* bufPtr; - const uint8_t* bufPtrEnd; - - SDL_assert (NULL != buf); - - SDL_assert (mtu > 0); - howmany = (int) ceil ((double) size / (double) mtu); - - i = 0; - numsent = 0; - bufPtr = (const uint8_t*) buf; - bufPtrEnd = bufPtr + size; - while ((i < howmany) && (bufPtr < bufPtrEnd) && (numsent < size)) - { - SafeUDPpacket p (mtu); - - len = size - numsent; - if (len > mtu) - len = mtu; - /* Use SDLNet_UDP_Send to send an empty packet instead... it's probably simpler than all this machinery. */ - if (len < 1) - break; - - /* Clear the entire data ... TODO */ - p.len (mtu); - p.clear (); - - /* Set the actual length */ - p.len (len); - p.maxlen (size); - - /* only memcpy in the relevant bits */ - p.slurp (bufPtr, len); - - m_packetV.push_back (std::move (p)); - - /* Advance */ - bufPtr += len; - numsent += len; - i++; - } - - SDL_assert (howmany == (int)m_packetV.size ()); -} - - -std::string -SafeUDPpacketV::toString (void) -{ - int howmany, maxlen, mtu, numrecv; - char* bufPtr; - char* bufPtrEnd; - std::string buf; - - howmany = m_packetV.size (); - SDL_assert (howmany > 0); - - maxlen = m_packetV[0].maxlen (); - SDL_assert (maxlen > 0); - buf.reserve (maxlen); - bufPtr = &buf[0]; - bufPtrEnd = bufPtr + maxlen; - - for (numrecv = 0; (numrecv < howmany) && (bufPtr < bufPtrEnd); numrecv++) - { - mtu = m_packetV[numrecv].len (); - SDL_assert (mtu > 0); - memcpy (bufPtr, m_packetV[numrecv].data (), mtu); - bufPtr += mtu; - } - - SDL_assert (numrecv == howmany); - - return buf; -} diff --git a/test/test_SafeUDPpacket.cpp b/test/test_SafeUDPpacket.cpp index 0e2b38a..8cdf8ff 100644 --- a/test/test_SafeUDPpacket.cpp +++ b/test/test_SafeUDPpacket.cpp @@ -8,7 +8,7 @@ #include "unused.h" #include "UDPbase.h" #include "FileIO.h" -#include "SafeUDPpacket.h" +#include "SafeUDPpacketV.h" #include "lib_GetOpt.h" #include