Investigating strange crash.

I don't think it was actually crashing, the server is programmed to exit
on Return (empty line) at the prompt.
This commit is contained in:
Bubblegumdrop 2022-01-09 09:27:25 -05:00
parent 0cad56c8c6
commit 0fbcdbbabb
10 changed files with 110 additions and 77 deletions

2
.gitignore vendored
View File

@ -1,7 +1,7 @@
.d/ .d/
.o/ .o/
imgui/ imgui/
res/
main_client main_client
main_server main_server
*.png *.png
res/

View File

@ -6,6 +6,7 @@ SRCDIR := src
OBJDIR := .o OBJDIR := .o
DEPDIR := .d DEPDIR := .d
TESTDIR := test TESTDIR := test
TEST_BINDIR := $(TESTDIR)/bin
MKDIR := mkdir -p MKDIR := mkdir -p
# }}} # }}}
@ -21,13 +22,10 @@ IMGUI_SRCS := \
imgui/imgui_tables.cpp \ imgui/imgui_tables.cpp \
imgui/imgui_widgets.cpp imgui/imgui_widgets.cpp
SRCS := $(wildcard $(SRCDIR)/*.cpp) $(IMGUI_SRCS) SRCS := $(wildcard $(SRCDIR)/*.cpp) $(IMGUI_SRCS) $(wildcard $(TESTDIR)/*.cpp)
OBJS := $(patsubst %,$(OBJDIR)/%.o,$(basename $(SRCS))) OBJS := $(patsubst %,$(OBJDIR)/%.o,$(basename $(SRCS)))
DEPS := $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS))) DEPS := $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS)))
$(shell $(MKDIR) $(dir $(OBJS)) >/dev/null)
$(shell $(MKDIR) $(dir $(DEPS)) >/dev/null)
COMMON_OBJS := \ COMMON_OBJS := \
$(OBJDIR)/src/lib_lua_common.o \ $(OBJDIR)/src/lib_lua_common.o \
$(OBJDIR)/src/lib_SDL_common.o \ $(OBJDIR)/src/lib_SDL_common.o \
@ -60,7 +58,11 @@ TEST_OBJ_COMMON := \
TESTS := test_FileIO test_lua test_UDPpacketBufferV TESTS := test_FileIO test_lua test_UDPpacketBufferV
TEST_PROGS := $(addprefix $(TESTDIR)/,$(TESTS)) TEST_PROGS := $(addprefix $(TEST_BINDIR)/,$(TESTS))
$(shell $(MKDIR) $(dir $(OBJS)) >/dev/null)
$(shell $(MKDIR) $(dir $(DEPS)) >/dev/null)
$(shell $(MKDIR) $(dir $(TEST_PROGS)) >/dev/null)
# {{{ Flags # {{{ Flags
CC := gcc CC := gcc
@ -147,12 +149,16 @@ main_server: $(SERVER_OBJS)
$(LINK.o) $^ $(LDLIBS) $(LINK.o) $^ $(LDLIBS)
test: $(TEST_PROGS) test: $(TEST_PROGS)
@echo $(TEST_PROGS)
test/test_FileIO: $(TEST_OBJ_COMMON) $(OBJDIR)/src/util.o $(TEST_BINDIR)/test_FileIO: $(OBJDIR)/test/test_FileIO.o $(TEST_OBJ_COMMON) $(OBJDIR)/src/util.o
$(LINK.o) $^ $(LDLIBS)
test/test_lua: $(TEST_OBJ_COMMON) $(OBJDIR)/src/lib_lua_common.o $(TEST_BINDIR)/test_lua: $(OBJDIR)/test/test_lua.o $(TEST_OBJ_COMMON) $(OBJDIR)/src/lib_lua_common.o
$(LINK.o) $^ $(LDLIBS)
test/test_UDPpacketBufferV: $(TEST_OBJ_COMMON) $(OBJDIR)/src/UDPbase.o $(OBJDIR)/src/lib_SDL_common.o $(TEST_BINDIR)/test_UDPpacketBufferV: $(OBJDIR)/test/test_UDPpacketBufferV.o $(TEST_OBJ_COMMON) $(OBJDIR)/src/UDPbase.o $(OBJDIR)/src/lib_SDL_common.o
$(LINK.o) $^ $(LDLIBS)
$(OBJDIR)/%.o: %.c $(OBJDIR)/%.o: %.c
$(OBJDIR)/%.o: %.c $(DEPDIR)/%.d $(OBJDIR)/%.o: %.c $(DEPDIR)/%.d

View File

@ -6,7 +6,7 @@
#include "lib_lua_common.h" #include "lib_lua_common.h"
#include "UDPbase.h" #include "UDPbase.h"
#define DEFAULT_MAX_SOCKET_TIMEOUT 10 #define DEFAULT_SOCKET_TIMEOUT 10
class Server : public UDPbase class Server : public UDPbase
{ {
@ -15,18 +15,24 @@ class Server : public UDPbase
std::vector<TCPsocket> TCPsocks; std::vector<TCPsocket> TCPsocks;
std::vector<UDPsocket> UDPsocks; std::vector<UDPsocket> UDPsocks;
UDPpacket** UDPPacketV;
SDLNet_SocketSet SocketSet; SDLNet_SocketSet SocketSet;
UDPpacket** packetV;
lua_State *mLuaState; lua_State *mLuaState;
public: public:
Server (const char* host, const Uint16 port); Server (const char* host, const Uint16 port);
~Server (void); ~Server (void);
/*
* error: invalid use of non-static member function Uint32 Server::FetchPacketsCallback(Uint32, void*)
* Don't understand this.
* static keyword fixes it.
*/
static Uint32 FetchPacketsCallback (Uint32 interval, void* param); static Uint32 FetchPacketsCallback (Uint32 interval, void* param);
void ProcessPacketHelper (UDPpacket* packet); void ProcessPacketHelper (UDPpacket* packet);
void RunLua (const std::string& buf); void RunLua (const std::string& buf);
int AllocateApproximateBufferSpace (const int nclients); int AllocateApproximateResources (const int nclients);
int Start (void); int Start (void);
int Stop (void); int Stop (void);
void CloseAllSockets (void); void CloseAllSockets (void);
@ -38,9 +44,9 @@ class Server : public UDPbase
{ {
return SDL_UnlockMutex (mProcessPacketMutex); return SDL_UnlockMutex (mProcessPacketMutex);
} }
lua_State* getLuaState(void) const { return mLuaState; } lua_State* getLuaState(void) { return mLuaState; }
const TCPsocket* getTCPsock (size_t idx) const { return &TCPsocks.at (idx); } TCPsocket getTCPsock (size_t idx) { return TCPsocks.at (idx); }
const UDPsocket* getUDPsock (size_t idx) const { return &UDPsocks.at (idx); } UDPsocket getUDPsock (size_t idx) { return UDPsocks.at (idx); }
UDPpacket* getUDPPacketV (size_t idx) const { return UDPPacketV[idx]; } UDPpacket** getPacketV (void) { return packetV; }
SDLNet_SocketSet getSocketSet (void) const { return SocketSet; } SDLNet_SocketSet getSocketSet (void) { return SocketSet; }
}; };

View File

@ -3,6 +3,8 @@
#include "util.h" #include "util.h"
#include "Server.h" #include "Server.h"
#define DEFAULT_MAX_PACKET_RECV_SIZE 1024
Server::Server (const char *host, const Uint16 port) Server::Server (const char *host, const Uint16 port)
: UDPbase (host, port) : UDPbase (host, port)
{ {
@ -13,8 +15,8 @@ Server::Server (const char *host, const Uint16 port)
UDPsocks.push_back (sock); UDPsocks.push_back (sock);
} }
SocketSet = NULL; SocketSet = NULL;
UDPPacketV = NULL;
mLuaState = NULL; mLuaState = NULL;
packetV = NULL;
mProcessPacketMutex = SDL_CreateMutex (); mProcessPacketMutex = SDL_CreateMutex ();
} }
@ -22,6 +24,13 @@ Server::Server (const char *host, const Uint16 port)
Server::~Server (void) Server::~Server (void)
{ {
CloseAllSockets (); CloseAllSockets ();
if (packetV)
{
SDLNet_FreePacketV (packetV);
packetV = NULL;
}
if (mProcessPacketMutex) if (mProcessPacketMutex)
{ {
SDL_DestroyMutex (mProcessPacketMutex); SDL_DestroyMutex (mProcessPacketMutex);
@ -45,32 +54,8 @@ Server::CloseAllSockets (void)
} }
void
Server::ProcessPacketHelper (UDPpacket* packet)
{
int len;
SDL_Event event;
SDL_UserEvent userevent;
UDPpacket *temp;
len = packet->len;
temp = SDLNet_AllocPacket (len);
temp->address = packet->address;
SDL_memcpy (temp->data, packet->data, len);
temp->len = len;
SDL_zero (event);
SDL_zero (userevent);
userevent.type = PACKET_SDL_EVENT;
userevent.code = 0;
userevent.data1 = (void *) temp;
userevent.data2 = (void *) NULL;
event.type = SDL_USEREVENT;
event.user = userevent;
SDL_PushEvent (&event);
}
int int
Server::AllocateApproximateBufferSpace (const int nclients) Server::AllocateApproximateResources (const int nclients)
{ {
int i, l, numused, total; int i, l, numused, total;
@ -82,11 +67,20 @@ Server::AllocateApproximateBufferSpace (const int nclients)
SDL_Log ("Preparing to serve %d clients", nclients); SDL_Log ("Preparing to serve %d clients", nclients);
if (!(UDPPacketV = SDLNet_AllocPacketV (nclients, DEFAULT_MAX_PACKET_SIZE))) /*
* If each client sends 1024 250 byte packets, that's 1/4th of a MB or 256000 bytes ...
* seems fine for now as an upper limit.
*/
if (packetV)
{
SDLNet_FreePacketV (packetV);
}
if (!(packetV = SDLNet_AllocPacketV (1024 * nclients, DEFAULT_MAX_PACKET_RECV_SIZE)))
{ {
SDL_Log ("SDLNet_AllocPacketV: %s", SDLNet_GetError ()); SDL_Log ("SDLNet_AllocPacketV: %s", SDLNet_GetError ());
return -1; return -1;
} }
if (!(SocketSet = SDLNet_AllocSocketSet (nclients))) if (!(SocketSet = SDLNet_AllocSocketSet (nclients)))
{ {
SDL_Log ("SDLNet_AllocSocketSet: %s", SDLNet_GetError ()); SDL_Log ("SDLNet_AllocSocketSet: %s", SDLNet_GetError ());
@ -121,7 +115,7 @@ Server::Start (void)
int l; int l;
const int nclients = UDPsocks.size (); const int nclients = UDPsocks.size ();
if ((l = AllocateApproximateBufferSpace (nclients) < 0)) if ((l = AllocateApproximateResources (nclients) < 0))
{ {
DEBUG_LOG ("Unable to allocate approximate buffer space"); DEBUG_LOG ("Unable to allocate approximate buffer space");
return l; return l;
@ -136,7 +130,7 @@ Server::Start (void)
mLuaState = luaL_newstate (); mLuaState = luaL_newstate ();
luaL_openlibs (mLuaState); luaL_openlibs (mLuaState);
mFetchPacketsCallbackTimerID = SDL_AddTimer (10, Server::FetchPacketsCallback, (void *) this); mFetchPacketsCallbackTimerID = SDL_AddTimer (DEFAULT_SOCKET_TIMEOUT, Server::FetchPacketsCallback, (void *) this);
return Unlock (); return Unlock ();
} }
@ -178,12 +172,6 @@ Server::Stop (void)
SocketSet = NULL; SocketSet = NULL;
} }
if (UDPPacketV)
{
SDLNet_FreePacketV (UDPPacketV);
UDPPacketV = NULL;
}
return 0; return 0;
} }
@ -195,10 +183,39 @@ Server::RunLua (const std::string& buf)
} }
void
Server::ProcessPacketHelper (UDPpacket* src)
{
SDL_Event event;
SDL_UserEvent userevent;
UDPpacket *dst;
dst = SDLNet_AllocPacket (src->len);
dst->address = src->address;
dst->channel = src->channel;
dst->status = src->status;
dst->len = src->len;
dst->maxlen = src->maxlen;
SDL_memset (dst->data, 0, src->len);
SDL_memcpy (dst->data, src->data, src->len);
SDL_zero (event);
SDL_zero (userevent);
userevent.type = PACKET_SDL_EVENT;
userevent.code = 0;
userevent.data1 = (void *) dst;
userevent.data2 = (void *) NULL;
event.type = SDL_USEREVENT;
event.user = userevent;
SDL_PushEvent (&event);
}
Uint32 Server::FetchPacketsCallback (Uint32 interval, void *param) Uint32 Server::FetchPacketsCallback (Uint32 interval, void *param)
{ {
int numrecv, numready;
int i, j; int i, j;
int numrecv, numready;
UDPpacket** packetV;
SDL_assert (NULL != param); SDL_assert (NULL != param);
Server* server = (Server *) param; Server* server = (Server *) param;
@ -209,7 +226,7 @@ Uint32 Server::FetchPacketsCallback (Uint32 interval, void *param)
return 0; return 0;
} }
numready = SDLNet_CheckSockets (server->getSocketSet (), DEFAULT_MAX_SOCKET_TIMEOUT); numready = SDLNet_CheckSockets (server->getSocketSet (), 1);
if (numready < 0) if (numready < 0)
{ {
DEBUG_LOG ("SDLNet_CheckSockets: %s", SDLNet_GetError ()); DEBUG_LOG ("SDLNet_CheckSockets: %s", SDLNet_GetError ());
@ -221,20 +238,24 @@ Uint32 Server::FetchPacketsCallback (Uint32 interval, void *param)
} }
/* check all sockets with SDLNet_SocketReady and handle the active ones. */ /* check all sockets with SDLNet_SocketReady and handle the active ones. */
UDPsocket udpsocket;
packetV = server->getPacketV ();
for (i = 0; i < numready; i++) for (i = 0; i < numready; i++)
{ {
if (SDLNet_SocketReady (*server->getUDPsock (i))) udpsocket = server->getUDPsock (i);
if (SDLNet_SocketReady (udpsocket))
{ {
UDPpacket* packet = server->getUDPPacketV (i); numrecv = SDLNet_UDP_RecvV (udpsocket, packetV);
numrecv = SDLNet_UDP_RecvV (*server->getUDPsock (i), &packet);
if (numrecv < 0) if (numrecv < 0)
{ {
DEBUG_LOG ("SDLNet_UDP_RecvV: %s", SDLNet_GetError ()); DEBUG_LOG ("SDLNet_UDP_RecvV: %s", SDLNet_GetError ());
break; break;
} }
/* j!!!! NOT i!!!!! */
for (j = 0; j < numrecv; j++) for (j = 0; j < numrecv; j++)
{ {
server->ProcessPacketHelper (packet); server->ProcessPacketHelper (packetV[j]);
} }
} }
} }

View File

@ -15,6 +15,7 @@ UDPpacketVBuffer::UDPpacketVBuffer (UDPpacket* src, const void* buf, const size_
SDL_assert (mtu > 0); SDL_assert (mtu > 0);
npackets = (int)ceil ((double) size / (double) mtu); npackets = (int)ceil ((double) size / (double) mtu);
SDL_Log ("%d", npackets);
if (NULL == (packetV = SDLNet_AllocPacketV (npackets, mtu))) if (NULL == (packetV = SDLNet_AllocPacketV (npackets, mtu)))
{ {
throw "SDLNet_AllocPacketV: " + std::string (SDLNet_GetError ()); throw "SDLNet_AllocPacketV: " + std::string (SDLNet_GetError ());

View File

@ -112,7 +112,7 @@ common_SDL_Init (void)
{ {
Uint32 Flags; Uint32 Flags;
common_Signal_Init (); // common_Signal_Init ();
Flags = SDL_INIT_EVERYTHING; Flags = SDL_INIT_EVERYTHING;
if (SDL_Init (Flags) < 0) if (SDL_Init (Flags) < 0)

View File

@ -96,9 +96,13 @@ my_SDL_UserEvent (SDL_Event * ev)
{ {
case PACKET_SDL_EVENT: case PACKET_SDL_EVENT:
{ {
UDPpacket *packet = (UDPpacket *) ev->user.data1; UDPpacket* packet = (UDPpacket *) ev->user.data1;
my_ProcessPacket (packet); if (packet)
SDLNet_FreePacket (packet); {
SDL_Log ("PACKET_SDL_EVENT");
my_ProcessPacket (packet);
SDLNet_FreePacket (packet);
}
} }
break; break;
default: default:
@ -128,6 +132,8 @@ my_ProcessPacket (UDPpacket* packet)
SDL_assert (packet->len > 0); SDL_assert (packet->len > 0);
len = packet->len; len = packet->len;
if (len > sizeof (SDL_Event))
len = sizeof (SDL_Event);
SDL_memcpy (&event, packet->data, len); SDL_memcpy (&event, packet->data, len);
// SDL_Log ("%d, %x", packet->len, event.type); // SDL_Log ("%d, %x", packet->len, event.type);

View File

@ -65,24 +65,23 @@ util_HexDump (const void* buf, const size_t len)
void void
util_UDP_DumpPacket (UDPpacket* packet) util_UDP_DumpPacket (UDPpacket* packet)
{ {
const char* buf; const uint8_t* buf;
SDL_assert (packet != NULL); SDL_assert (packet != NULL);
// SDL_assert (packet->len > 0); // SDL_assert (packet->len > 0);
buf = (const char*)packet->data; SDL_Log ("address: %s:%d", SDLNet_ResolveIP (&packet->address), SDLNet_Read16 (&packet->address.port));
SDL_Log ("address: %s", SDLNet_ResolveIP (&packet->address));
SDL_Log ("channel: %d", packet->channel); SDL_Log ("channel: %d", packet->channel);
SDL_Log ("len : %d", packet->len); SDL_Log ("len : %d", packet->len);
SDL_Log ("maxlen : %d", packet->maxlen); SDL_Log ("maxlen : %d", packet->maxlen);
SDL_Log ("status : %d", packet->status); SDL_Log ("status : %d", packet->status);
if (packet->len) if (packet->len > 0)
{ {
if (util_IsProbablyAscii (buf, packet->len)) buf = (const uint8_t*)packet->data;
SDL_Log ("data:\n%s", buf); // if (util_IsProbablyAscii (buf, packet->len))
else // SDL_Log ("data:\n%s\n", buf);
// else
util_HexDump (buf, packet->len); util_HexDump (buf, packet->len);
} }
else else

6
test/.gitignore vendored
View File

@ -1,5 +1 @@
test_FileIO bin/
test_File_Write_UDP
test_lua
test_Object
test/test_UDP_DumpPacket

View File

@ -1,5 +1,3 @@
#include "camera.h"
#include "cube.h"
#include "debug.h" #include "debug.h"
#include "glCheckErrors.h" #include "glCheckErrors.h"
#include "lib_GL_common.h" #include "lib_GL_common.h"