2019-01-26 03:52:10 +00:00
|
|
|
#ifndef UDPCONNECTION_H
|
|
|
|
#define UDPCONNECTION_H
|
|
|
|
|
2019-03-06 11:31:23 +00:00
|
|
|
/*!
|
|
|
|
* Any function or struct starting with "UDPC_INTERNAL" should never be used,
|
|
|
|
* as they are used internally by this library.
|
|
|
|
*/
|
|
|
|
|
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-29 02:53:39 +00:00
|
|
|
#include "UDPC_Defines.h"
|
2019-01-30 06:46:52 +00:00
|
|
|
#include "UDPC_Deque.h"
|
2019-02-15 04:52:38 +00:00
|
|
|
#include "UDPC_HashMap.h"
|
2019-01-26 07:22:31 +00:00
|
|
|
|
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)
|
2019-01-31 03:16:01 +00:00
|
|
|
#else
|
|
|
|
#define CleanupSocket(x) ((void)0)
|
2019-01-26 07:22:31 +00:00
|
|
|
#endif
|
|
|
|
|
2019-02-01 10:22:04 +00:00
|
|
|
#define UDPC_ATOSTR_BUF_SIZE 16
|
2019-01-30 06:46:52 +00:00
|
|
|
|
2019-02-20 06:13:57 +00:00
|
|
|
/// (void *userData, uint32_t address)
|
|
|
|
/*!
|
|
|
|
* Note address is in network byte order (usually big-endian)
|
|
|
|
*/
|
|
|
|
typedef void (*UDPC_callback_connected)(void*, uint32_t);
|
|
|
|
|
|
|
|
/// (void *userData, uint32_t address)
|
|
|
|
/*!
|
|
|
|
* Note address is in network byte order (usually big-endian)
|
|
|
|
*/
|
|
|
|
typedef void (*UDPC_callback_disconnected)(void*, uint32_t);
|
|
|
|
|
2019-03-06 04:23:22 +00:00
|
|
|
/// (void *userData, uint32_t address, char *packetData, uint32_t packetSize)
|
2019-02-20 06:13:57 +00:00
|
|
|
/*!
|
|
|
|
* The data pointed to by the packetData argument is to data internally managed
|
|
|
|
* by the UDPC_Context. It will change every time this callback is called so do
|
|
|
|
* not depend on it persisting. This means you should copy the data out of it
|
|
|
|
* when the callback is invoked and work with the copied data.
|
|
|
|
*/
|
2019-03-06 04:23:22 +00:00
|
|
|
typedef void (*UDPC_callback_received)(void*, uint32_t, char*, uint32_t);
|
2019-02-20 06:13:57 +00:00
|
|
|
|
2019-01-31 03:16:01 +00:00
|
|
|
/// This struct should not be used outside of this library
|
2019-02-04 08:21:49 +00:00
|
|
|
typedef struct {
|
2019-02-01 10:22:04 +00:00
|
|
|
uint32_t addr; // in network order (big-endian)
|
2019-01-26 10:11:12 +00:00
|
|
|
uint32_t id;
|
|
|
|
/*
|
|
|
|
* 0x1 - is resending
|
2019-01-31 10:44:32 +00:00
|
|
|
* 0x2 - is check received packet
|
2019-01-26 10:11:12 +00:00
|
|
|
* 0x4 - has been re-sent
|
2019-03-04 06:04:16 +00:00
|
|
|
* 0x8 - initiate connection packet
|
2019-01-26 10:11:12 +00:00
|
|
|
*/
|
|
|
|
uint32_t flags;
|
2019-02-20 06:13:57 +00:00
|
|
|
char *data; // no-header in sendPktQueue and receivedPackets, header in sentPkts
|
2019-01-31 10:44:32 +00:00
|
|
|
uint32_t size;
|
2019-01-26 10:11:12 +00:00
|
|
|
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-02-04 08:21:49 +00:00
|
|
|
typedef struct {
|
2019-01-26 10:11:12 +00:00
|
|
|
/*
|
|
|
|
* 0x1 - trigger send
|
|
|
|
* 0x2 - is good mode
|
|
|
|
* 0x4 - is good rtt
|
2019-03-04 06:04:16 +00:00
|
|
|
* 0x8 - is id not set yet / initiating connection to server
|
2019-01-26 10:11:12 +00:00
|
|
|
*/
|
|
|
|
uint32_t flags;
|
|
|
|
uint32_t id;
|
|
|
|
uint32_t lseq;
|
|
|
|
uint32_t rseq;
|
|
|
|
uint32_t ack;
|
|
|
|
float timer;
|
|
|
|
float toggleT;
|
|
|
|
float toggleTimer;
|
|
|
|
float toggledTimer;
|
2019-02-01 10:22:04 +00:00
|
|
|
uint32_t addr; // in network order (big-endian)
|
2019-01-26 10:11:12 +00:00
|
|
|
uint16_t port;
|
2019-01-30 06:46:52 +00:00
|
|
|
UDPC_Deque *sentPkts;
|
|
|
|
UDPC_Deque *sendPktQueue;
|
2019-03-04 06:04:16 +00:00
|
|
|
UDPC_Deque *priorityPktQueue;
|
2019-01-26 10:11:12 +00:00
|
|
|
struct timespec received;
|
|
|
|
struct timespec sent;
|
2019-02-18 07:32:50 +00:00
|
|
|
float rtt;
|
2019-01-26 10:11:12 +00:00
|
|
|
} UDPC_INTERNAL_ConnectionData;
|
|
|
|
|
2019-02-19 04:28:16 +00:00
|
|
|
/// This struct should not be used externally, only passed to functions that require it
|
2019-02-04 08:21:49 +00:00
|
|
|
typedef struct {
|
2019-01-26 07:22:31 +00:00
|
|
|
/*
|
|
|
|
* 0x1 - is threaded
|
2019-01-30 06:46:52 +00:00
|
|
|
* 0x2 - is client
|
2019-02-01 10:22:04 +00:00
|
|
|
* 0x4 - log errors
|
|
|
|
* 0x8 - log warnings
|
|
|
|
* 0x10 - log info
|
|
|
|
* 0x20 - log verbose
|
2019-02-04 06:33:44 +00:00
|
|
|
* 0x40 - accept new connections
|
2019-01-26 07:22:31 +00:00
|
|
|
*/
|
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;
|
2019-03-07 02:57:06 +00:00
|
|
|
uint32_t protocolID;
|
2019-01-26 10:11:12 +00:00
|
|
|
uint32_t error;
|
|
|
|
int socketHandle;
|
|
|
|
struct sockaddr_in socketInfo;
|
|
|
|
thrd_t threadHandle;
|
|
|
|
mtx_t tCVMtx;
|
|
|
|
mtx_t tflagsMtx;
|
|
|
|
cnd_t threadCV;
|
2019-02-15 04:52:38 +00:00
|
|
|
UDPC_HashMap *conMap;
|
2019-01-31 03:16:01 +00:00
|
|
|
struct timespec lastUpdated;
|
2019-02-01 10:22:04 +00:00
|
|
|
char atostrBuf[UDPC_ATOSTR_BUF_SIZE];
|
2019-02-04 06:33:44 +00:00
|
|
|
char recvBuf[UDPC_PACKET_MAX_SIZE];
|
2019-02-21 06:40:30 +00:00
|
|
|
UDPC_Deque *connectedEvents;
|
|
|
|
UDPC_Deque *disconnectedEvents;
|
2019-02-20 06:13:57 +00:00
|
|
|
UDPC_Deque *receivedPackets;
|
|
|
|
|
|
|
|
UDPC_callback_connected callbackConnected;
|
|
|
|
void *callbackConnectedUserData;
|
|
|
|
UDPC_callback_disconnected callbackDisconnected;
|
|
|
|
void *callbackDisconnectedUserData;
|
|
|
|
UDPC_callback_received callbackReceived;
|
|
|
|
void *callbackReceivedUserData;
|
2019-01-26 07:22:31 +00:00
|
|
|
} UDPC_Context;
|
|
|
|
|
2019-03-06 11:31:23 +00:00
|
|
|
/// This struct should not be used outside of this library
|
2019-02-15 04:52:38 +00:00
|
|
|
typedef struct {
|
|
|
|
struct timespec tsNow;
|
|
|
|
float dt;
|
|
|
|
UDPC_Deque *removedQueue;
|
|
|
|
UDPC_Context *ctx;
|
|
|
|
} UDPC_INTERNAL_update_struct;
|
|
|
|
|
2019-03-06 11:31:23 +00:00
|
|
|
/// Creates a new UDPC_Context, for establishing a connection via UDP
|
|
|
|
/*!
|
|
|
|
* Callbacks must be set to use UDPC effectively, using the following functions:
|
|
|
|
* - UDPC_set_callback_connected()
|
|
|
|
* - UDPC_set_callback_disconnected()
|
|
|
|
* - UDPC_set_callback_received()
|
|
|
|
*
|
|
|
|
* Clients should also use UDPC_client_initiate_connection() to let UDPC know
|
|
|
|
* what server to connect to.
|
|
|
|
*
|
|
|
|
* UDPC_update() must be called periodically (ideally at 30 updates per second
|
|
|
|
* or faster), to send and receive UDP packets, and establish new connections
|
|
|
|
* when enabled. An init variant "UDPC_init_threaded_update()" is available
|
|
|
|
* which will call update periodically on a separate thread.
|
|
|
|
*
|
|
|
|
* UDPC_check_events() must also be called to call the set callbacks for
|
|
|
|
* connected, disconnected, and received events.
|
|
|
|
*
|
|
|
|
* When finished using UDPC, UDPC_destroy must be called on the context to free
|
|
|
|
* resources.
|
|
|
|
*/
|
2019-03-06 10:53:15 +00:00
|
|
|
UDPC_Context* UDPC_init(uint16_t listenPort, uint32_t listenAddr, int isClient);
|
2019-01-26 07:22:31 +00:00
|
|
|
|
2019-03-06 11:31:23 +00:00
|
|
|
/// Creates a new UDPC_Context, where a thread is created to update UDPC on its own
|
|
|
|
/*!
|
|
|
|
* If using a UDPC_Context created by this init variant, UDPC_update() must not
|
|
|
|
* be manually called, as it is called automatically by a separate thread.
|
|
|
|
*
|
|
|
|
* See the documentation for UDPC_init() for more details.
|
|
|
|
*/
|
2019-03-06 10:53:15 +00:00
|
|
|
UDPC_Context* UDPC_init_threaded_update(uint16_t listenPort, uint32_t listenAddr, int isClient);
|
2019-01-26 07:22:31 +00:00
|
|
|
|
2019-03-06 11:34:24 +00:00
|
|
|
/// This fn must be called on a UDPC_Context to free resources
|
2019-01-26 07:22:31 +00:00
|
|
|
void UDPC_destroy(UDPC_Context *ctx);
|
|
|
|
|
2019-02-18 03:31:42 +00:00
|
|
|
void UDPC_INTERNAL_destroy_conMap(void *unused, uint32_t addr, char *data);
|
2019-02-15 04:52:38 +00:00
|
|
|
|
2019-03-06 11:34:24 +00:00
|
|
|
/// Sets the callback for connected events
|
2019-02-20 06:13:57 +00:00
|
|
|
void UDPC_set_callback_connected(
|
|
|
|
UDPC_Context *ctx, UDPC_callback_connected fptr, void *userData);
|
|
|
|
|
2019-03-06 11:34:24 +00:00
|
|
|
/// Sets the callback for disconnected events
|
2019-02-20 06:13:57 +00:00
|
|
|
void UDPC_set_callback_disconnected(
|
|
|
|
UDPC_Context *ctx, UDPC_callback_disconnected fptr, void *userData);
|
|
|
|
|
2019-03-06 11:34:24 +00:00
|
|
|
/// Sets the callback for received packet events
|
2019-02-20 06:13:57 +00:00
|
|
|
void UDPC_set_callback_received(
|
|
|
|
UDPC_Context *ctx, UDPC_callback_received fptr, void *userData);
|
|
|
|
|
2019-03-06 11:34:24 +00:00
|
|
|
/// Invokes callbacks based on events that have ocurred during UDPC_update()
|
2019-02-21 06:40:30 +00:00
|
|
|
void UDPC_check_events(UDPC_Context *ctx);
|
2019-02-20 06:13:57 +00:00
|
|
|
|
2019-03-06 11:34:24 +00:00
|
|
|
/// Tells UDPC to initiate a connection to a server
|
2019-03-04 06:04:16 +00:00
|
|
|
void UDPC_client_initiate_connection(UDPC_Context *ctx, uint32_t addr, uint16_t port);
|
|
|
|
|
2019-02-20 06:13:57 +00:00
|
|
|
/*!
|
|
|
|
* \brief Queues a packet to send to a connected peer
|
|
|
|
* Note addr is expected to be in network-byte-order (big-endian).
|
|
|
|
* If isChecked is non-zero, UDPC will attempt to resend the packet if peer has
|
|
|
|
* not received it within UDPC_PACKET_TIMEOUT_SEC seconds.
|
|
|
|
* \return non-zero on success
|
|
|
|
*/
|
|
|
|
int UDPC_queue_send(
|
|
|
|
UDPC_Context *ctx, uint32_t addr, uint32_t isChecked, void *data, uint32_t size);
|
|
|
|
|
2019-02-21 06:40:30 +00:00
|
|
|
/*!
|
|
|
|
* \brief get the number of packets that can be queued to the addr
|
|
|
|
* \return number of queueable packets or 0 if connection has not been established
|
|
|
|
*/
|
|
|
|
int UDPC_get_queue_send_available(UDPC_Context *ctx, uint32_t addr);
|
|
|
|
|
2019-03-06 11:13:38 +00:00
|
|
|
/// Returns non-zero if UDPC is accepting new connections
|
|
|
|
int UDPC_get_accept_new_connections(UDPC_Context *ctx);
|
|
|
|
|
|
|
|
/// Set isAccepting to non-zero to let UDPC accept new connections
|
2019-03-06 11:31:23 +00:00
|
|
|
/*!
|
|
|
|
* Set isAccepting to zero to prevent UDPC from accepting new connections.
|
|
|
|
*/
|
2019-03-06 11:13:38 +00:00
|
|
|
void UDPC_set_accept_new_connections(UDPC_Context *ctx, int isAccepting);
|
|
|
|
|
2019-03-07 02:57:06 +00:00
|
|
|
/// Gets the currently set protocol id
|
|
|
|
uint32_t UDPC_get_protocol_id(UDPC_Context *ctx);
|
|
|
|
|
|
|
|
/// Sets the protocol id
|
|
|
|
/*!
|
|
|
|
* Note that UDPC can only connect to other UDPC instances that use the same
|
|
|
|
* protocol id.
|
|
|
|
*/
|
|
|
|
void UDPC_set_protocol_id(UDPC_Context *ctx, uint32_t id);
|
|
|
|
|
2019-03-06 11:13:38 +00:00
|
|
|
/*!
|
|
|
|
* \brief Get the currently set error code, and clear it internally
|
|
|
|
* Error codes and their meanings are defined in UDPC_Defines.h .
|
|
|
|
* Use UDPC_get_error_str() to get a string describing the error.
|
|
|
|
*/
|
2019-01-29 02:53:39 +00:00
|
|
|
uint32_t UDPC_get_error(UDPC_Context *ctx);
|
|
|
|
|
2019-03-06 11:13:38 +00:00
|
|
|
/// Returns a string describing the error code for UDPC
|
2019-01-29 02:53:39 +00:00
|
|
|
const char* UDPC_get_error_str(uint32_t error);
|
|
|
|
|
2019-02-01 10:22:04 +00:00
|
|
|
/*!
|
|
|
|
* 0 - log nothing
|
|
|
|
* 1 - log only errors
|
|
|
|
* 2 - log only errors and warnings
|
|
|
|
* 3 - log errors, warnings, and info
|
|
|
|
* 4+ - log everything
|
|
|
|
*
|
|
|
|
* By default, erros and warnings are logged.
|
|
|
|
*/
|
|
|
|
void UDPC_set_logging_type(UDPC_Context *ctx, uint32_t logType);
|
|
|
|
|
2019-01-31 03:16:01 +00:00
|
|
|
/// If threaded, this function is called automatically
|
|
|
|
void UDPC_update(UDPC_Context *ctx);
|
|
|
|
|
2019-02-18 03:31:42 +00:00
|
|
|
void UDPC_INTERNAL_update_to_rtt_si(void *userData, uint32_t addr, char *data);
|
2019-02-15 04:52:38 +00:00
|
|
|
|
2019-02-18 03:31:42 +00:00
|
|
|
void UDPC_INTERNAL_update_send(void *userData, uint32_t addr, char *data);
|
2019-02-15 04:52:38 +00:00
|
|
|
|
2019-02-18 07:32:50 +00:00
|
|
|
void UDPC_INTERNAL_update_rtt(
|
|
|
|
UDPC_Context *ctx,
|
|
|
|
UDPC_INTERNAL_ConnectionData *cd,
|
|
|
|
uint32_t rseq,
|
|
|
|
struct timespec *tsNow);
|
|
|
|
|
|
|
|
void UDPC_INTERNAL_check_pkt_timeout(
|
2019-02-19 04:28:16 +00:00
|
|
|
UDPC_Context *ctx,
|
2019-02-18 07:32:50 +00:00
|
|
|
UDPC_INTERNAL_ConnectionData *cd,
|
|
|
|
uint32_t rseq,
|
|
|
|
uint32_t ack,
|
|
|
|
struct timespec *tsNow);
|
|
|
|
|
2019-02-20 06:13:57 +00:00
|
|
|
float UDPC_INTERNAL_ts_diff(struct timespec *ts0, struct timespec *ts1);
|
2019-01-31 03:16:01 +00:00
|
|
|
|
2019-02-20 06:13:57 +00:00
|
|
|
int UDPC_INTERNAL_threadfn(void *context);
|
2019-01-26 03:52:10 +00:00
|
|
|
|
2019-01-31 10:44:32 +00:00
|
|
|
/*
|
|
|
|
* 0x1 - is ping
|
|
|
|
* 0x2 - is resending
|
|
|
|
* 0x4 - is checked received packet
|
|
|
|
*/
|
|
|
|
void UDPC_INTERNAL_prepare_pkt(
|
|
|
|
void *data,
|
2019-03-07 02:57:06 +00:00
|
|
|
uint32_t protocolID,
|
2019-01-31 10:44:32 +00:00
|
|
|
uint32_t conID,
|
|
|
|
uint32_t rseq,
|
|
|
|
uint32_t ack,
|
|
|
|
uint32_t *seqID,
|
|
|
|
int flags);
|
|
|
|
|
2019-02-01 10:22:04 +00:00
|
|
|
/*!
|
|
|
|
* 0 - error
|
|
|
|
* 1 - warning
|
|
|
|
* 2 - info
|
|
|
|
* 3 - verbose
|
|
|
|
*/
|
|
|
|
void UDPC_INTERNAL_log(UDPC_Context *ctx, uint32_t level, const char *msg, ...);
|
|
|
|
|
|
|
|
char* UDPC_INTERNAL_atostr(UDPC_Context *ctx, uint32_t addr);
|
|
|
|
|
2019-02-20 06:13:57 +00:00
|
|
|
uint32_t UDPC_INTERNAL_generate_id(UDPC_Context *ctx);
|
|
|
|
|
|
|
|
void UDPC_INTERNAL_check_ids(void *userData, uint32_t addr, char *data);
|
|
|
|
|
2019-03-06 10:53:15 +00:00
|
|
|
/*1
|
|
|
|
* \brief Converts a IPV4 string to a 32-bit unsigned integer address in big-endian
|
|
|
|
* \return 0 if string is invalid, address in big-endian format otherwise
|
|
|
|
*/
|
2019-03-04 06:05:21 +00:00
|
|
|
uint32_t UDPC_strtoa(const char *addrStr);
|
|
|
|
|
2019-01-26 03:52:10 +00:00
|
|
|
#endif
|