2019-06-06 07:02:48 +00:00
|
|
|
#ifndef UDPC_CONNECTION_H
|
|
|
|
#define UDPC_CONNECTION_H
|
|
|
|
|
|
|
|
// Determine platform macros
|
|
|
|
#define UDPC_PLATFORM_WINDOWS 1
|
|
|
|
#define UDPC_PLATFORM_MAC 2
|
|
|
|
#define UDPC_PLATFORM_LINUX 3
|
|
|
|
#define UDPC_PLATFORM_UNKNOWN 0
|
|
|
|
|
|
|
|
#if defined _WIN32
|
2019-06-06 07:42:07 +00:00
|
|
|
#define UDPC_PLATFORM UDPC_PLATFORM_WINDOWS
|
2019-06-06 07:02:48 +00:00
|
|
|
#elif defined __APPLE__
|
2019-06-06 07:42:07 +00:00
|
|
|
#define UDPC_PLATFORM UDPC_PLATFORM_MAC
|
2019-06-06 07:02:48 +00:00
|
|
|
#elif defined __linux__
|
2019-06-06 07:42:07 +00:00
|
|
|
#define UDPC_PLATFORM UDPC_PLATFORM_LINUX
|
2019-06-06 07:02:48 +00:00
|
|
|
#else
|
2019-06-06 07:42:07 +00:00
|
|
|
#define UDPC_PLATFORM UDPC_PLATFORM_UNKNOWN
|
2019-06-06 07:02:48 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// OS-based networking macros
|
|
|
|
#if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
|
2019-06-06 07:42:07 +00:00
|
|
|
#include <winsock2.h>
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-06-06 07:42:07 +00:00
|
|
|
#define CleanupSocket(x) closesocket(x)
|
2019-06-06 07:02:48 +00:00
|
|
|
#elif UDPC_PLATFORM == UDPC_PLATFORM_MAC || UDPC_PLATFORM == UDPC_PLATFORM_LINUX
|
2019-06-06 07:42:07 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <unistd.h>
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-06-06 07:42:07 +00:00
|
|
|
#define CleanupSocket(x) close(x)
|
2019-06-06 07:02:48 +00:00
|
|
|
#else
|
2019-06-06 07:42:07 +00:00
|
|
|
#define CleanupSocket(x) ((void)0)
|
2019-06-06 07:02:48 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// other defines
|
|
|
|
#define UDPC_PACKET_MAX_SIZE 8192
|
|
|
|
#define UDPC_DEFAULT_PROTOCOL_ID 1357924680
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
2019-06-06 07:42:07 +00:00
|
|
|
#include <cstdint>
|
2019-06-06 07:02:48 +00:00
|
|
|
extern "C" {
|
|
|
|
#else
|
2019-06-06 07:42:07 +00:00
|
|
|
#include <stdint.h>
|
2019-06-06 07:02:48 +00:00
|
|
|
#endif
|
|
|
|
|
2019-08-30 03:03:26 +00:00
|
|
|
// Opaque struct handle to Context
|
|
|
|
struct UDPC_Context;
|
2019-08-30 03:11:07 +00:00
|
|
|
typedef struct UDPC_Context *UDPC_HContext;
|
2019-08-30 03:03:26 +00:00
|
|
|
|
2019-06-06 07:42:07 +00:00
|
|
|
typedef enum { SILENT, ERROR, WARNING, VERBOSE, INFO } UDPC_LoggingType;
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-09-03 03:06:46 +00:00
|
|
|
typedef struct {
|
|
|
|
uint32_t addr;
|
|
|
|
uint16_t port;
|
|
|
|
} UDPC_ConnectionId;
|
|
|
|
|
2019-06-06 07:02:48 +00:00
|
|
|
typedef struct {
|
2019-08-22 11:16:07 +00:00
|
|
|
// id is stored at offset 8, size 4 (uint32_t) even for "empty" PktInfos
|
2019-06-06 07:02:48 +00:00
|
|
|
char data[UDPC_PACKET_MAX_SIZE];
|
2019-07-25 11:51:08 +00:00
|
|
|
/*
|
|
|
|
* 0x1 - connect
|
|
|
|
* 0x2 - ping
|
|
|
|
* 0x4 - no_rec_chk
|
|
|
|
* 0x8 - resending
|
|
|
|
*/
|
|
|
|
uint32_t flags;
|
2019-06-06 07:02:48 +00:00
|
|
|
uint16_t dataSize; // zero if invalid
|
2019-09-03 03:06:46 +00:00
|
|
|
UDPC_ConnectionId sender;
|
|
|
|
UDPC_ConnectionId receiver;
|
2019-07-25 11:51:08 +00:00
|
|
|
} UDPC_PacketInfo;
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-09-03 03:06:46 +00:00
|
|
|
UDPC_ConnectionId UDPC_create_id(uint32_t addr, uint16_t port);
|
|
|
|
|
2019-08-27 08:00:29 +00:00
|
|
|
/// listenPort must be in native byte order, listenAddr must be in network byte order (big-endian)
|
2019-08-30 03:03:26 +00:00
|
|
|
UDPC_HContext UDPC_init(uint16_t listenPort, uint32_t listenAddr, int isClient);
|
2019-08-27 08:00:29 +00:00
|
|
|
/// listenPort must be in native byte order, listenAddr must be in network byte order (big-endian)
|
2019-08-30 03:03:26 +00:00
|
|
|
UDPC_HContext UDPC_init_threaded_update(uint16_t listenPort, uint32_t listenAddr,
|
2019-06-06 07:42:07 +00:00
|
|
|
int isClient);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-08-30 03:03:26 +00:00
|
|
|
void UDPC_destroy(UDPC_HContext ctx);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-08-30 03:03:26 +00:00
|
|
|
void UDPC_update(UDPC_HContext ctx);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-08-29 02:07:24 +00:00
|
|
|
/// addr must be in network byte order (big-endian), port must be in native byte order
|
2019-08-30 03:03:26 +00:00
|
|
|
void UDPC_client_initiate_connection(UDPC_HContext ctx, uint32_t addr, uint16_t port);
|
2019-08-29 02:07:24 +00:00
|
|
|
|
|
|
|
/// addr must be in network byte order (big-endian), port must be in native byte order
|
2019-08-30 03:03:26 +00:00
|
|
|
int UDPC_get_queue_send_available(UDPC_HContext ctx, uint32_t addr, uint16_t port);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-08-29 02:07:24 +00:00
|
|
|
/// destAddr must be in network byte order (big-endian), destPort must be in native byte order
|
2019-08-30 03:03:26 +00:00
|
|
|
void UDPC_queue_send(UDPC_HContext ctx, uint32_t destAddr, uint16_t destPort,
|
2019-06-06 07:42:07 +00:00
|
|
|
uint32_t isChecked, void *data, uint32_t size);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-08-30 03:03:26 +00:00
|
|
|
int UDPC_set_accept_new_connections(UDPC_HContext ctx, int isAccepting);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-08-27 08:00:29 +00:00
|
|
|
/// addr must be in network byte order (big-endian), port must be in native byte order
|
2019-08-30 03:03:26 +00:00
|
|
|
int UDPC_drop_connection(UDPC_HContext ctx, uint32_t addr, uint16_t port);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-08-28 07:38:14 +00:00
|
|
|
/// addr must be in network byte order, drops all connections to specified addr
|
2019-08-30 03:03:26 +00:00
|
|
|
int UDPC_drop_connection_addr(UDPC_HContext ctx, uint32_t addr);
|
2019-08-28 07:38:14 +00:00
|
|
|
|
2019-08-30 03:03:26 +00:00
|
|
|
uint32_t UDPC_set_protocol_id(UDPC_HContext ctx, uint32_t id);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-08-30 03:03:26 +00:00
|
|
|
UDPC_LoggingType set_logging_type(UDPC_HContext ctx, UDPC_LoggingType loggingType);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-08-30 03:03:26 +00:00
|
|
|
UDPC_PacketInfo UDPC_get_received(UDPC_HContext ctx);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-08-27 08:00:29 +00:00
|
|
|
/// addr must be in network byte order
|
2019-08-30 03:03:26 +00:00
|
|
|
const char *UDPC_atostr(UDPC_HContext ctx, uint32_t addr);
|
2019-06-06 07:02:48 +00:00
|
|
|
|
2019-08-27 08:00:29 +00:00
|
|
|
/// returns a 4 byte unsigned integer address in network byte order
|
2019-06-06 07:02:48 +00:00
|
|
|
uint32_t UDPC_strtoa(const char *addrStr);
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|