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:
parent
0cad56c8c6
commit
0fbcdbbabb
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,7 +1,7 @@
|
||||
.d/
|
||||
.o/
|
||||
imgui/
|
||||
res/
|
||||
main_client
|
||||
main_server
|
||||
*.png
|
||||
res/
|
||||
|
22
Makefile
22
Makefile
@ -6,6 +6,7 @@ SRCDIR := src
|
||||
OBJDIR := .o
|
||||
DEPDIR := .d
|
||||
TESTDIR := test
|
||||
TEST_BINDIR := $(TESTDIR)/bin
|
||||
MKDIR := mkdir -p
|
||||
# }}}
|
||||
|
||||
@ -21,13 +22,10 @@ IMGUI_SRCS := \
|
||||
imgui/imgui_tables.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)))
|
||||
DEPS := $(patsubst %,$(DEPDIR)/%.d,$(basename $(SRCS)))
|
||||
|
||||
$(shell $(MKDIR) $(dir $(OBJS)) >/dev/null)
|
||||
$(shell $(MKDIR) $(dir $(DEPS)) >/dev/null)
|
||||
|
||||
COMMON_OBJS := \
|
||||
$(OBJDIR)/src/lib_lua_common.o \
|
||||
$(OBJDIR)/src/lib_SDL_common.o \
|
||||
@ -60,7 +58,11 @@ TEST_OBJ_COMMON := \
|
||||
|
||||
|
||||
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
|
||||
CC := gcc
|
||||
@ -147,12 +149,16 @@ main_server: $(SERVER_OBJS)
|
||||
$(LINK.o) $^ $(LDLIBS)
|
||||
|
||||
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 $(DEPDIR)/%.d
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include "lib_lua_common.h"
|
||||
#include "UDPbase.h"
|
||||
|
||||
#define DEFAULT_MAX_SOCKET_TIMEOUT 10
|
||||
#define DEFAULT_SOCKET_TIMEOUT 10
|
||||
|
||||
class Server : public UDPbase
|
||||
{
|
||||
@ -15,18 +15,24 @@ class Server : public UDPbase
|
||||
|
||||
std::vector<TCPsocket> TCPsocks;
|
||||
std::vector<UDPsocket> UDPsocks;
|
||||
UDPpacket** UDPPacketV;
|
||||
SDLNet_SocketSet SocketSet;
|
||||
UDPpacket** packetV;
|
||||
lua_State *mLuaState;
|
||||
|
||||
public:
|
||||
Server (const char* host, const Uint16 port);
|
||||
~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);
|
||||
|
||||
void ProcessPacketHelper (UDPpacket* packet);
|
||||
void RunLua (const std::string& buf);
|
||||
int AllocateApproximateBufferSpace (const int nclients);
|
||||
int AllocateApproximateResources (const int nclients);
|
||||
int Start (void);
|
||||
int Stop (void);
|
||||
void CloseAllSockets (void);
|
||||
@ -38,9 +44,9 @@ class Server : public UDPbase
|
||||
{
|
||||
return SDL_UnlockMutex (mProcessPacketMutex);
|
||||
}
|
||||
lua_State* getLuaState(void) const { return mLuaState; }
|
||||
const TCPsocket* getTCPsock (size_t idx) const { return &TCPsocks.at (idx); }
|
||||
const UDPsocket* getUDPsock (size_t idx) const { return &UDPsocks.at (idx); }
|
||||
UDPpacket* getUDPPacketV (size_t idx) const { return UDPPacketV[idx]; }
|
||||
SDLNet_SocketSet getSocketSet (void) const { return SocketSet; }
|
||||
lua_State* getLuaState(void) { return mLuaState; }
|
||||
TCPsocket getTCPsock (size_t idx) { return TCPsocks.at (idx); }
|
||||
UDPsocket getUDPsock (size_t idx) { return UDPsocks.at (idx); }
|
||||
UDPpacket** getPacketV (void) { return packetV; }
|
||||
SDLNet_SocketSet getSocketSet (void) { return SocketSet; }
|
||||
};
|
||||
|
103
src/Server.cpp
103
src/Server.cpp
@ -3,6 +3,8 @@
|
||||
#include "util.h"
|
||||
#include "Server.h"
|
||||
|
||||
#define DEFAULT_MAX_PACKET_RECV_SIZE 1024
|
||||
|
||||
Server::Server (const char *host, const Uint16 port)
|
||||
: UDPbase (host, port)
|
||||
{
|
||||
@ -13,8 +15,8 @@ Server::Server (const char *host, const Uint16 port)
|
||||
UDPsocks.push_back (sock);
|
||||
}
|
||||
SocketSet = NULL;
|
||||
UDPPacketV = NULL;
|
||||
mLuaState = NULL;
|
||||
packetV = NULL;
|
||||
mProcessPacketMutex = SDL_CreateMutex ();
|
||||
}
|
||||
|
||||
@ -22,6 +24,13 @@ Server::Server (const char *host, const Uint16 port)
|
||||
Server::~Server (void)
|
||||
{
|
||||
CloseAllSockets ();
|
||||
|
||||
if (packetV)
|
||||
{
|
||||
SDLNet_FreePacketV (packetV);
|
||||
packetV = NULL;
|
||||
}
|
||||
|
||||
if (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
|
||||
Server::AllocateApproximateBufferSpace (const int nclients)
|
||||
Server::AllocateApproximateResources (const int nclients)
|
||||
{
|
||||
int i, l, numused, total;
|
||||
|
||||
@ -82,11 +67,20 @@ Server::AllocateApproximateBufferSpace (const int 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 ());
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!(SocketSet = SDLNet_AllocSocketSet (nclients)))
|
||||
{
|
||||
SDL_Log ("SDLNet_AllocSocketSet: %s", SDLNet_GetError ());
|
||||
@ -121,7 +115,7 @@ Server::Start (void)
|
||||
int l;
|
||||
|
||||
const int nclients = UDPsocks.size ();
|
||||
if ((l = AllocateApproximateBufferSpace (nclients) < 0))
|
||||
if ((l = AllocateApproximateResources (nclients) < 0))
|
||||
{
|
||||
DEBUG_LOG ("Unable to allocate approximate buffer space");
|
||||
return l;
|
||||
@ -136,7 +130,7 @@ Server::Start (void)
|
||||
mLuaState = luaL_newstate ();
|
||||
luaL_openlibs (mLuaState);
|
||||
|
||||
mFetchPacketsCallbackTimerID = SDL_AddTimer (10, Server::FetchPacketsCallback, (void *) this);
|
||||
mFetchPacketsCallbackTimerID = SDL_AddTimer (DEFAULT_SOCKET_TIMEOUT, Server::FetchPacketsCallback, (void *) this);
|
||||
|
||||
return Unlock ();
|
||||
}
|
||||
@ -178,12 +172,6 @@ Server::Stop (void)
|
||||
SocketSet = NULL;
|
||||
}
|
||||
|
||||
if (UDPPacketV)
|
||||
{
|
||||
SDLNet_FreePacketV (UDPPacketV);
|
||||
UDPPacketV = NULL;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
int numrecv, numready;
|
||||
int i, j;
|
||||
int numrecv, numready;
|
||||
UDPpacket** packetV;
|
||||
|
||||
SDL_assert (NULL != param);
|
||||
Server* server = (Server *) param;
|
||||
@ -209,7 +226,7 @@ Uint32 Server::FetchPacketsCallback (Uint32 interval, void *param)
|
||||
return 0;
|
||||
}
|
||||
|
||||
numready = SDLNet_CheckSockets (server->getSocketSet (), DEFAULT_MAX_SOCKET_TIMEOUT);
|
||||
numready = SDLNet_CheckSockets (server->getSocketSet (), 1);
|
||||
if (numready < 0)
|
||||
{
|
||||
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. */
|
||||
UDPsocket udpsocket;
|
||||
packetV = server->getPacketV ();
|
||||
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 (*server->getUDPsock (i), &packet);
|
||||
numrecv = SDLNet_UDP_RecvV (udpsocket, packetV);
|
||||
if (numrecv < 0)
|
||||
{
|
||||
DEBUG_LOG ("SDLNet_UDP_RecvV: %s", SDLNet_GetError ());
|
||||
break;
|
||||
}
|
||||
|
||||
/* j!!!! NOT i!!!!! */
|
||||
for (j = 0; j < numrecv; j++)
|
||||
{
|
||||
server->ProcessPacketHelper (packet);
|
||||
server->ProcessPacketHelper (packetV[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ UDPpacketVBuffer::UDPpacketVBuffer (UDPpacket* src, const void* buf, const size_
|
||||
SDL_assert (mtu > 0);
|
||||
|
||||
npackets = (int)ceil ((double) size / (double) mtu);
|
||||
SDL_Log ("%d", npackets);
|
||||
if (NULL == (packetV = SDLNet_AllocPacketV (npackets, mtu)))
|
||||
{
|
||||
throw "SDLNet_AllocPacketV: " + std::string (SDLNet_GetError ());
|
||||
|
@ -112,7 +112,7 @@ common_SDL_Init (void)
|
||||
{
|
||||
Uint32 Flags;
|
||||
|
||||
common_Signal_Init ();
|
||||
// common_Signal_Init ();
|
||||
|
||||
Flags = SDL_INIT_EVERYTHING;
|
||||
if (SDL_Init (Flags) < 0)
|
||||
|
@ -97,9 +97,13 @@ my_SDL_UserEvent (SDL_Event * ev)
|
||||
case PACKET_SDL_EVENT:
|
||||
{
|
||||
UDPpacket* packet = (UDPpacket *) ev->user.data1;
|
||||
if (packet)
|
||||
{
|
||||
SDL_Log ("PACKET_SDL_EVENT");
|
||||
my_ProcessPacket (packet);
|
||||
SDLNet_FreePacket (packet);
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
@ -128,6 +132,8 @@ my_ProcessPacket (UDPpacket* packet)
|
||||
SDL_assert (packet->len > 0);
|
||||
|
||||
len = packet->len;
|
||||
if (len > sizeof (SDL_Event))
|
||||
len = sizeof (SDL_Event);
|
||||
SDL_memcpy (&event, packet->data, len);
|
||||
|
||||
// SDL_Log ("%d, %x", packet->len, event.type);
|
||||
|
15
src/util.cpp
15
src/util.cpp
@ -65,24 +65,23 @@ util_HexDump (const void* buf, const size_t len)
|
||||
void
|
||||
util_UDP_DumpPacket (UDPpacket* packet)
|
||||
{
|
||||
const char* buf;
|
||||
const uint8_t* buf;
|
||||
|
||||
SDL_assert (packet != NULL);
|
||||
// SDL_assert (packet->len > 0);
|
||||
|
||||
buf = (const char*)packet->data;
|
||||
|
||||
SDL_Log ("address: %s", SDLNet_ResolveIP (&packet->address));
|
||||
SDL_Log ("address: %s:%d", SDLNet_ResolveIP (&packet->address), SDLNet_Read16 (&packet->address.port));
|
||||
SDL_Log ("channel: %d", packet->channel);
|
||||
SDL_Log ("len : %d", packet->len);
|
||||
SDL_Log ("maxlen : %d", packet->maxlen);
|
||||
SDL_Log ("status : %d", packet->status);
|
||||
|
||||
if (packet->len)
|
||||
if (packet->len > 0)
|
||||
{
|
||||
if (util_IsProbablyAscii (buf, packet->len))
|
||||
SDL_Log ("data:\n%s", buf);
|
||||
else
|
||||
buf = (const uint8_t*)packet->data;
|
||||
// if (util_IsProbablyAscii (buf, packet->len))
|
||||
// SDL_Log ("data:\n%s\n", buf);
|
||||
// else
|
||||
util_HexDump (buf, packet->len);
|
||||
}
|
||||
else
|
||||
|
6
test/.gitignore
vendored
6
test/.gitignore
vendored
@ -1,5 +1 @@
|
||||
test_FileIO
|
||||
test_File_Write_UDP
|
||||
test_lua
|
||||
test_Object
|
||||
test/test_UDP_DumpPacket
|
||||
bin/
|
||||
|
@ -1,5 +1,3 @@
|
||||
#include "camera.h"
|
||||
#include "cube.h"
|
||||
#include "debug.h"
|
||||
#include "glCheckErrors.h"
|
||||
#include "lib_GL_common.h"
|
||||
|
Loading…
Reference in New Issue
Block a user