Fixed SDL_SwapBE16 issue on client side.
< Bubblegumdrop> SDLNet_UDP_Open seems to call SDLNet_Read16 (which is just SDL_SwapBE16) < Bubblegumdrop> But on the server side I don't use SDLNet_UDP_Open to host the port < whitt> probably the sender was flipping it but the receiver wasn't and so they were incompatible < Bubblegumdrop> removing the SDL_SwapBE16 on the client side fixed it
This commit is contained in:
parent
46b2ed80ae
commit
6a8f90500f
10
Makefile
10
Makefile
@ -31,7 +31,8 @@ $(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 \
|
||||||
$(OBJDIR)/src/signal_common.o
|
$(OBJDIR)/src/signal_common.o \
|
||||||
|
$(OBJDIR)/src/util.o
|
||||||
|
|
||||||
SERVER_OBJS := \
|
SERVER_OBJS := \
|
||||||
$(OBJDIR)/src/server_main.o \
|
$(OBJDIR)/src/server_main.o \
|
||||||
@ -40,7 +41,6 @@ SERVER_OBJS := \
|
|||||||
|
|
||||||
CLIENT_OBJS := \
|
CLIENT_OBJS := \
|
||||||
$(OBJDIR)/src/camera.o \
|
$(OBJDIR)/src/camera.o \
|
||||||
$(OBJDIR)/src/client_common.o \
|
|
||||||
$(OBJDIR)/src/client_main.o \
|
$(OBJDIR)/src/client_main.o \
|
||||||
$(OBJDIR)/src/Client.o \
|
$(OBJDIR)/src/Client.o \
|
||||||
$(OBJDIR)/src/cube.o \
|
$(OBJDIR)/src/cube.o \
|
||||||
@ -148,14 +148,16 @@ main_server: $(SERVER_OBJS)
|
|||||||
|
|
||||||
test: $(TEST_PROGS)
|
test: $(TEST_PROGS)
|
||||||
|
|
||||||
test/test_FileIO: $(TEST_OBJ_COMMON)
|
test/test_FileIO: $(TEST_OBJ_COMMON) $(OBJDIR)/src/util.o
|
||||||
|
|
||||||
test/test_File_Write_UDP: $(TEST_OBJ_COMMON) $(OBJDIR)/src/client_common.o $(OBJDIR)/src/UDP_Write.o $(OBJDIR)/src/Client.o
|
test/test_File_Write_UDP: $(TEST_OBJ_COMMON) $(OBJDIR)/src/UDP_Write.o $(OBJDIR)/src/Client.o
|
||||||
|
|
||||||
test/test_Object: $(TEST_OBJ_COMMON) $(OBJDIR)/src/object.o $(OBJDIR)/src/quad.o $(OBJDIR)/src/cube.o $(OBJDIR)/src/lib_GL_common.o
|
test/test_Object: $(TEST_OBJ_COMMON) $(OBJDIR)/src/object.o $(OBJDIR)/src/quad.o $(OBJDIR)/src/cube.o $(OBJDIR)/src/lib_GL_common.o
|
||||||
|
|
||||||
test/test_lua: $(TEST_OBJ_COMMON) $(OBJDIR)/src/lib_lua_common.o
|
test/test_lua: $(TEST_OBJ_COMMON) $(OBJDIR)/src/lib_lua_common.o
|
||||||
|
|
||||||
|
test/test_Client: $(TEST_OBJ_COMMON) $(OBJDIR)/src/UDP_Write.o $(OBJDIR)/src/Client.o
|
||||||
|
|
||||||
$(OBJDIR)/%.o: %.c
|
$(OBJDIR)/%.o: %.c
|
||||||
$(OBJDIR)/%.o: %.c $(DEPDIR)/%.d
|
$(OBJDIR)/%.o: %.c $(DEPDIR)/%.d
|
||||||
$(PRECOMPILE)
|
$(PRECOMPILE)
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "debug.h"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -14,8 +16,17 @@ class Client
|
|||||||
public:
|
public:
|
||||||
Client (void)
|
Client (void)
|
||||||
{
|
{
|
||||||
|
DEBUG_LOG ("Client::Client ()");
|
||||||
|
}
|
||||||
|
~Client (void)
|
||||||
|
{
|
||||||
|
DEBUG_LOG ("Client::~Client ()");
|
||||||
}
|
}
|
||||||
Client (const char* host, const Uint16 port, const int channel);
|
Client (const char* host, const Uint16 port, const int channel);
|
||||||
UDPsocket UDP_Open (const char* host, const Uint16 port, const int channel);
|
UDPsocket UDP_Open (const char* host, const Uint16 port, const int channel);
|
||||||
void UDP_CloseAll (void);
|
void UDP_CloseAll (void);
|
||||||
|
UDPsocket First (void)
|
||||||
|
{
|
||||||
|
return UDPsocks.at (0);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
class Server
|
class Server
|
||||||
{
|
{
|
||||||
bool mProcessPacketCallbackRunning;
|
SDL_mutex* mProcessPacketMutex;
|
||||||
SDL_TimerID mProcessPacketCallbackTimerID;
|
SDL_TimerID mProcessPacketCallbackTimerID;
|
||||||
static Uint32 ProcessPacketCallback (Uint32, void*);
|
static Uint32 ProcessPacketCallback (Uint32, void*);
|
||||||
public:
|
public:
|
||||||
@ -20,19 +20,31 @@ class Server
|
|||||||
UDPpacket **UDPPacketV;
|
UDPpacket **UDPPacketV;
|
||||||
Server (void)
|
Server (void)
|
||||||
{
|
{
|
||||||
mProcessPacketCallbackRunning = false;
|
|
||||||
UDPsocks.clear ();
|
UDPsocks.clear ();
|
||||||
SocketSet = NULL;
|
SocketSet = NULL;
|
||||||
UDPPacketV = NULL;
|
UDPPacketV = NULL;
|
||||||
|
mProcessPacketMutex = NULL;
|
||||||
|
}
|
||||||
|
~Server (void)
|
||||||
|
{
|
||||||
|
// UDP_CloseAll ();
|
||||||
|
// mProcessPacketMutex = SDL_CreateMutex ();
|
||||||
|
// UDPsocks.clear ();
|
||||||
|
// SocketSet = NULL;
|
||||||
|
// UDPPacketV = NULL;
|
||||||
}
|
}
|
||||||
Server (const char* host, const Uint16 port);
|
Server (const char* host, const Uint16 port);
|
||||||
UDPsocket UDP_Open (Uint16 port);
|
UDPsocket UDP_Open (Uint16 port);
|
||||||
void UDP_CloseAll (void);
|
void UDP_CloseAll (void);
|
||||||
void ProcessPacket (UDPpacket* packet);
|
void ProcessPacket (UDPpacket* packet);
|
||||||
void Start (void);
|
int Start (void);
|
||||||
void Stop (void);
|
int Stop (void);
|
||||||
bool ProcessPacketCallbackRunning (void)
|
int Lock (void)
|
||||||
{
|
{
|
||||||
return mProcessPacketCallbackRunning;
|
return SDL_LockMutex (mProcessPacketMutex);
|
||||||
|
}
|
||||||
|
int Unlock (void)
|
||||||
|
{
|
||||||
|
return SDL_UnlockMutex (mProcessPacketMutex);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -3,8 +3,8 @@
|
|||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
#include <SDL_log.h>
|
#include <SDL_log.h>
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#define DEBUG_LOG(...) debug_log (__FILE__, __LINE__, __VA_ARGS__)
|
#define DEBUG_LOG(...) debug_log (__FILE__, __LINE__, __func__, __VA_ARGS__)
|
||||||
static void debug_log (const char* filename, const int lineno, const char* fmt, ...)
|
static void debug_log (const char* filename, const int lineno, const char* func, const char* fmt, ...)
|
||||||
{
|
{
|
||||||
char buf[8192];
|
char buf[8192];
|
||||||
va_list ap;
|
va_list ap;
|
||||||
@ -14,7 +14,7 @@ static void debug_log (const char* filename, const int lineno, const char* fmt,
|
|||||||
vsnprintf (buf, (sizeof buf) - 1, fmt, ap);
|
vsnprintf (buf, (sizeof buf) - 1, fmt, ap);
|
||||||
va_end (ap);
|
va_end (ap);
|
||||||
|
|
||||||
SDL_LogMessage (SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "%s:%d: %s", filename, lineno, buf);
|
SDL_LogMessage (SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "%s:%d: %s(): %s", filename, lineno, func, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
6
include/util.h
Normal file
6
include/util.h
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <stddef.h> /* size_t */
|
||||||
|
|
||||||
|
int IsProbablyAscii (const void *, const size_t);
|
||||||
|
void HexDump (const void *, const size_t);
|
@ -1,10 +1,13 @@
|
|||||||
#include "debug.h"
|
#include "debug.h"
|
||||||
#include "Client.h"
|
#include "Client.h"
|
||||||
|
|
||||||
Client::Client (const char* host, const Uint16 port, const int channel)
|
#include <arpa/inet.h> /* inet_ntop */
|
||||||
|
|
||||||
|
Client::Client (const char *host, const Uint16 port, const int channel)
|
||||||
{
|
{
|
||||||
UDPsocket udpsock;
|
UDPsocket udpsock;
|
||||||
if (NULL != (udpsock = UDP_Open (host, SDL_SwapBE16 (port), channel)))
|
DEBUG_LOG ("Attempting to connect %s:%d ch%d", host, port, channel);
|
||||||
|
if (NULL != (udpsock = UDP_Open (host, port, channel)))
|
||||||
{
|
{
|
||||||
mHost = host;
|
mHost = host;
|
||||||
mPort = port;
|
mPort = port;
|
||||||
@ -15,25 +18,28 @@ Client::Client (const char* host, const Uint16 port, const int channel)
|
|||||||
|
|
||||||
|
|
||||||
UDPsocket
|
UDPsocket
|
||||||
Client::UDP_Open (const char* host, const Uint16 port, const int channel)
|
Client::UDP_Open (const char *host, const Uint16 port, const int channel)
|
||||||
{
|
{
|
||||||
IPaddress address;
|
char hoststring[128];
|
||||||
|
IPaddress ipaddress;
|
||||||
UDPsocket udpsock;
|
UDPsocket udpsock;
|
||||||
if (!(udpsock = SDLNet_UDP_Open (0)))
|
if (!(udpsock = SDLNet_UDP_Open (0)))
|
||||||
{
|
{
|
||||||
DEBUG_LOG ("SDLNet_UDP_Open: %s", SDLNet_GetError ());
|
DEBUG_LOG ("SDLNet_UDP_Open: %s", SDLNet_GetError ());
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (SDLNet_ResolveHost (&address, host, port) < 0)
|
if (SDLNet_ResolveHost (&ipaddress, host, port) < 0)
|
||||||
{
|
{
|
||||||
DEBUG_LOG ("SDLNet_ResolveHost: %s", SDLNet_GetError ());
|
DEBUG_LOG ("SDLNet_ResolveHost: %s", SDLNet_GetError ());
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (SDLNet_UDP_Bind (udpsock, channel, &address) < 0)
|
if (SDLNet_UDP_Bind (udpsock, channel, &ipaddress) < 0)
|
||||||
{
|
{
|
||||||
DEBUG_LOG ("SDLNet_UDP_Bind: %s", SDLNet_GetError ());
|
DEBUG_LOG ("SDLNet_UDP_Bind: %s", SDLNet_GetError ());
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
inet_ntop (AF_INET, &ipaddress.host, hoststring, 128);
|
||||||
|
DEBUG_LOG ("Bound socket %s:%d ch%d", hoststring, ipaddress.port, channel);
|
||||||
return udpsock;
|
return udpsock;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,9 +6,9 @@
|
|||||||
|
|
||||||
Server::Server (const char *host, const Uint16 port)
|
Server::Server (const char *host, const Uint16 port)
|
||||||
{
|
{
|
||||||
|
char hoststring[128];
|
||||||
IPaddress ipaddress;
|
IPaddress ipaddress;
|
||||||
UDPsocket udpsock;
|
UDPsocket udpsock;
|
||||||
char hoststring[128];
|
|
||||||
if (SDLNet_ResolveHost (&ipaddress, host, SDL_SwapBE16 (port)) < 0)
|
if (SDLNet_ResolveHost (&ipaddress, host, SDL_SwapBE16 (port)) < 0)
|
||||||
{
|
{
|
||||||
DEBUG_LOG ("SDLNet_ResolveHost: %s", SDLNet_GetError ());
|
DEBUG_LOG ("SDLNet_ResolveHost: %s", SDLNet_GetError ());
|
||||||
@ -17,9 +17,8 @@ Server::Server (const char *host, const Uint16 port)
|
|||||||
if (NULL != (udpsock = UDP_Open (ipaddress.port)))
|
if (NULL != (udpsock = UDP_Open (ipaddress.port)))
|
||||||
{
|
{
|
||||||
UDPsocks.push_back (udpsock);
|
UDPsocks.push_back (udpsock);
|
||||||
mProcessPacketCallbackRunning = false;
|
mProcessPacketMutex = SDL_CreateMutex ();
|
||||||
inet_ntop (AF_INET, &ipaddress.host, hoststring, 128);
|
inet_ntop (AF_INET, &ipaddress.host, hoststring, 128);
|
||||||
SDL_Log ("Listening on %s:%d", hoststring, ipaddress.port);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -31,10 +30,8 @@ Server::Server (const char *host, const Uint16 port)
|
|||||||
void
|
void
|
||||||
Server::UDP_CloseAll (void)
|
Server::UDP_CloseAll (void)
|
||||||
{
|
{
|
||||||
size_t i, nclients;
|
const size_t nclients = UDPsocks.size ();
|
||||||
nclients = UDPsocks.size ();
|
for (size_t i = 0; i < nclients; i++)
|
||||||
SDL_Log ("Server::~Server %ld", nclients);
|
|
||||||
for (i = 0; i < nclients; i++)
|
|
||||||
{
|
{
|
||||||
SDLNet_UDP_Close (UDPsocks[i]);
|
SDLNet_UDP_Close (UDPsocks[i]);
|
||||||
UDPsocks[i] = NULL;
|
UDPsocks[i] = NULL;
|
||||||
@ -83,54 +80,88 @@ Server::ProcessPacket (UDPpacket * packet)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
int
|
||||||
Server::Start (void)
|
Server::Start (void)
|
||||||
{
|
{
|
||||||
size_t i;
|
int l;
|
||||||
int nclients, numused;
|
int numused;
|
||||||
nclients = (int) UDPsocks.size ();
|
|
||||||
SDL_Log ("Server %d clients", nclients);
|
if (!mProcessPacketMutex)
|
||||||
|
{
|
||||||
|
mProcessPacketMutex = SDL_CreateMutex ();
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((l = Lock ()) < 0)
|
||||||
|
{
|
||||||
|
DEBUG_LOG ("Lock failed: %s", SDL_GetError ());
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
|
const size_t nclients = UDPsocks.size ();
|
||||||
|
if (!nclients)
|
||||||
|
{
|
||||||
|
SDL_Log ("No Clients!");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
DEBUG_LOG ("Serving %ld clients", nclients);
|
||||||
|
}
|
||||||
|
|
||||||
if (!(UDPPacketV = SDLNet_AllocPacketV (nclients, MAX_PACKET_SIZE)))
|
if (!(UDPPacketV = SDLNet_AllocPacketV (nclients, MAX_PACKET_SIZE)))
|
||||||
{
|
{
|
||||||
DEBUG_LOG ("SDLNet_AllocPacketV: %s", SDLNet_GetError ());
|
DEBUG_LOG ("SDLNet_AllocPacketV: %s", SDLNet_GetError ());
|
||||||
return;
|
return -1;
|
||||||
}
|
}
|
||||||
if (!(SocketSet = SDLNet_AllocSocketSet (nclients)))
|
if (!(SocketSet = SDLNet_AllocSocketSet (nclients)))
|
||||||
{
|
{
|
||||||
DEBUG_LOG ("SDLNet_AllocSocketSet: %s", SDLNet_GetError ());
|
DEBUG_LOG ("SDLNet_AllocSocketSet: %s", SDLNet_GetError ());
|
||||||
return;
|
return -1;
|
||||||
}
|
}
|
||||||
for (i = 0; i < (size_t) nclients; i++)
|
for (size_t i = 0; i < nclients; i++)
|
||||||
{
|
{
|
||||||
numused = SDLNet_UDP_AddSocket (SocketSet, UDPsocks[i]);
|
numused = SDLNet_UDP_AddSocket (SocketSet, UDPsocks[i]);
|
||||||
if (numused < 0)
|
if (numused < 0)
|
||||||
{
|
{
|
||||||
DEBUG_LOG ("SDLNet_AddSocket: %s", SDLNet_GetError ());
|
DEBUG_LOG ("SDLNet_AddSocket: %s", SDLNet_GetError ());
|
||||||
return;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mProcessPacketCallbackRunning = true;
|
|
||||||
mProcessPacketCallbackTimerID = SDL_AddTimer (10, Server::ProcessPacketCallback, (void *) this);
|
mProcessPacketCallbackTimerID = SDL_AddTimer (10, Server::ProcessPacketCallback, (void *) this);
|
||||||
|
|
||||||
|
return Unlock ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
int
|
||||||
Server::Stop (void)
|
Server::Stop (void)
|
||||||
{
|
{
|
||||||
size_t i, nclients;
|
int l;
|
||||||
mProcessPacketCallbackRunning = false;
|
if ((l = Lock ()) < 0)
|
||||||
|
{
|
||||||
|
DEBUG_LOG ("Lock failed");
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
|
||||||
SDL_RemoveTimer (mProcessPacketCallbackTimerID);
|
SDL_RemoveTimer (mProcessPacketCallbackTimerID);
|
||||||
|
|
||||||
nclients = UDPsocks.size ();
|
const size_t nclients = UDPsocks.size ();
|
||||||
for (i = 0; i < nclients; i++)
|
for (size_t i = 0; i < nclients; i++)
|
||||||
{
|
{
|
||||||
SDLNet_UDP_DelSocket (SocketSet, UDPsocks[i]);
|
SDLNet_UDP_DelSocket (SocketSet, UDPsocks[i]);
|
||||||
}
|
}
|
||||||
SDLNet_FreeSocketSet (SocketSet);
|
|
||||||
SocketSet = NULL;
|
|
||||||
|
|
||||||
|
SDLNet_FreeSocketSet (SocketSet);
|
||||||
SDLNet_FreePacketV (UDPPacketV);
|
SDLNet_FreePacketV (UDPPacketV);
|
||||||
|
SocketSet = NULL;
|
||||||
UDPPacketV = NULL;
|
UDPPacketV = NULL;
|
||||||
|
|
||||||
|
Unlock ();
|
||||||
|
SDL_DestroyMutex (mProcessPacketMutex);
|
||||||
|
mProcessPacketMutex = NULL;
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -141,10 +172,13 @@ Server::ProcessPacketCallback (Uint32 interval, void *param)
|
|||||||
size_t i, j;
|
size_t i, j;
|
||||||
|
|
||||||
SDL_assert (NULL != param);
|
SDL_assert (NULL != param);
|
||||||
Server & server = *(Server *) param;
|
Server& server = *(Server *) param;
|
||||||
|
|
||||||
if (!server.ProcessPacketCallbackRunning ())
|
if (server.Lock () < 0)
|
||||||
|
{
|
||||||
|
DEBUG_LOG ("Lock failed");
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
numready = SDLNet_CheckSockets (server.SocketSet, ~0);
|
numready = SDLNet_CheckSockets (server.SocketSet, ~0);
|
||||||
if (numready < 0)
|
if (numready < 0)
|
||||||
@ -175,5 +209,7 @@ Server::ProcessPacketCallback (Uint32 interval, void *param)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
server.Unlock ();
|
||||||
|
|
||||||
return interval;
|
return interval;
|
||||||
}
|
}
|
||||||
|
@ -11,14 +11,16 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <arpa/inet.h> /* inet_ntop */
|
||||||
/* XXX gross */
|
/* XXX gross */
|
||||||
int
|
int
|
||||||
Buffer_Write_UDP (UDPsocket udpsock, const int channel, const size_t mtu, const void *buf, const size_t size)
|
Buffer_Write_UDP (UDPsocket udpsock, const int channel, const size_t mtu, const void *buf, const size_t size)
|
||||||
{
|
{
|
||||||
|
char hoststring[128];
|
||||||
const char *bufPtr;
|
const char *bufPtr;
|
||||||
const char *bufPtrEnd;
|
const char *bufPtrEnd;
|
||||||
int numsent, npackets;
|
int numsent, npackets;
|
||||||
IPaddress *address;
|
IPaddress *ipaddress;
|
||||||
size_t i, our_nsent, our_mtu;
|
size_t i, our_nsent, our_mtu;
|
||||||
UDPpacket **packetV;
|
UDPpacket **packetV;
|
||||||
SDL_assert (mtu > 0);
|
SDL_assert (mtu > 0);
|
||||||
@ -30,19 +32,24 @@ Buffer_Write_UDP (UDPsocket udpsock, const int channel, const size_t mtu, const
|
|||||||
npackets = ceil ((double) size / (double) our_mtu);
|
npackets = ceil ((double) size / (double) our_mtu);
|
||||||
if (!npackets)
|
if (!npackets)
|
||||||
{
|
{
|
||||||
DEBUG_LOG ("File_Write_UDP: zero length packet\n");
|
DEBUG_LOG ("File_Write_UDP: zero length packet");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
packetV = SDLNet_AllocPacketV (npackets, our_mtu);
|
packetV = SDLNet_AllocPacketV (npackets, our_mtu);
|
||||||
if (!packetV)
|
if (!packetV)
|
||||||
{
|
{
|
||||||
DEBUG_LOG ("SDLNet_AllocPacketV: %s\n", SDLNet_GetError ());
|
DEBUG_LOG ("SDLNet_AllocPacketV: %s", SDLNet_GetError ());
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
i = 0;
|
i = 0;
|
||||||
bufPtr = (const char *) buf;
|
bufPtr = (const char *) buf;
|
||||||
bufPtrEnd = bufPtr + size;
|
bufPtrEnd = bufPtr + size;
|
||||||
address = SDLNet_UDP_GetPeerAddress (udpsock, channel);
|
if (NULL == (ipaddress = SDLNet_UDP_GetPeerAddress (udpsock, channel)))
|
||||||
|
{
|
||||||
|
DEBUG_LOG ("SDLNet_UDP_GetPeerAddress: %s", SDLNet_GetError ());
|
||||||
|
}
|
||||||
|
inet_ntop (AF_INET, &ipaddress->host, hoststring, 128);
|
||||||
|
DEBUG_LOG ("Bound socket %s:%d ch%d", hoststring, ipaddress->port, channel);
|
||||||
size_t nsent = size;
|
size_t nsent = size;
|
||||||
while (i < (size_t) npackets && bufPtr < bufPtrEnd)
|
while (i < (size_t) npackets && bufPtr < bufPtrEnd)
|
||||||
{
|
{
|
||||||
@ -50,7 +57,7 @@ Buffer_Write_UDP (UDPsocket udpsock, const int channel, const size_t mtu, const
|
|||||||
if (our_nsent > our_mtu)
|
if (our_nsent > our_mtu)
|
||||||
our_nsent = our_mtu;
|
our_nsent = our_mtu;
|
||||||
packetV[i]->channel = channel;
|
packetV[i]->channel = channel;
|
||||||
SDL_memcpy (&packetV[i]->address, address, sizeof (IPaddress));
|
SDL_memcpy (&packetV[i]->address, ipaddress, sizeof (IPaddress));
|
||||||
SDL_memset (packetV[i]->data, 0, our_mtu);
|
SDL_memset (packetV[i]->data, 0, our_mtu);
|
||||||
SDL_memcpy (packetV[i]->data, bufPtr, our_nsent);
|
SDL_memcpy (packetV[i]->data, bufPtr, our_nsent);
|
||||||
packetV[i]->len = our_nsent;
|
packetV[i]->len = our_nsent;
|
||||||
@ -61,7 +68,7 @@ Buffer_Write_UDP (UDPsocket udpsock, const int channel, const size_t mtu, const
|
|||||||
numsent = SDLNet_UDP_SendV (udpsock, packetV, npackets);
|
numsent = SDLNet_UDP_SendV (udpsock, packetV, npackets);
|
||||||
if (!numsent)
|
if (!numsent)
|
||||||
{
|
{
|
||||||
SDL_Log ("SDLNet_UDP_SendV (%d): %s\n", npackets, SDLNet_GetError ());
|
SDL_Log ("SDLNet_UDP_SendV (%d): %s", npackets, SDLNet_GetError ());
|
||||||
}
|
}
|
||||||
SDLNet_FreePacketV (packetV);
|
SDLNet_FreePacketV (packetV);
|
||||||
return numsent;
|
return numsent;
|
||||||
@ -98,7 +105,7 @@ SendBuffer_UDP (const char *host, const Uint16 port, const int channel, const si
|
|||||||
UDPsocket udpsock;
|
UDPsocket udpsock;
|
||||||
numsent = 0;
|
numsent = 0;
|
||||||
Client client (host, port, channel);
|
Client client (host, port, channel);
|
||||||
if (udpsock)
|
if (NULL != (udpsock = client.First ()))
|
||||||
{
|
{
|
||||||
numsent = Buffer_Write_UDP (udpsock, channel, mtu, buf, size);
|
numsent = Buffer_Write_UDP (udpsock, channel, mtu, buf, size);
|
||||||
client.UDP_CloseAll ();
|
client.UDP_CloseAll ();
|
||||||
@ -114,7 +121,7 @@ SendFile_UDP (const char *host, const Uint16 port, const int channel, const size
|
|||||||
UDPsocket udpsock;
|
UDPsocket udpsock;
|
||||||
numsent = 0;
|
numsent = 0;
|
||||||
Client client (host, port, channel);
|
Client client (host, port, channel);
|
||||||
if (udpsock)
|
if (NULL != (udpsock = client.First ()))
|
||||||
{
|
{
|
||||||
numsent = File_Write_UDP (udpsock, channel, path, mtu);
|
numsent = File_Write_UDP (udpsock, channel, path, mtu);
|
||||||
client.UDP_CloseAll ();
|
client.UDP_CloseAll ();
|
||||||
|
@ -81,6 +81,7 @@ struct Client_State
|
|||||||
|
|
||||||
static std::shared_ptr<Cube> cube;
|
static std::shared_ptr<Cube> cube;
|
||||||
static std::shared_ptr<Quad> quad;
|
static std::shared_ptr<Quad> quad;
|
||||||
|
static std::shared_ptr<Client> client;
|
||||||
|
|
||||||
static Camera camera;
|
static Camera camera;
|
||||||
static ShaderProgram shader;
|
static ShaderProgram shader;
|
||||||
@ -90,8 +91,8 @@ static struct Uniform_Values values;
|
|||||||
int
|
int
|
||||||
main (int argc, char **argv)
|
main (int argc, char **argv)
|
||||||
{
|
{
|
||||||
SDL_Event event;
|
|
||||||
glm::mat4 model, view, projection;
|
glm::mat4 model, view, projection;
|
||||||
|
SDL_Event event;
|
||||||
|
|
||||||
my_Init (argc, argv);
|
my_Init (argc, argv);
|
||||||
|
|
||||||
@ -108,7 +109,8 @@ main (int argc, char **argv)
|
|||||||
|
|
||||||
state.mtu = 1450;
|
state.mtu = 1450;
|
||||||
state.quit = 0;
|
state.quit = 0;
|
||||||
if (NULL == (state.udpsock = Client::UDP_Open (state.host, state.port, state.channel)))
|
client = std::make_shared<Client> (state.host, state.port, state.channel);
|
||||||
|
if (NULL == (state.udpsock = client->First ()))
|
||||||
{
|
{
|
||||||
SDL_Log ("Unable to open client port");
|
SDL_Log ("Unable to open client port");
|
||||||
goto _out;
|
goto _out;
|
||||||
@ -156,7 +158,7 @@ main (int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
_out:
|
_out:
|
||||||
client.UDP_CloseAll ();
|
client->UDP_CloseAll ();
|
||||||
SDL_GL_DeleteContext (state.GLContext);
|
SDL_GL_DeleteContext (state.GLContext);
|
||||||
SDL_DestroyWindow (state.Window);
|
SDL_DestroyWindow (state.Window);
|
||||||
common_SDL_Quit ();
|
common_SDL_Quit ();
|
||||||
@ -219,10 +221,8 @@ static int
|
|||||||
my_SDL_MouseMotion (SDL_Event * event)
|
my_SDL_MouseMotion (SDL_Event * event)
|
||||||
{
|
{
|
||||||
SDL_assert (NULL != event);
|
SDL_assert (NULL != event);
|
||||||
const float
|
const float xpos = event->motion.x;
|
||||||
xpos = event->motion.x;
|
const float ypos = event->motion.y;
|
||||||
const float
|
|
||||||
ypos = event->motion.y;
|
|
||||||
|
|
||||||
if (!state.InputCaptureMouse || state.ImGuiEnabled)
|
if (!state.InputCaptureMouse || state.ImGuiEnabled)
|
||||||
return SDL_FALSE;
|
return SDL_FALSE;
|
||||||
@ -235,10 +235,8 @@ my_SDL_MouseMotion (SDL_Event * event)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// reversed since y-coordinates go from bottom to top
|
// reversed since y-coordinates go from bottom to top
|
||||||
const float
|
const float Xoffset = xpos - values.iMouse.x;
|
||||||
Xoffset = xpos - values.iMouse.x;
|
const float Yoffset = values.iMouse.y - ypos;
|
||||||
const float
|
|
||||||
Yoffset = values.iMouse.y - ypos;
|
|
||||||
|
|
||||||
values.iMouse.x = xpos;
|
values.iMouse.x = xpos;
|
||||||
values.iMouse.y = ypos;
|
values.iMouse.y = ypos;
|
||||||
@ -296,10 +294,8 @@ my_Input (SDL_Event * event)
|
|||||||
static int
|
static int
|
||||||
my_AnalogInput (void)
|
my_AnalogInput (void)
|
||||||
{
|
{
|
||||||
const Uint8 *
|
const Uint8* kbd = SDL_GetKeyboardState (NULL);
|
||||||
kbd = SDL_GetKeyboardState (NULL);
|
const float dt = 0.016;
|
||||||
const float
|
|
||||||
dt = 0.016;
|
|
||||||
(void) state;
|
(void) state;
|
||||||
|
|
||||||
camera.ProcessKeyboard (kbd[SDL_SCANCODE_W] ? Camera::Movement::FORWARD : Camera::Movement::NONE, dt);
|
camera.ProcessKeyboard (kbd[SDL_SCANCODE_W] ? Camera::Movement::FORWARD : Camera::Movement::NONE, dt);
|
||||||
@ -333,13 +329,23 @@ my_Input_Key (SDL_Event * event)
|
|||||||
{
|
{
|
||||||
if (down)
|
if (down)
|
||||||
{
|
{
|
||||||
state.InputCaptureMouse = true;
|
|
||||||
state.InputFirstMouse = true;
|
state.InputFirstMouse = true;
|
||||||
|
state.InputCaptureMouse = true;
|
||||||
state.InputRelativeMouseMode = true;
|
state.InputRelativeMouseMode = true;
|
||||||
common_SDL_ToggleFullscreen (SDL_GetWindowFromID (event->key.windowID));
|
common_SDL_ToggleFullscreen (SDL_GetWindowFromID (event->key.windowID));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
case SDLK_m:
|
||||||
|
{
|
||||||
|
if (down)
|
||||||
|
{
|
||||||
|
state.InputFirstMouse = true;
|
||||||
|
state.InputCaptureMouse = !state.InputCaptureMouse;
|
||||||
|
state.InputRelativeMouseMode = !state.InputRelativeMouseMode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
case SDLK_r:
|
case SDLK_r:
|
||||||
{
|
{
|
||||||
if (down)
|
if (down)
|
||||||
@ -354,7 +360,12 @@ my_Input_Key (SDL_Event * event)
|
|||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
{
|
{
|
||||||
Buffer_Write_UDP (state.udpsock, state.channel, state.mtu, event, sizeof (SDL_Event));
|
if (event->key.keysym.scancode != SDL_GetScancodeFromKey (event->key.keysym.sym))
|
||||||
|
{
|
||||||
|
DEBUG_LOG ("Physical %s key acting as %s key", SDL_GetScancodeName (event->key.keysym.scancode), SDL_GetKeyName (event->key.keysym.sym));
|
||||||
|
}
|
||||||
|
// SDL_Log ("%d %d %ld", state.channel, state.mtu, sizeof (SDL_Event));
|
||||||
|
Buffer_Write_UDP (state.udpsock, state.channel, state.mtu, (const void*)event, sizeof (SDL_Event));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return quit;
|
return quit;
|
||||||
@ -394,9 +405,9 @@ static void
|
|||||||
my_GetOpt (int argc, char **argv)
|
my_GetOpt (int argc, char **argv)
|
||||||
{
|
{
|
||||||
int opt;
|
int opt;
|
||||||
state.channel = 1;
|
|
||||||
state.host = "localhost";
|
state.host = "localhost";
|
||||||
state.port = 6666;
|
state.port = 6666;
|
||||||
|
state.channel = 1;
|
||||||
while ((opt = getopt (argc, argv, "hp:s:")) != -1)
|
while ((opt = getopt (argc, argv, "hp:s:")) != -1)
|
||||||
{
|
{
|
||||||
switch (opt)
|
switch (opt)
|
||||||
|
@ -6,6 +6,10 @@
|
|||||||
#include "events_common.h"
|
#include "events_common.h"
|
||||||
#include "trim.h"
|
#include "trim.h"
|
||||||
#include "lib_lua_common.h"
|
#include "lib_lua_common.h"
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#include "util.h"
|
||||||
|
};
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -34,7 +38,11 @@ main (int argc, char **argv)
|
|||||||
common_SDL_Init ();
|
common_SDL_Init ();
|
||||||
|
|
||||||
server = Server (host, port);
|
server = Server (host, port);
|
||||||
server.Start ();
|
if (server.Start () < 0)
|
||||||
|
{
|
||||||
|
SDL_Log ("Couldn't start server!");
|
||||||
|
exit (EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
done = false;
|
done = false;
|
||||||
while (!done)
|
while (!done)
|
||||||
@ -110,15 +118,18 @@ my_ProcessPacket (UDPpacket* packet)
|
|||||||
len = packet->len;
|
len = packet->len;
|
||||||
SDL_memcpy (&event, packet->data, len);
|
SDL_memcpy (&event, packet->data, len);
|
||||||
|
|
||||||
if ((event.type != SDL_KEYDOWN) && (event.type != SDL_KEYUP))
|
// SDL_Log ("%d, %x", packet->len, event.type);
|
||||||
|
|
||||||
|
if (!((event.type == SDL_KEYDOWN) || (event.type == SDL_KEYUP)))
|
||||||
{
|
{
|
||||||
|
std::string buf ((const char*)packet->data, len);
|
||||||
|
if (IsProbablyAscii (buf.c_str (), buf.length ()))
|
||||||
|
printf ("%s", buf.c_str ());
|
||||||
|
else
|
||||||
|
HexDump (buf.c_str (), buf.length ());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.key.keysym.scancode != SDL_GetScancodeFromKey (event.key.keysym.sym))
|
|
||||||
{
|
|
||||||
DEBUG_LOG ("Physical %s key acting as %s key", SDL_GetScancodeName (event.key.keysym.scancode), SDL_GetKeyName (event.key.keysym.sym));
|
|
||||||
}
|
|
||||||
std::string str = std::string (SDL_GetKeyName (event.key.keysym.sym));
|
std::string str = std::string (SDL_GetKeyName (event.key.keysym.sym));
|
||||||
bool down = (event.type == SDL_KEYDOWN);
|
bool down = (event.type == SDL_KEYDOWN);
|
||||||
|
|
||||||
|
47
src/util.c
Normal file
47
src/util.c
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
#include <stdio.h> /* printf */
|
||||||
|
#include <stdint.h> /* uint8_t */
|
||||||
|
#include <math.h> /* round */
|
||||||
|
|
||||||
|
int
|
||||||
|
IsProbablyAscii (const void *buf, const size_t len)
|
||||||
|
{
|
||||||
|
const uint8_t* bufPtr;
|
||||||
|
const uint8_t* bufPtrEnd;
|
||||||
|
size_t nAsciiCount;
|
||||||
|
nAsciiCount = 0;
|
||||||
|
bufPtr = (const uint8_t*) buf;
|
||||||
|
bufPtrEnd = bufPtr + len;
|
||||||
|
while (bufPtr < bufPtrEnd && (NULL != bufPtr) && (*bufPtr != '\0'))
|
||||||
|
{
|
||||||
|
nAsciiCount += (*bufPtr >= ' ') && (*bufPtr <= '~');
|
||||||
|
bufPtr++;
|
||||||
|
}
|
||||||
|
return (int) round ((double) nAsciiCount / (double) len);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
HexDump (const void *buf, const size_t len)
|
||||||
|
{
|
||||||
|
const uint8_t* bufPtr;
|
||||||
|
const uint8_t* bufPtrEnd;
|
||||||
|
const size_t width = 16;
|
||||||
|
size_t nout;
|
||||||
|
nout = 0;
|
||||||
|
bufPtr = (const uint8_t*) buf;
|
||||||
|
bufPtrEnd = bufPtr + len;
|
||||||
|
while (bufPtr < bufPtrEnd && (NULL != bufPtr))
|
||||||
|
{
|
||||||
|
if (nout == width)
|
||||||
|
{
|
||||||
|
printf ("\n");
|
||||||
|
nout = 0;
|
||||||
|
}
|
||||||
|
printf ("%02x ", *bufPtr);
|
||||||
|
bufPtr++;
|
||||||
|
nout++;
|
||||||
|
}
|
||||||
|
puts ("");
|
||||||
|
}
|
BIN
test/test_Client
Executable file
BIN
test/test_Client
Executable file
Binary file not shown.
64
test/test_Client.cpp
Normal file
64
test/test_Client.cpp
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
#include "debug.h"
|
||||||
|
#include "Client.h"
|
||||||
|
#include "UDP_Write.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
int
|
||||||
|
main (int argc, char **argv)
|
||||||
|
{
|
||||||
|
int numsent;
|
||||||
|
const char *host = "localhost";
|
||||||
|
const Uint16 port = 6666;
|
||||||
|
const int channel = 1;
|
||||||
|
const size_t mtu = 1450;
|
||||||
|
Client c;
|
||||||
|
UDPsocket udpsock;
|
||||||
|
c = Client (host, port, channel);
|
||||||
|
if (!(udpsock = c.First ()))
|
||||||
|
{
|
||||||
|
SDL_Log ("Client error");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string buf = "Hello, world!\n";
|
||||||
|
numsent = Buffer_Write_UDP (udpsock, channel, mtu, buf.c_str (), buf.length ());
|
||||||
|
|
||||||
|
SDL_Log ("%d", numsent);
|
||||||
|
c.UDP_CloseAll ();
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
#if 0
|
||||||
|
IPaddress ipaddress;
|
||||||
|
UDPsocket udpsock;
|
||||||
|
UDPpacket *packet;
|
||||||
|
udpsock = SDLNet_UDP_Open (0);
|
||||||
|
if (SDLNet_ResolveHost (&ipaddress, host, port) < 0)
|
||||||
|
{
|
||||||
|
DEBUG_LOG ("SDLNet_ResolveHost: %s", SDLNet_GetError ());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (SDLNet_UDP_Bind (udpsock, channel, &ipaddress) < 0)
|
||||||
|
{
|
||||||
|
DEBUG_LOG ("SDLNet_UDP_Bind: %s", SDLNet_GetError ());
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
packet = SDLNet_AllocPacket (1024);
|
||||||
|
if (packet)
|
||||||
|
{
|
||||||
|
packet->len = sprintf ((char*)packet->data, "Hello World!");
|
||||||
|
packet->address = ipaddress;
|
||||||
|
numsent = SDLNet_UDP_Send (udpsock, packet->channel, packet);
|
||||||
|
if (!numsent)
|
||||||
|
{
|
||||||
|
DEBUG_LOG ("SDLNet_UDP_Send: %s\n", SDLNet_GetError ());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SDLNet_UDP_Unbind (udpsock, channel);
|
||||||
|
SDLNet_UDP_Close (udpsock);
|
||||||
|
udpsock = NULL;
|
||||||
|
#endif
|
||||||
|
}
|
@ -1,4 +1,8 @@
|
|||||||
#include "FileIO.h"
|
#include "FileIO.h"
|
||||||
|
extern "C"
|
||||||
|
{
|
||||||
|
#include "util.h"
|
||||||
|
};
|
||||||
|
|
||||||
#include <stdint.h> /* uint8_t */
|
#include <stdint.h> /* uint8_t */
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -7,48 +11,8 @@
|
|||||||
#include <errno.h> /* errno */
|
#include <errno.h> /* errno */
|
||||||
#include <string.h> /* strerror */
|
#include <string.h> /* strerror */
|
||||||
|
|
||||||
static int
|
|
||||||
IsProbablyAscii (const void *buf, const size_t len)
|
|
||||||
{
|
|
||||||
const uint8_t *bufPtr;
|
|
||||||
const uint8_t *bufPtrEnd;
|
|
||||||
size_t nAsciiCount;
|
|
||||||
nAsciiCount = 0;
|
|
||||||
bufPtr = (const uint8_t *) buf;
|
|
||||||
bufPtrEnd = bufPtr + len;
|
|
||||||
while (bufPtr < bufPtrEnd && (NULL != bufPtr) && (*bufPtr != '\0'))
|
|
||||||
{
|
|
||||||
nAsciiCount += (*bufPtr >= ' ') && (*bufPtr <= '~');
|
|
||||||
bufPtr++;
|
|
||||||
}
|
|
||||||
return (int) round ((double) nAsciiCount / (double) len);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
static void
|
|
||||||
HexDump (const void *buf, const size_t len)
|
|
||||||
{
|
|
||||||
const uint8_t *bufPtr;
|
|
||||||
const uint8_t *bufPtrEnd;
|
|
||||||
const size_t width = 16;
|
|
||||||
size_t nout;
|
|
||||||
nout = 0;
|
|
||||||
bufPtr = (const uint8_t *) buf;
|
|
||||||
bufPtrEnd = bufPtr + len;
|
|
||||||
while (bufPtr < bufPtrEnd && (NULL != bufPtr))
|
|
||||||
{
|
|
||||||
if (nout == width)
|
|
||||||
{
|
|
||||||
printf ("\n");
|
|
||||||
nout = 0;
|
|
||||||
}
|
|
||||||
printf ("%02x ", *bufPtr);
|
|
||||||
bufPtr++;
|
|
||||||
nout++;
|
|
||||||
}
|
|
||||||
puts ("");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
main (int argc, char **argv)
|
main (int argc, char **argv)
|
||||||
|
Loading…
Reference in New Issue
Block a user