Another Buffer_To_UDPPacketV. Yet to test.
Split UDPbase out. Change ShaderProgram to std::unique_ptr<ShaderProgram>. I think I may change Buffer_To_UDPPacketV to some kind of RAII style code.
This commit is contained in:
parent
9e957d466f
commit
764ebe4ce2
1
Makefile
1
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 := \
|
||||
|
@ -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&);
|
||||
|
@ -3,59 +3,18 @@
|
||||
#include <string>
|
||||
#include <SDL_net.h>
|
||||
|
||||
#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);
|
||||
};
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
@ -9,12 +9,6 @@
|
||||
#include <sys/types.h> /* stat */
|
||||
#include <unistd.h> /* 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;
|
||||
}
|
||||
|
@ -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 ());
|
||||
}
|
||||
|
69
src/UDPbase.cpp
Normal file
69
src/UDPbase.cpp
Normal file
@ -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);
|
||||
}
|
@ -83,9 +83,9 @@ struct Client_State
|
||||
static std::unique_ptr<Cube> cube;
|
||||
static std::unique_ptr<Quad> quad;
|
||||
static std::unique_ptr<UDPbase> client;
|
||||
static std::unique_ptr<ShaderProgram> 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<ShaderProgram> ("./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);
|
||||
}
|
||||
|
||||
|
||||
|
@ -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);
|
||||
|
66
src/util.cpp
66
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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user