diff --git a/Makefile b/Makefile index 3b9aa77..366cb16 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/UDPpacketVBuffer.o \ + $(OBJDIR)/src/SafeUDPpacket.o \ $(OBJDIR)/src/util.o SERVER_OBJS := \ @@ -60,7 +60,7 @@ TEST_OBJ_COMMON := \ $(COMMON_OBJS) -TESTS := test_FileIO test_lua test_UDPpacketBufferV +TESTS := test_FileIO test_lua test_SafeUDPpacket TEST_PROGS := $(addprefix $(TEST_BINDIR)/,$(TESTS)) $(shell $(MKDIR) $(dir $(OBJS)) >/dev/null) @@ -160,7 +160,7 @@ $(TEST_BINDIR)/test_FileIO: $(OBJDIR)/test/test_FileIO.o $(TEST_OBJ_COMMON) $(OB $(TEST_BINDIR)/test_lua: $(OBJDIR)/test/test_lua.o $(TEST_OBJ_COMMON) $(OBJDIR)/src/lib_lua_common.o $(LINK.o) $^ $(LDLIBS) -$(TEST_BINDIR)/test_UDPpacketBufferV: $(OBJDIR)/test/test_UDPpacketBufferV.o $(TEST_OBJ_COMMON) $(OBJDIR)/src/UDPbase.o $(OBJDIR)/src/lib_SDL_common.o +$(TEST_BINDIR)/test_SafeUDPpacket: $(OBJDIR)/test/test_SafeUDPpacket.o $(TEST_OBJ_COMMON) $(OBJDIR)/src/UDPbase.o $(OBJDIR)/src/lib_SDL_common.o $(LINK.o) $^ $(LDLIBS) $(OBJDIR)/%.o: %.c diff --git a/glsl/new.f.glsl b/glsl/new.f.glsl index 50bd4ec..507cf57 100644 --- a/glsl/new.f.glsl +++ b/glsl/new.f.glsl @@ -1,21 +1,40 @@ /* vim: set ft=c: */ +# define M_PI (3.141592653589793238462643383279502884) /* pi */ +# define M_PI_2 (1.570796326794896619231321691639751442) /* pi/2 */ +# define M_PI_4 (0.785398163397448309615660845819875721) /* pi/4 */ + /* */ void mainImage (out vec4 fragColor, in vec2 fragCoord) { - // Normalized pixel coordinates (from 0 to 1) - // vec2 uv = fragCoord.xy / iResolution.xy; + const float t = iTime; + const int T = int (3 * (0.5 + 0.5 * cos (t))); - // Texture coordinates - vec2 uv = TexCoords; + // Normalized pixel coordinates (from 0 to 1) + // vec2 uv = fragCoord.xy / iResolution.xy; + + // Texture coordinates + vec2 uv = TexCoords; // float du = 1.0 / iResolution.y; // vec2 uv = du * (fragCoord.xy - (0.5 * iResolution.xy)); // Time varying pixel color - vec3 col = 0.5 + 0.5 * cos (iTime + uv.xyx + vec3 (0, 2, 4)); + vec3 z1 = vec3 ( + sin (t) * cos (t) + , sin (t / M_PI) * cos (t / M_PI) + , sin (t / M_PI / 2) * cos (t / M_PI_2) + ); + vec3 z2 = uv.xyx; + switch (T) + { + case 0: z2 = uv.xxx; break; + case 1: z2 = uv.yyy; break; + case 2: z2 = uv.xyx; break; + } + vec3 col = 0.5 + 0.5 * cos (iTime + z1 + z2); // Output to screen fragColor = vec4 (col, 1.0); diff --git a/include/SafeUDPpacket.h b/include/SafeUDPpacket.h index 1edd8e5..bd8ad86 100644 --- a/include/SafeUDPpacket.h +++ b/include/SafeUDPpacket.h @@ -22,6 +22,19 @@ class SafeUDPpacket m_packet.reset (p); } public: + SafeUDPpacket (const void* buf, int size) + { + UDPpacket* p; + if (NULL == (p = SDLNet_AllocPacket (size))) + { + throw "SDLNet_AllocPacket: " + std::string (SDLNet_GetError ()); + } + else + { + SDL_memcpy (p->data, buf, size); + Reset (p); + } + } SafeUDPpacket (int size) { UDPpacket* p; @@ -29,7 +42,10 @@ class SafeUDPpacket { throw "SDLNet_AllocPacket: " + std::string (SDLNet_GetError ()); } - Reset (p); + else + { + Reset (p); + } } SafeUDPpacket (UDPpacket* p) { @@ -39,7 +55,19 @@ class SafeUDPpacket { return m_packet.get (); } - + void clear (void) + { + SDL_assert (len () > 0); + SDL_memset (data (), 0, len ()); + } + void slurp (const void* buf, const size_t size) + { + SDL_memcpy (data (), buf, size); + } + int Send (UDPsocket sock) + { + return SDLNet_UDP_Send (sock, channel(), get ()); + } int channel (int channel) const { return get()->channel = channel; } int channel (void) const { return get()->channel; } @@ -87,13 +115,30 @@ class SafeUDPpacketV SDL_free (p); } } - const SafeUDPpacket& get (size_t idx) + int Send (UDPsocket sock) { - return m_packetV.at (idx); - } - size_t size (void) const - { - return m_packetV.size (); + 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/include/Server.h b/include/Server.h index 33b507b..c19b2b8 100644 --- a/include/Server.h +++ b/include/Server.h @@ -49,7 +49,7 @@ class Server : public UDPbase } lua_State* getLuaState(void) { return mLuaState; } - UDPsocket getUDPsock (size_t idx) { return UDPsocks.at (idx); } + UDPsocket getUDPsock (size_t idx) { return m_socks.at (idx); } UDPpacket** getPacketV (void) { return packetV; } SDLNet_SocketSet getSocketSet (void) { return SocketSet; } }; diff --git a/include/UDPbase.h b/include/UDPbase.h index fd1aee7..07e3475 100644 --- a/include/UDPbase.h +++ b/include/UDPbase.h @@ -10,48 +10,39 @@ class UDPbase { - int mChannel; - IPaddress mIPAddress; - void - CloseAllSockets (void); + int m_channel; + IPaddress m_address; + void CloseAllSockets (void); protected: - std::vector UDPsocks; + std::vector m_socks; public: - UDPbase (const char *host, const Uint16 port); - UDPbase (const char *host, const Uint16 port, const int channel); + UDPbase (const char*, const Uint16); + UDPbase (const char*, const Uint16, const int); ~UDPbase (void); - int - ResolveHost (IPaddress *, const char *, const Uint16); + int ResolveHost (IPaddress*, const char*, const Uint16); - UDPsocket - UDP_Open (void); - UDPsocket - UDP_Open (const Uint16 port); - void - UDP_Close (UDPsocket sock); - int - UDP_Bind (UDPsocket sock, const int channel); - void - UDP_Unbind (UDPsocket sock, const int channel); - IPaddress * - UDP_GetPeerAddress (UDPsocket sock, const int channel); + UDPsocket UDP_Open (void); + UDPsocket UDP_Open (const Uint16 port); - int - getChannel (void) const + void UDP_Close (UDPsocket sock); + + int UDP_Bind (UDPsocket sock, const int channel); + void UDP_Unbind (UDPsocket sock, const int channel); + + IPaddress* UDP_GetPeerAddress (UDPsocket sock, const int channel); + + /* ======================================================= */ + int channel (void) const { - return - mChannel; - } - UDPsocket - getSocket (const size_t idx) const - { - return - UDPsocks. - at (idx); + return m_channel; } - std::string - toString (void); + UDPsocket get (const size_t idx) const + { + return m_socks.at (idx); + } + + std::string toString (void); }; diff --git a/include/UDPpacketVBuffer.h b/include/UDPpacketVBuffer.h deleted file mode 100644 index b652813..0000000 --- a/include/UDPpacketVBuffer.h +++ /dev/null @@ -1,62 +0,0 @@ -#pragma once - -#include -#include - -/** - -# UDPpacketVBuffer - -Create your `UDPsocket` somewhere: - - UDPsocket sock; - ... - -You have a buffer: - - FileIO f (path, "r"); - std::string buf = f.ReadToString (); - -Allocate a setup packet and set `len` to your MTU, set your destination `address`, and the destination `channel`. -I'm not sure -1 will work as a channel, be sure to set it to your active channel. - - UDPpacket* packet = SDL_AllocPacket (buf.length ()); - packet->len = DEFAULT_MAX_PACKET_SIZE; - packet->channel = -1; - packet->address = GetPeerAddress (sock, packet->channel); - -Use `UDPpacketVBuffer` wrapper to send the data, the destructor will automatically free the `UDPpacket**`: - - UDPpacketVBuffer packetV (&buf[0], buf.length ()); - packetV.Send (sock); - -Then free the leftover setup packet: - - SDLNet_FreePacket (packet); - -*/ -class UDPpacketVBuffer -{ - int m_howmany; - UDPpacket** m_packetV; - public: - UDPpacketVBuffer (UDPpacket* src, const void* buf, const size_t size); - ~UDPpacketVBuffer (void) - { - if (m_packetV) - { - SDLNet_FreePacketV (m_packetV); - m_packetV = NULL; - } - } - int Send (UDPsocket sock) - { - int numsent = 0; - if ((NULL != m_packetV) && (m_howmany > 0)) - { - numsent = SDLNet_UDP_SendV (sock, m_packetV, m_howmany); - } - return numsent; - } - std::string Reassemble (void); -}; diff --git a/src/SafeUDPpacket.cpp b/src/SafeUDPpacket.cpp index d2fbc8b..a58bdd6 100644 --- a/src/SafeUDPpacket.cpp +++ b/src/SafeUDPpacket.cpp @@ -26,15 +26,19 @@ SafeUDPpacketV::SafeUDPpacketV (const void* buf, const int size, const int mtu) if (len < 1) break; - SDL_memset (p.data (), 0, mtu); + /* Clear the entire data ... TODO */ + p.len (mtu); + p.clear (); + + /* Set the actual length */ p.len (len); p.maxlen (size); - /* But only memcpy in the relevant bits */ - SDL_memcpy (p.data (), bufPtr, len); + /* only memcpy in the relevant bits */ + p.slurp (bufPtr, len); + m_packetV.push_back (std::move (p)); - /* But we will want the destination address of this packet */ /* Advance */ bufPtr += len; numsent += len; diff --git a/src/Server.cpp b/src/Server.cpp index 1f120c1..5480d55 100644 --- a/src/Server.cpp +++ b/src/Server.cpp @@ -80,7 +80,7 @@ Server::AllocateApproximateResources (const int nclients) total = 0; for (i = 0; i < nclients; i++) { - numused = SDLNet_UDP_AddSocket (SocketSet, UDPsocks[i]); + numused = SDLNet_UDP_AddSocket (SocketSet, m_socks[i]); if (numused < 0) { SDL_Log ("SDLNet_AddSocket: %s", SDLNet_GetError ()); @@ -89,9 +89,9 @@ Server::AllocateApproximateResources (const int nclients) total += numused; } - if (total != (int)UDPsocks.size ()) + if (total != (int)m_socks.size ()) { - SDL_Log ("Total clients: %d (%ld?)", total, UDPsocks.size ()); + SDL_Log ("Total clients: %d (%ld?)", total, m_socks.size ()); return -1; } @@ -104,7 +104,7 @@ Server::Start (void) { int l; - const int nclients = UDPsocks.size (); + const int nclients = m_socks.size (); if ((l = AllocateApproximateResources (nclients) < 0)) { DEBUG_LOG ("Unable to allocate approximate buffer space"); @@ -148,10 +148,10 @@ Server::Stop (void) if (SocketSet) { - int nclients = UDPsocks.size (); + int nclients = m_socks.size (); for (i = 0; i < nclients; i++) { - SDLNet_UDP_DelSocket (SocketSet, UDPsocks[i]); + SDLNet_UDP_DelSocket (SocketSet, m_socks[i]); } SDLNet_FreeSocketSet (SocketSet); diff --git a/src/UDPbase.cpp b/src/UDPbase.cpp index f51be6b..245f3a6 100644 --- a/src/UDPbase.cpp +++ b/src/UDPbase.cpp @@ -1,23 +1,25 @@ #include "UDPbase.h" +#include /* std::find */ + UDPbase::UDPbase (const char* host, const Uint16 port) { - if (ResolveHost (&mIPAddress, host, port) < 0) + if (ResolveHost (&m_address, host, port) < 0) { throw "ResolveHost failed"; } - mChannel = -1; + m_channel = -1; } /* * Shortcut for clients who want to open a single channel - * The channel will be available in client->getChannel () + * The channel will be available in client->channel () */ UDPbase::UDPbase (const char* host, const Uint16 port, const int channel) { UDPsocket sock; - if (ResolveHost (&mIPAddress, host, port) < 0) + if (ResolveHost (&m_address, host, port) < 0) { throw "ResolveHost failed"; } @@ -25,7 +27,7 @@ UDPbase::UDPbase (const char* host, const Uint16 port, const int channel) { throw "Unable to open client port"; } - if (-1 == (mChannel = UDP_Bind (sock, channel))) + if (-1 == (m_channel = UDP_Bind (sock, channel))) { throw "Unable to bind client port"; } @@ -39,7 +41,7 @@ UDPbase::~UDPbase (void) int -UDPbase::ResolveHost (IPaddress* addr, const char* host, const Uint16 port) +UDPbase::ResolveHost (IPaddress* addr, const char* host, const Uint16 port) { int rc; if ((rc = SDLNet_ResolveHost (addr, host, port)) < 0) @@ -53,25 +55,24 @@ UDPbase::ResolveHost (IPaddress* addr, const char* host, const Uint16 port) /* * Open *our* UDP IPaddress->port */ -UDPsocket -UDPbase::UDP_Open (void) +UDPsocket UDPbase::UDP_Open (void) { - return UDP_Open (mIPAddress.port); + return UDP_Open (m_address.port); } -UDPsocket -UDPbase::UDP_Open (const Uint16 port) +UDPsocket UDPbase::UDP_Open (const Uint16 port) { - UDPsocket sock = NULL; + UDPsocket + sock = NULL; if (NULL == (sock = SDLNet_UDP_Open (port))) { throw "SDLNet_UDP_Open: " + std::string (SDLNet_GetError ()); } if (NULL != UDP_GetPeerAddress (sock, -1)) { - SDL_Log ("Opened UDP port %d", SDLNet_Read16 (&mIPAddress.port)); - UDPsocks.push_back (sock); + SDL_Log ("Opened UDP port %d", SDLNet_Read16 (&m_address.port)); + m_socks.push_back (sock); } return sock; } @@ -81,6 +82,11 @@ void UDPbase::UDP_Close (UDPsocket sock) { SDL_assert (NULL != sock); + const auto& ref = std::find (m_socks.begin (), m_socks.end (), sock); + if (ref != m_socks.end ()) + { + m_socks.erase (ref); + } SDLNet_UDP_Close (sock); sock = NULL; } @@ -91,7 +97,7 @@ UDPbase::UDP_Bind (UDPsocket sock, const int channel) { int binding; SDL_assert (NULL != sock); - if (-1 == (binding = SDLNet_UDP_Bind (sock, channel, &mIPAddress))) + if (-1 == (binding = SDLNet_UDP_Bind (sock, channel, &m_address))) { throw "SDLNet_UDP_Bind: " + std::string (SDLNet_GetError ()); } @@ -123,17 +129,17 @@ void UDPbase::CloseAllSockets (void) { size_t i, n; - - n = UDPsocks.size (); + n = m_socks.size (); for (i = 0; i < n; i++) - UDP_Close (UDPsocks.at (i)); - - UDPsocks.clear (); + { + UDP_Close (m_socks.at (i)); + } + m_socks.clear (); } std::string UDPbase::toString (void) { - return std::string (SDLNet_ResolveIP (&mIPAddress)) + ":" + - std::to_string (SDLNet_Read16 (&mIPAddress.port)); + return std::string (SDLNet_ResolveIP (&m_address)) + ":" + + std::to_string (SDLNet_Read16 (&m_address.port)); } diff --git a/src/UDPpacketVBuffer.cpp b/src/UDPpacketVBuffer.cpp deleted file mode 100644 index e9546b2..0000000 --- a/src/UDPpacketVBuffer.cpp +++ /dev/null @@ -1,87 +0,0 @@ -#include "UDPpacketVBuffer.h" - -#include - -UDPpacketVBuffer::UDPpacketVBuffer (UDPpacket* src, const void* buf, const size_t size) -{ - int i, len, mtu, nsent; - const uint8_t* bufPtr; - const uint8_t* bufPtrEnd; - - SDL_assert (NULL != buf); - SDL_assert (NULL != src); - - mtu = src->len; - SDL_assert (mtu > 0); - m_howmany = (int) ceil ((double) size / (double) mtu); - // SDL_Log ("%d", m_howmany); - - if (NULL == (m_packetV = SDLNet_AllocPacketV (m_howmany, mtu))) - { - throw "SDLNet_AllocPacketV: " + std::string (SDLNet_GetError ()); - } - - i = 0; - nsent = size; - bufPtr = (const uint8_t*) buf; - bufPtrEnd = bufPtr + size; - while ((i < m_howmany) && (bufPtr < bufPtrEnd) && (nsent > 0)) - { - len = nsent; - 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; - - /* SDLNet_UDP_Send sets the channel in the argument, - * this one takes it implicitly from the source UDPpacket. - * Set it for all packets in the vector. */ - m_packetV[i]->channel = src->channel; - /* Always zero out the entire data */ - SDL_memset (m_packetV[i]->data, 0, mtu); - /* But only memcpy in the relevant bits */ - SDL_memcpy (m_packetV[i]->data, bufPtr, len); - m_packetV[i]->len = len; - /* probably don't need maxlen or status set after this */ - m_packetV[i]->maxlen = size; - m_packetV[i]->status = 0; - /* But we will want the destination address of this packet */ - m_packetV[i]->address = src->address; - /* Advance */ - bufPtr += len; - nsent -= len; - i++; - } -} - - -std::string -UDPpacketVBuffer::Reassemble (void) -{ - int i, maxlen, mtu, numrecv; - char* bufPtr; - char* bufPtrEnd; - std::string buf; - - SDL_assert (NULL != m_packetV); - SDL_assert (NULL != m_packetV[0]); - - maxlen = m_packetV[0]->maxlen; - SDL_assert (maxlen > 0); - buf.reserve (maxlen); - bufPtr = &buf[0]; - bufPtrEnd = bufPtr + maxlen; - - for (numrecv = 0; (numrecv < m_howmany) && m_packetV[numrecv] && (bufPtr < bufPtrEnd); numrecv++) - { - mtu = m_packetV[numrecv]->len; - SDL_assert (mtu > 0); - memcpy (bufPtr, m_packetV[numrecv]->data, mtu); - bufPtr += mtu; - } - - SDL_assert (numrecv == m_howmany); - - return buf; -} diff --git a/src/client_main.cpp b/src/client_main.cpp index 0ae642e..85e5010 100644 --- a/src/client_main.cpp +++ b/src/client_main.cpp @@ -9,7 +9,7 @@ #include "signal_common.h" #include "unused.h" #include "UDPbase.h" -#include "UDPpacketVBuffer.h" +#include "SafeUDPpacket.h" #include "events_common.h" #include "lib_GetOpt.h" @@ -126,12 +126,12 @@ main (int argc, char** argv) state.channel = state.init_opts.channel; state.port = state.init_opts.port; client = std::make_unique (state.host, state.port, state.channel); - if (NULL == (state.udpsock = client->getSocket (0))) + if (NULL == (state.udpsock = client->get (0))) { SDL_Log ("Unable to open client port"); goto _out; } - if (-1 == (state.channel = client->getChannel ())) + if (-1 == (state.channel = client->channel ())) { SDL_Log ("Unable to bind client port"); goto _out; @@ -442,20 +442,15 @@ my_Input_Key (SDL_Event* event) { DEBUG_LOG ("Physical %s key acting as %s key", SDL_GetScancodeName (event->key.keysym.scancode), SDL_GetKeyName (event->key.keysym.sym)); } - UDPpacket* packet = SDLNet_AllocPacket (sizeof (SDL_Event)); - if (packet) + IPaddress* addr = SDLNet_UDP_GetPeerAddress (state.udpsock, state.channel); + if (addr) { - packet->len = sizeof (SDL_Event); - packet->channel = state.channel; - IPaddress* addr = SDLNet_UDP_GetPeerAddress (state.udpsock, state.channel); - if (addr) - { - // SDL_Log ("%s:%d", SDLNet_ResolveIP (addr), SDLNet_Read16(&addr->port)); - packet->address = *addr; - UDPpacketVBuffer packetV (packet, (const void*)event, sizeof (SDL_Event)); - packetV.Send (state.udpsock); - } - SDLNet_FreePacket (packet); + SafeUDPpacket p ((const void*)event, sizeof (SDL_Event)); + p.len (sizeof (SDL_Event)); + p.maxlen (sizeof (SDL_Event)); + p.channel (state.channel); + p.address (*addr); + p.Send (state.udpsock); } } } diff --git a/src/lib_GetOpt.cpp b/src/lib_GetOpt.cpp index bc474e8..9afe2e8 100644 --- a/src/lib_GetOpt.cpp +++ b/src/lib_GetOpt.cpp @@ -1,4 +1,5 @@ #include "lib_GetOpt.h" +#include "UDPbase.h" #include #include @@ -16,10 +17,12 @@ void common_GetOpt (int argc, char** argv, Init_CommonOpts* state) { int opt; + state->host = "localhost"; state->channel = -1; - state->mtu = 1450; + state->mtu = DEFAULT_MAX_PACKET_SIZE; state->port = 6666; + while ((opt = getopt (argc, argv, "hm:p:s:")) != -1) { switch (opt) @@ -33,8 +36,8 @@ common_GetOpt (int argc, char** argv, Init_CommonOpts* state) case 's': state->host = optarg; break; - case 'h': default: + case 'h': common_Usage (argv[0]); exit (EXIT_FAILURE); break; diff --git a/test/test_SafeUDPpacket.cpp b/test/test_SafeUDPpacket.cpp index 095ca47..0e2b38a 100644 --- a/test/test_SafeUDPpacket.cpp +++ b/test/test_SafeUDPpacket.cpp @@ -1,11 +1,142 @@ +#include "debug.h" +#include "glCheckErrors.h" +#include "lib_GL_common.h" +#include "lib_SDL_common.h" +#include "quad.h" +#include "shader.h" +#include "signal_common.h" +#include "unused.h" +#include "UDPbase.h" +#include "FileIO.h" #include "SafeUDPpacket.h" +#include "lib_GetOpt.h" -int main(void) +#include +#include +#include +#include + +#include /* exit */ +#include /* memset */ +#include /* getopt */ +#include /* getopt */ + +#define MIN_FRAME_MS 7 /* 144 Hz */ + +static const char* host = "localhost"; +static Uint16 port = 6666; +static int channel = -1; + +static std::unique_ptr client; + +struct Test_CommonOpts : public Init_CommonOpts { - int size; - size = 1; - UDPpacket* p_ptr; - SafeUDPpacket p (size); - p_ptr = p.get (); + std::string path; +}; + +static void my_GetOpt (int argc, char** argv, Test_CommonOpts*); +static void my_Usage (const char*); + +int +main (int argc, char **argv) +{ + { + Test_CommonOpts init_opts; + + UDPsocket udpsock; + + common_Signal_Init (); + my_GetOpt (argc, argv, &init_opts); + + common_SDL_Init (); + + client = std::make_unique (host, port); + if (NULL == (udpsock = client->UDP_Open (0))) + { + SDL_Log ("Unable to open client port"); + return -1; + } + if (-1 == (channel = client->UDP_Bind (udpsock, -1))) + { + SDL_Log ("Unable to bind client port"); + return -1; + } + + std::string buf; + if (init_opts.path == "-") + { + try + { + std::string line; + while (getline (std::cin, line)) + { + buf += line + "\n"; + } + } + catch (std::ifstream::failure& e) + { + // std::cout << e << std::endl; + } + } + else + { + FileIO f (init_opts.path, "r"); + buf = f.ReadToString (); + } + + IPaddress* addr = SDLNet_UDP_GetPeerAddress (udpsock, channel); + SafeUDPpacketV pV (&buf[0], buf.length (), DEFAULT_MAX_PACKET_SIZE); + pV.len (DEFAULT_MAX_PACKET_SIZE); + pV.address (*addr); + pV.channel (channel); + pV.Send (udpsock); + } + + common_SDL_Quit (); + return 0; } + + +static void +my_Usage (const char* argv0) +{ + fprintf (stderr, "Usage: %s [-h] [-f [-|path]] [-s server] [-p port]\n", argv0); +} + + +void +my_GetOpt (int argc, char** argv, Test_CommonOpts* state) +{ + int opt; + + state->host = "localhost"; + state->channel = -1; + state->mtu = DEFAULT_MAX_PACKET_SIZE; + state->port = 6666; + state->path = "test/test_SafeUDPpacket.cpp"; + + while ((opt = getopt (argc, argv, "f:hm:p:s:")) != -1) + { + switch (opt) + { + case 'f': + state->path = optarg; + break; + case 'm': + state->mtu = strtol (optarg, NULL, 0); + break; + case 'p': + state->port = strtol (optarg, NULL, 0); + break; + case 's': + state->host = optarg; + break; + default: + case 'h': + my_Usage (argv[0]); + exit (EXIT_FAILURE); + break; + } + } +} diff --git a/test/test_UDPpacketBufferV.cpp b/test/test_UDPpacketBufferV.cpp deleted file mode 100644 index a5d953a..0000000 --- a/test/test_UDPpacketBufferV.cpp +++ /dev/null @@ -1,132 +0,0 @@ -#include "debug.h" -#include "glCheckErrors.h" -#include "lib_GL_common.h" -#include "lib_SDL_common.h" -#include "quad.h" -#include "shader.h" -#include "signal_common.h" -#include "unused.h" -#include "UDPbase.h" -#include "UDPpacketVBuffer.h" -#include "FileIO.h" - -#include -#include -#include -#include - -#include /* getopt */ -#include /* exit */ -#include /* memset */ -#include /* getopt */ - -#include -#include - -#define Perf_Diff(a, b) ((double) 1000 * ((double)(((double) (b) - (double) (a)) / (double) SDL_GetPerformanceFrequency ()))) - -#define MIN_FRAME_MS 7 /* 144 Hz */ - -static void my_GetOpt (int, char **); -static void my_Usage (const char *); - -static const char* host = "localhost"; -static Uint16 port = 6666; -static int channel = -1; -static std::string path = "./test/test_UDPpacketVBuffer.cpp"; - -static std::unique_ptr client; - -int -main (int argc, char **argv) -{ - { - UDPsocket udpsock; - - common_Signal_Init (); - my_GetOpt (argc, argv); - - common_SDL_Init (); - - client = std::make_unique (host, port); - if (NULL == (udpsock = client->UDP_Open (0))) - { - SDL_Log ("Unable to open client port"); - return -1; - } - if (-1 == (channel = client->UDP_Bind (udpsock, -1))) - { - SDL_Log ("Unable to bind client port"); - return -1; - } - - std::string buf; - if (path == "-") - { - try { - std::string line; - while (getline (std::cin, line)) - { - buf += line + "\n"; - } - } - catch (std::ifstream::failure& e) { - // std::cout << e << std::endl; - } - } - else - { - FileIO f (path, "r"); - buf = f.ReadToString (); - } - - UDPpacket* packet = SDLNet_AllocPacket (buf.length ()); - IPaddress* addr = SDLNet_UDP_GetPeerAddress (udpsock, channel); - packet->len = DEFAULT_MAX_PACKET_SIZE; - packet->address = *addr; - packet->channel = channel; - UDPpacketVBuffer packetV (packet, &buf[0], buf.length ()); - packetV.Send (udpsock); - } - - common_SDL_Quit (); - - return 0; -} - - - -static void -my_Usage (const char *argv0) -{ - fprintf (stderr, "Usage: %s [-h] [-s host] [-p port]\n", argv0); -} - - -static void -my_GetOpt (int argc, char **argv) -{ - int opt; - host = "localhost"; - port = 6666; - while ((opt = getopt (argc, argv, "f:hp:s:")) != -1) - { - switch (opt) - { - case 'p': - port = strtol (optarg, NULL, 0); - break; - case 's': - host = optarg; - break; - case 'f': - path = std::string (optarg); - break; - case 'h': - default: - my_Usage (argv[0]); - exit (EXIT_FAILURE); - break; - } - } -}