From 764ebe4ce2d1e2e423a924ffe32af45946bda56e Mon Sep 17 00:00:00 2001 From: Bubblegumdrop Date: Sat, 8 Jan 2022 21:23:42 -0500 Subject: [PATCH] Another Buffer_To_UDPPacketV. Yet to test. Split UDPbase out. Change ShaderProgram to std::unique_ptr. I think I may change Buffer_To_UDPPacketV to some kind of RAII style code. --- Makefile | 1 + include/FileIO.h | 1 - include/UDPbase.h | 59 +++++++-------------------------------------- include/shader.h | 1 - include/util.h | 3 ++- src/FileIO.cpp | 8 +------ src/Server.cpp | 2 +- src/UDPbase.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/client_main.cpp | 24 +++++++++---------- src/shader.cpp | 5 ---- src/util.cpp | 66 +++++++++++++++++++++++++++++++++++++++++--------- 11 files changed, 150 insertions(+), 89 deletions(-) create mode 100644 src/UDPbase.cpp diff --git a/Makefile b/Makefile index b1c7cae..681da51 100644 --- a/Makefile +++ b/Makefile @@ -32,6 +32,7 @@ COMMON_OBJS := \ $(OBJDIR)/src/lib_lua_common.o \ $(OBJDIR)/src/lib_SDL_common.o \ $(OBJDIR)/src/signal_common.o \ + $(OBJDIR)/src/UDPbase.o \ $(OBJDIR)/src/util.o SERVER_OBJS := \ diff --git a/include/FileIO.h b/include/FileIO.h index ab9b96a..6a26326 100644 --- a/include/FileIO.h +++ b/include/FileIO.h @@ -16,7 +16,6 @@ class FileIO FILE* fp; std::string path; public: - FileIO (FILE* f); FileIO (const std::string&, const std::string&); ~FileIO (void); FILE *Open (const std::string&, const std::string&); diff --git a/include/UDPbase.h b/include/UDPbase.h index 12b68eb..18b7a86 100644 --- a/include/UDPbase.h +++ b/include/UDPbase.h @@ -3,59 +3,18 @@ #include #include -#define MAX_PACKET_SIZE 250 +#define DEFAULT_MAX_PACKET_SIZE 250 class UDPbase { IPaddress addr; public: - UDPbase (const char* host, const Uint16 port) - { - if (SDLNet_ResolveHost (&addr, host, SDLNet_Read16 (&port)) < 0) - { - throw "SDLNet_ResolveHost: " + std::string (SDLNet_GetError ()); - } - } - ~UDPbase (void) - { - } - UDPsocket Open (void) - { - return Open (addr.port); - } - UDPsocket Open (const Uint16 port) - { - UDPsocket sock = NULL; - IPaddress* addr; - if (NULL == (sock = SDLNet_UDP_Open (port))) - { - throw "SDLNet_UDP_Open: " + std::string (SDLNet_GetError ()); - } - if (NULL != (addr = SDLNet_UDP_GetPeerAddress (sock, -1))) - { - SDL_Log ("Opened UDP port %d", SDLNet_Read16 (&addr->port)); - } - return sock; - } - int Bind (UDPsocket sock, const int channel) - { - int binding; - SDL_assert (NULL != sock); - if (-1 == (binding = SDLNet_UDP_Bind (sock, channel, &addr))) - { - throw "SDLNet_UDP_Bind: " + std::string (SDLNet_GetError ()); - } - SDL_Log ("Binding UDP socket %s ch%d", this->toString ().c_str (), binding); - return binding; - } - void Close (UDPsocket sock) - { - SDL_assert (NULL != sock); - SDLNet_UDP_Close (sock); - sock = NULL; - } - std::string toString (void) - { - return std::string (SDLNet_ResolveIP (&addr)) + ":" + std::to_string (addr.port); - } + UDPbase (const char* host, const Uint16 port); + ~UDPbase (void); + UDPsocket Open (void); + UDPsocket Open (const Uint16 port); + void Close (UDPsocket sock); + int Bind (UDPsocket sock, const int channel); + void Unbind (UDPsocket sock, const int channel); + std::string toString (void); }; diff --git a/include/shader.h b/include/shader.h index e4548fd..30516c9 100644 --- a/include/shader.h +++ b/include/shader.h @@ -15,7 +15,6 @@ class ShaderProgram static GLint my_checkCompileSuccess (const GLuint, const GLenum); public: GLuint ID; - ShaderProgram (void); ShaderProgram (const std::string&, const std::string&, const std::string&); GLint getCachedLoc (const std::string&); void RefreshUniformLocations (void); diff --git a/include/util.h b/include/util.h index 6178a9d..7363904 100644 --- a/include/util.h +++ b/include/util.h @@ -12,4 +12,5 @@ int util_IsProbablyAscii (const void *, const size_t); void util_HexDump (const void *, const size_t); void util_UDP_DumpPacket (UDPpacket*); -int util_UDP_ApproximatePacketVSize (UDPpacket*); + +UDPpacket** Buffer_To_UDPPacketV (UDPpacket* src, const void* buf, const size_t size); diff --git a/src/FileIO.cpp b/src/FileIO.cpp index 95dd8df..cc630dc 100644 --- a/src/FileIO.cpp +++ b/src/FileIO.cpp @@ -9,12 +9,6 @@ #include /* stat */ #include /* stat */ -FileIO::FileIO (FILE* f) -{ - fp = f; -} - - FileIO::FileIO (const std::string& filename, const std::string& mode) { if (!(fp = Open (filename, mode))) @@ -111,7 +105,7 @@ std::string FileIO::ReadToString (void) { off_t size = Size (); - std::string b (size + 1, 0); + std::string b (size, 0); Read (&b[0], size); return b; } diff --git a/src/Server.cpp b/src/Server.cpp index 7795fba..8d48b68 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -74,7 +74,7 @@ Server::AllocateApproximateBufferSpace (const int nclients) SDL_Log ("Serving %d clients", nclients); - if (!(UDPPacketV = SDLNet_AllocPacketV (nclients, MAX_PACKET_SIZE))) + if (!(UDPPacketV = SDLNet_AllocPacketV (nclients, DEFAULT_MAX_PACKET_SIZE))) { SDL_Log ("SDLNet_AllocPacketV: %s", SDLNet_GetError ()); } diff --git a/src/UDPbase.cpp b/src/UDPbase.cpp new file mode 100644 index 0000000..ff4968c --- /dev/null +++ b/src/UDPbase.cpp @@ -0,0 +1,69 @@ +#include "UDPbase.h" + +UDPbase::UDPbase (const char* host, const Uint16 port) +{ + if (SDLNet_ResolveHost (&addr, host, SDLNet_Read16 (&port)) < 0) + { + throw "SDLNet_ResolveHost: " + std::string (SDLNet_GetError ()); + } +} + + +UDPbase::~UDPbase (void) +{ +} + + +UDPsocket UDPbase::Open (void) +{ + return Open (addr.port); +} + + +UDPsocket UDPbase::Open (const Uint16 port) +{ + UDPsocket sock = NULL; + IPaddress* addr; + if (NULL == (sock = SDLNet_UDP_Open (port))) + { + throw "SDLNet_UDP_Open: " + std::string (SDLNet_GetError ()); + } + if (NULL != (addr = SDLNet_UDP_GetPeerAddress (sock, -1))) + { + SDL_Log ("Opened UDP port %d", SDLNet_Read16 (&addr->port)); + } + return sock; +} + + +int UDPbase::Bind (UDPsocket sock, const int channel) +{ + int binding; + SDL_assert (NULL != sock); + if (-1 == (binding = SDLNet_UDP_Bind (sock, channel, &addr))) + { + throw "SDLNet_UDP_Bind: " + std::string (SDLNet_GetError ()); + } + SDL_Log ("Binding UDP socket %s ch%d", this->toString ().c_str (), binding); + return binding; +} + + +void UDPbase::Unbind (UDPsocket sock, const int channel) +{ + SDLNet_UDP_Unbind (sock, channel); +} + + +void UDPbase::Close (UDPsocket sock) +{ + SDL_assert (NULL != sock); + SDLNet_UDP_Close (sock); + sock = NULL; +} + + +std::string UDPbase::toString (void) +{ + return std::string (SDLNet_ResolveIP (&addr)) + ":" + std::to_string (addr.port); +} diff --git a/src/client_main.cpp b/src/client_main.cpp index b3f7a63..e5061cd 100644 --- a/src/client_main.cpp +++ b/src/client_main.cpp @@ -83,9 +83,9 @@ struct Client_State static std::unique_ptr cube; static std::unique_ptr quad; static std::unique_ptr client; +static std::unique_ptr shader; static Camera camera; -static ShaderProgram shader; static struct Client_State state; static struct Uniform_Values values; @@ -97,8 +97,8 @@ main (int argc, char **argv) my_Init (argc, argv); - shader = ShaderProgram ("./glsl/camera.v.glsl", "", "./glsl/new.f.glsl"); - if (!shader.ID) + shader = std::make_unique ("./glsl/camera.v.glsl", "", "./glsl/new.f.glsl"); + if (!shader->ID) { SDL_Log ("Unable to load shader."); goto _out; @@ -153,10 +153,10 @@ main (int argc, char **argv) my_UpdateValues (); - shader.Use (); - shader.Mat4 ("Projection", projection); - shader.Mat4 ("View", view); - shader.Mat4 ("Model", model); + shader->Use (); + shader->Mat4 ("Projection", projection); + shader->Mat4 ("View", view); + shader->Mat4 ("Model", model); my_Render (); SDL_GL_SwapWindow (state.Window); @@ -214,11 +214,11 @@ my_UpdateValues (void) values.iResolution.y = state.Window_Height; values.iResolution.z = 1.0; - shader.Use (); - shader.Int ("iFrame", values.iFrame); - shader.Float ("iTime", values.iTime); - shader.Float ("iTimeDelta", values.iTimeDelta); - shader.Vec3 ("iResolution", values.iResolution); + shader->Use (); + shader->Int ("iFrame", values.iFrame); + shader->Float ("iTime", values.iTime); + shader->Float ("iTimeDelta", values.iTimeDelta); + shader->Vec3 ("iResolution", values.iResolution); } diff --git a/src/shader.cpp b/src/shader.cpp index c595228..4e3c38d 100644 --- a/src/shader.cpp +++ b/src/shader.cpp @@ -12,11 +12,6 @@ #define DEFAULT_FRAGMENT_HEADER "./glsl/header.f.glsl" -ShaderProgram::ShaderProgram (void) -{ -} - - ShaderProgram::ShaderProgram (const std::string& vertexPath, const std::string& geometryPath, const std::string& fragmentPath) { GLuint id = Compile3ShaderBuffers (vertexPath, geometryPath, fragmentPath); diff --git a/src/util.cpp b/src/util.cpp index 52d0596..8932281 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -95,18 +95,62 @@ util_UDP_DumpPacket (UDPpacket* packet) /* - * You can get the number of packets in a sorta hacky way if your packet is set up correctly. - * - * UDPpacket** packetV = SDLNet_AllocPacketV (1, n); - * UDP_ApproximatePacketVSize (packetV[0]); - * - * It relies on the maxlen field being set to the total length of the - * data, with the len field being set to the MTU. + * UDPsock sock; + * FileIO f (path, "r"); + * std::string buf = f.ReadToString (); + * UDPpacket* packet = SDL_AllocPacket (buf.length ()); + * packet->len = DEFAULT_MAX_PACKET_SIZE; + * packet->address = ipaddress; + * packet->channel = -1; + * UDPpacket** packetV = Buffer_To_UDPPacketV (packet, &buf[0], buf.length ()); + * SDLNet_UDP_SendV (sock, packetV, packetV[0].status); + * SDLNet_FreePacket (packet); + * SDLNet_FreePacketV (packetV); */ -int -util_UDP_ApproximatePacketVSize (UDPpacket* src) +UDPpacket** +Buffer_To_UDPPacketV (UDPpacket* src, const void* buf, const size_t size) { + int i, howmany, len, mtu, nsent; + const uint8_t* bufPtr; + const uint8_t* bufPtrEnd; + UDPpacket** packetV; + SDL_assert (NULL != src); - SDL_assert (src->len > 0); - return (int)ceil ((double) src->maxlen / (double) src->len); + SDL_assert (NULL != buf); + + mtu = src->len; + SDL_assert (mtu > 0); + + howmany = (int)ceil ((double) size / (double) mtu); + if (NULL == (packetV = SDLNet_AllocPacketV (howmany, mtu))) + { + SDL_Log ("SDLNet_AllocPacketV: %s", SDLNet_GetError ()); + return NULL; + } + + i = 0; + nsent = size; + bufPtr = (const uint8_t*)buf; + bufPtrEnd = bufPtr + size; + while ((i < howmany) && (bufPtr < bufPtrEnd) && (nsent > 0)) + { + len = nsent; + if (len > mtu) + len = mtu; + + packetV[i]->channel = src->channel; + /* Always zero out the entire data */ + SDL_memset (packetV[i]->data, 0, mtu); + /* But only memcpy in the relevant bits */ + SDL_memcpy (packetV[i]->data, bufPtr, len); + packetV[i]->len = len; + packetV[i]->maxlen = size; + packetV[i]->status = 0; + packetV[i]->address = src->address; + bufPtr += len; + nsent -= len; + i++; + } + + return packetV; }