UDPConnection/src/UDPConnection.h

110 lines
2.4 KiB
C
Raw Normal View History

2019-01-26 03:52:10 +00:00
#ifndef UDPCONNECTION_H
#define UDPCONNECTION_H
#include <stdio.h>
#include <threads.h>
2019-01-26 10:11:12 +00:00
#include <time.h>
#include <stdint.h>
#include "UDPC_Defines.h"
2019-01-30 06:46:52 +00:00
#include "UDPC_Deque.h"
#if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
#include <winsock2.h>
#define CleanupSocket(x) closesocket(x)
#elif UDPC_PLATFORM == UDPC_PLATFORM_MAC || UDPC_PLATFORM == UDPC_PLATFORM_LINUX
#include <sys/socket.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <unistd.h>
#define CleanupSocket(x) close(x)
2019-01-31 03:16:01 +00:00
#else
#define CleanupSocket(x) ((void)0)
#endif
2019-01-30 06:46:52 +00:00
#define UDPC_CD_AMOUNT 32
2019-01-31 03:16:01 +00:00
/// This struct should not be used outside of this library
2019-01-26 10:11:12 +00:00
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;
2019-01-31 03:16:01 +00:00
/// This struct should not be used outside of this library
2019-01-26 10:11:12 +00:00
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;
2019-01-30 06:46:52 +00:00
UDPC_Deque *sentPkts;
UDPC_Deque *sendPktQueue;
2019-01-26 10:11:12 +00:00
struct timespec received;
struct timespec sent;
struct timespec rtt;
} UDPC_INTERNAL_ConnectionData;
2019-01-31 03:16:01 +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
{
/*
* 0x1 - is threaded
2019-01-30 06:46:52 +00:00
* 0x2 - is client
*/
2019-01-26 10:11:12 +00:00
uint32_t flags;
/*
* 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-30 06:46:52 +00:00
UDPC_Deque *connected;
2019-01-31 03:16:01 +00:00
struct timespec lastUpdated;
} UDPC_Context;
2019-01-30 06:46:52 +00:00
UDPC_Context* UDPC_init(uint16_t listenPort, int isClient);
2019-01-30 06:46:52 +00:00
UDPC_Context* UDPC_init_threaded_update(uint16_t listenPort, int isClient);
void UDPC_destroy(UDPC_Context *ctx);
uint32_t UDPC_get_error(UDPC_Context *ctx);
const char* UDPC_get_error_str(uint32_t error);
2019-01-31 03:16:01 +00:00
/// If threaded, this function is called automatically
void UDPC_update(UDPC_Context *ctx);
float UDPC_ts_diff_to_seconds(struct timespec *ts0, struct timespec *ts1);
int UDPC_INTERNAL_threadfn(void *context); // internal usage only
2019-01-26 03:52:10 +00:00
#endif