2019-01-26 03:52:10 +00:00
|
|
|
#ifndef UDPCONNECTION_H
|
|
|
|
#define UDPCONNECTION_H
|
|
|
|
|
2019-01-26 07:22:31 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <threads.h>
|
2019-01-26 10:11:12 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <stdint.h>
|
2019-01-26 07:22:31 +00:00
|
|
|
|
2019-01-26 07:49:54 +00:00
|
|
|
#define UDPC_PLATFORM_WINDOWS 1
|
|
|
|
#define UDPC_PLATFORM_MAC 2
|
|
|
|
#define UDPC_PLATFORM_LINUX 3
|
|
|
|
#define UDPC_PLATFORM_UNKNOWN 0
|
2019-01-26 07:22:31 +00:00
|
|
|
|
|
|
|
#if defined _WIN32
|
2019-01-26 07:49:54 +00:00
|
|
|
#define UDPC_PLATFORM UDPC_PLATFORM_WINDOWS
|
2019-01-26 07:22:31 +00:00
|
|
|
#elif defined __APPLE__
|
2019-01-26 07:49:54 +00:00
|
|
|
#define UDPC_PLATFORM UDPC_PLATFORM_MAC
|
2019-01-26 07:22:31 +00:00
|
|
|
#elif defined __linux__
|
2019-01-26 07:49:54 +00:00
|
|
|
#define UDPC_PLATFORM UDPC_PLATFORM_LINUX
|
2019-01-26 07:22:31 +00:00
|
|
|
#else
|
2019-01-26 07:49:54 +00:00
|
|
|
#define UDPC_PLATFORM UDPC_PLATFORM_UNKNOWN
|
2019-01-26 07:22:31 +00:00
|
|
|
#endif
|
|
|
|
|
2019-01-26 07:49:54 +00:00
|
|
|
#if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
|
2019-01-26 07:22:31 +00:00
|
|
|
#include <winsock2.h>
|
|
|
|
|
|
|
|
#define CleanupSocket(x) closesocket(x)
|
2019-01-26 07:49:54 +00:00
|
|
|
#elif UDPC_PLATFORM == UDPC_PLATFORM_MAC || UDPC_PLATFORM == UDPC_PLATFORM_LINUX
|
2019-01-26 07:22:31 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#define CleanupSocket(x) close(x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define UDPCON_ERR_SOCKETFAIL 1 // failed to create socket
|
|
|
|
#define UDPCON_ERR_SOCKETBINDF 2 // failed to bind socket
|
|
|
|
#define UDPCON_ERR_SOCKETNONBF 3 // failed to set non-blocking on socket
|
|
|
|
#define UDPCON_ERR_MTXFAIL 4 // failed to create mutex
|
|
|
|
#define UDPCON_ERR_CVFAIL 5 // failed to create condition variable
|
|
|
|
#define UDPCON_ERR_THREADFAIL 6 // failed to create thread
|
|
|
|
|
2019-01-26 10:11:12 +00:00
|
|
|
// This struct should not be used outside of this library
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint32_t addr;
|
|
|
|
uint32_t id;
|
|
|
|
/*
|
|
|
|
* 0x1 - is resending
|
|
|
|
* 0x2 - is not received checked
|
|
|
|
* 0x4 - has been re-sent
|
|
|
|
*/
|
|
|
|
uint32_t flags;
|
|
|
|
char *data;
|
|
|
|
struct timespec sent;
|
|
|
|
} UDPC_INTERNAL_PacketInfo;
|
|
|
|
|
|
|
|
// This struct should not be used outside of this library
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* 0x1 - trigger send
|
|
|
|
* 0x2 - is good mode
|
|
|
|
* 0x4 - is good rtt
|
|
|
|
*/
|
|
|
|
uint32_t flags;
|
|
|
|
uint32_t id;
|
|
|
|
uint32_t lseq;
|
|
|
|
uint32_t rseq;
|
|
|
|
uint32_t ack;
|
|
|
|
float timer;
|
|
|
|
float toggleT;
|
|
|
|
float toggleTimer;
|
|
|
|
float toggledTimer;
|
|
|
|
uint16_t port;
|
|
|
|
UDPC_INTERNAL_PacketInfo *sentPkts;
|
|
|
|
UDPC_INTERNAL_PacketInfo *sendPktQueue;
|
|
|
|
struct timespec received;
|
|
|
|
struct timespec sent;
|
|
|
|
struct timespec rtt;
|
|
|
|
} UDPC_INTERNAL_ConnectionData;
|
|
|
|
|
2019-01-26 07:22:31 +00:00
|
|
|
// This struct should not be modified, only passed to functions that require it
|
|
|
|
typedef struct
|
2019-01-26 03:52:10 +00:00
|
|
|
{
|
2019-01-26 07:22:31 +00:00
|
|
|
/*
|
|
|
|
* 0x1 - is threaded
|
|
|
|
*/
|
2019-01-26 10:11:12 +00:00
|
|
|
uint32_t flags;
|
2019-01-26 07:22:31 +00:00
|
|
|
/*
|
|
|
|
* 0x1 - thread should stop
|
|
|
|
*/
|
2019-01-26 10:11:12 +00:00
|
|
|
uint32_t threadFlags;
|
|
|
|
uint32_t error;
|
|
|
|
int socketHandle;
|
|
|
|
struct sockaddr_in socketInfo;
|
|
|
|
thrd_t threadHandle;
|
|
|
|
mtx_t tCVMtx;
|
|
|
|
mtx_t tflagsMtx;
|
|
|
|
cnd_t threadCV;
|
2019-01-26 07:22:31 +00:00
|
|
|
} UDPC_Context;
|
|
|
|
|
2019-01-26 10:11:12 +00:00
|
|
|
UDPC_Context UDPC_init(uint16_t listenPort);
|
2019-01-26 07:22:31 +00:00
|
|
|
|
2019-01-26 10:11:12 +00:00
|
|
|
UDPC_Context UDPC_init_threaded_update(uint16_t listenPort);
|
2019-01-26 07:22:31 +00:00
|
|
|
|
|
|
|
void UDPC_destroy(UDPC_Context *ctx);
|
|
|
|
|
|
|
|
int UDPC_INTERNAL_threadfn(void *context); // internal usage only
|
2019-01-26 03:52:10 +00:00
|
|
|
|
|
|
|
#endif
|