Invoke ALEFix

This commit is contained in:
Stephen Seo 2019-06-06 16:42:07 +09:00
parent 2aa4600c57
commit 28d832a349
3 changed files with 69 additions and 86 deletions

View file

@ -3,9 +3,9 @@
#define UDPC_CONTEXT_IDENTIFIER 0x902F4DB3 #define UDPC_CONTEXT_IDENTIFIER 0x902F4DB3
#include <cstdint>
#include <bitset>
#include <atomic> #include <atomic>
#include <bitset>
#include <cstdint>
#include "UDPConnection.h" #include "UDPConnection.h"

View file

@ -1,18 +1,16 @@
#include "UDPConnection.h"
#include "UDPC_Defines.hpp" #include "UDPC_Defines.hpp"
#include "UDPConnection.h"
UDPC::Context::Context(bool isThreaded) : UDPC::Context::Context(bool isThreaded)
_contextIdentifier(UDPC_CONTEXT_IDENTIFIER), : _contextIdentifier(UDPC_CONTEXT_IDENTIFIER), flags(),
flags(), isAcceptNewConnections(true), protocolID(UDPC_DEFAULT_PROTOCOL_ID),
isAcceptNewConnections(true),
protocolID(UDPC_DEFAULT_PROTOCOL_ID),
#ifndef NDEBUG #ifndef NDEBUG
loggingType(INFO) loggingType(INFO)
#else #else
loggingType(WARNING) loggingType(WARNING)
#endif #endif
{ {
if(isThreaded) { if (isThreaded) {
flags.set(0); flags.set(0);
} else { } else {
flags.reset(0); flags.reset(0);
@ -20,24 +18,25 @@ loggingType(WARNING)
} }
bool UDPC::VerifyContext(void *ctx) { bool UDPC::VerifyContext(void *ctx) {
if(ctx == nullptr) { if (ctx == nullptr) {
return false; return false;
} }
UDPC::Context* c = (UDPC::Context*) ctx; UDPC::Context *c = (UDPC::Context *)ctx;
if(c->_contextIdentifier == UDPC_CONTEXT_IDENTIFIER) { if (c->_contextIdentifier == UDPC_CONTEXT_IDENTIFIER) {
return true; return true;
} else { } else {
return false; return false;
} }
} }
void* UDPC_init(uint16_t listenPort, uint32_t listenAddr, int isClient) { void *UDPC_init(uint16_t listenPort, uint32_t listenAddr, int isClient) {
UDPC::Context *ctx = new UDPC::Context(false); UDPC::Context *ctx = new UDPC::Context(false);
return ctx; return ctx;
} }
void* UDPC_init_threaded_update(uint16_t listenPort, uint32_t listenAddr, int isClient) { void *UDPC_init_threaded_update(uint16_t listenPort, uint32_t listenAddr,
int isClient) {
UDPC::Context *ctx = new UDPC::Context(true); UDPC::Context *ctx = new UDPC::Context(true);
return ctx; return ctx;
@ -45,16 +44,16 @@ void* UDPC_init_threaded_update(uint16_t listenPort, uint32_t listenAddr, int is
void UDPC_destroy(void *ctx) { void UDPC_destroy(void *ctx) {
if (UDPC::VerifyContext(ctx)) { if (UDPC::VerifyContext(ctx)) {
delete (UDPC::Context*)ctx; delete (UDPC::Context *)ctx;
} }
} }
void UDPC_update(void *ctx) { void UDPC_update(void *ctx) {
if(!UDPC::VerifyContext(ctx)) { if (!UDPC::VerifyContext(ctx)) {
return; return;
} }
UDPC::Context *c = (UDPC::Context*)ctx; UDPC::Context *c = (UDPC::Context *)ctx;
if(c->flags.test(0)) { if (c->flags.test(0)) {
// is threaded, update should not be called // is threaded, update should not be called
return; return;
} }
@ -63,86 +62,83 @@ void UDPC_update(void *ctx) {
} }
int UDPC_get_queue_send_available(void *ctx, uint32_t addr) { int UDPC_get_queue_send_available(void *ctx, uint32_t addr) {
if(!UDPC::VerifyContext(ctx)) { if (!UDPC::VerifyContext(ctx)) {
return 0; return 0;
} }
UDPC::Context *c = (UDPC::Context*)ctx; UDPC::Context *c = (UDPC::Context *)ctx;
// TODO impl // TODO impl
return 0; return 0;
} }
void UDPC_queue_send(void *ctx, uint32_t destAddr, uint16_t destPort, uint32_t isChecked, void *data, uint32_t size) { void UDPC_queue_send(void *ctx, uint32_t destAddr, uint16_t destPort,
if(!UDPC::VerifyContext(ctx)) { uint32_t isChecked, void *data, uint32_t size) {
if (!UDPC::VerifyContext(ctx)) {
return; return;
} }
UDPC::Context *c = (UDPC::Context*)ctx; UDPC::Context *c = (UDPC::Context *)ctx;
// TODO impl // TODO impl
} }
int UDPC_set_accept_new_connections(void *ctx, int isAccepting) { int UDPC_set_accept_new_connections(void *ctx, int isAccepting) {
if(!UDPC::VerifyContext(ctx)) { if (!UDPC::VerifyContext(ctx)) {
return 0; return 0;
} }
UDPC::Context *c = (UDPC::Context*)ctx; UDPC::Context *c = (UDPC::Context *)ctx;
return c->isAcceptNewConnections.exchange(isAccepting == 0 ? false : true); return c->isAcceptNewConnections.exchange(isAccepting == 0 ? false : true);
} }
int UDPC_drop_connection(void *ctx, uint32_t addr, uint16_t port) { int UDPC_drop_connection(void *ctx, uint32_t addr, uint16_t port) {
if(!UDPC::VerifyContext(ctx)) { if (!UDPC::VerifyContext(ctx)) {
return 0; return 0;
} }
UDPC::Context *c = (UDPC::Context*)ctx; UDPC::Context *c = (UDPC::Context *)ctx;
// TODO impl // TODO impl
return 0; return 0;
} }
uint32_t UDPC_set_protocol_id(void *ctx, uint32_t id) { uint32_t UDPC_set_protocol_id(void *ctx, uint32_t id) {
if(!UDPC::VerifyContext(ctx)) { if (!UDPC::VerifyContext(ctx)) {
return 0; return 0;
} }
UDPC::Context *c = (UDPC::Context*)ctx; UDPC::Context *c = (UDPC::Context *)ctx;
return c->protocolID.exchange(id); return c->protocolID.exchange(id);
} }
UDPC_LoggingType set_logging_type(void *ctx, UDPC_LoggingType loggingType) { UDPC_LoggingType set_logging_type(void *ctx, UDPC_LoggingType loggingType) {
if(!UDPC::VerifyContext(ctx)) { if (!UDPC::VerifyContext(ctx)) {
return static_cast<UDPC_LoggingType>(0); return static_cast<UDPC_LoggingType>(0);
} }
UDPC::Context *c = (UDPC::Context*)ctx; UDPC::Context *c = (UDPC::Context *)ctx;
return static_cast<UDPC_LoggingType>(c->loggingType.exchange(loggingType)); return static_cast<UDPC_LoggingType>(c->loggingType.exchange(loggingType));
} }
PacketInfo UDPC_get_received(void *ctx) { PacketInfo UDPC_get_received(void *ctx) {
if(!UDPC::VerifyContext(ctx)) { if (!UDPC::VerifyContext(ctx)) {
return PacketInfo{{0}, 0, 0, 0, 0, 0}; return PacketInfo{{0}, 0, 0, 0, 0, 0};
} }
UDPC::Context *c = (UDPC::Context*)ctx; UDPC::Context *c = (UDPC::Context *)ctx;
// TODO impl // TODO impl
return PacketInfo{{0}, 0, 0, 0, 0, 0}; return PacketInfo{{0}, 0, 0, 0, 0, 0};
} }
const char* UDPC_atostr(void *ctx, uint32_t addr) { const char *UDPC_atostr(void *ctx, uint32_t addr) {
if(!UDPC::VerifyContext(ctx)) { if (!UDPC::VerifyContext(ctx)) {
return nullptr; return nullptr;
} }
UDPC::Context *c = (UDPC::Context*)ctx; UDPC::Context *c = (UDPC::Context *)ctx;
int index = 0; int index = 0;
for(int x = 0; x < 4; ++x) for (int x = 0; x < 4; ++x) {
{
unsigned char temp = (addr >> (x * 8)) & 0xFF; unsigned char temp = (addr >> (x * 8)) & 0xFF;
if(temp >= 100) if (temp >= 100) {
{
c->atostrBuf[index++] = '0' + temp / 100; c->atostrBuf[index++] = '0' + temp / 100;
} }
if(temp >= 10) if (temp >= 10) {
{
c->atostrBuf[index++] = '0' + ((temp / 10) % 10); c->atostrBuf[index++] = '0' + ((temp / 10) % 10);
} }
c->atostrBuf[index++] = '0' + temp % 10; c->atostrBuf[index++] = '0' + temp % 10;
if(x < 3) if (x < 3) {
{
c->atostrBuf[index++] = '.'; c->atostrBuf[index++] = '.';
} }
} }
@ -155,32 +151,23 @@ uint32_t UDPC_strtoa(const char *addrStr) {
uint32_t addr = 0; uint32_t addr = 0;
uint32_t temp = 0; uint32_t temp = 0;
uint32_t index = 0; uint32_t index = 0;
while(*addrStr != 0) while (*addrStr != 0) {
{ if (*addrStr >= '0' && *addrStr <= '9') {
if(*addrStr >= '0' && *addrStr <= '9')
{
temp *= 10; temp *= 10;
temp += *addrStr - '0'; temp += *addrStr - '0';
} } else if (*addrStr == '.' && temp <= 0xFF && index < 3) {
else if(*addrStr == '.' && temp <= 0xFF && index < 3)
{
addr |= (temp << (8 * index++)); addr |= (temp << (8 * index++));
temp = 0; temp = 0;
} } else {
else
{
return 0; return 0;
} }
++addrStr; ++addrStr;
} }
if(index == 3 && temp <= 0xFF) if (index == 3 && temp <= 0xFF) {
{
addr |= temp << 24; addr |= temp << 24;
return addr; return addr;
} } else {
else
{
return 0; return 0;
} }
} }

View file

@ -8,29 +8,29 @@
#define UDPC_PLATFORM_UNKNOWN 0 #define UDPC_PLATFORM_UNKNOWN 0
#if defined _WIN32 #if defined _WIN32
#define UDPC_PLATFORM UDPC_PLATFORM_WINDOWS #define UDPC_PLATFORM UDPC_PLATFORM_WINDOWS
#elif defined __APPLE__ #elif defined __APPLE__
#define UDPC_PLATFORM UDPC_PLATFORM_MAC #define UDPC_PLATFORM UDPC_PLATFORM_MAC
#elif defined __linux__ #elif defined __linux__
#define UDPC_PLATFORM UDPC_PLATFORM_LINUX #define UDPC_PLATFORM UDPC_PLATFORM_LINUX
#else #else
#define UDPC_PLATFORM UDPC_PLATFORM_UNKNOWN #define UDPC_PLATFORM UDPC_PLATFORM_UNKNOWN
#endif #endif
// OS-based networking macros // OS-based networking macros
#if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS #if UDPC_PLATFORM == UDPC_PLATFORM_WINDOWS
#include <winsock2.h> #include <winsock2.h>
#define CleanupSocket(x) closesocket(x) #define CleanupSocket(x) closesocket(x)
#elif UDPC_PLATFORM == UDPC_PLATFORM_MAC || UDPC_PLATFORM == UDPC_PLATFORM_LINUX #elif UDPC_PLATFORM == UDPC_PLATFORM_MAC || UDPC_PLATFORM == UDPC_PLATFORM_LINUX
#include <sys/socket.h> #include <fcntl.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <fcntl.h> #include <sys/socket.h>
#include <unistd.h> #include <unistd.h>
#define CleanupSocket(x) close(x) #define CleanupSocket(x) close(x)
#else #else
#define CleanupSocket(x) ((void)0) #define CleanupSocket(x) ((void)0)
#endif #endif
// other defines // other defines
@ -38,19 +38,13 @@
#define UDPC_DEFAULT_PROTOCOL_ID 1357924680 #define UDPC_DEFAULT_PROTOCOL_ID 1357924680
#ifdef __cplusplus #ifdef __cplusplus
# include <cstdint> #include <cstdint>
extern "C" { extern "C" {
#else #else
# include <stdint.h> #include <stdint.h>
#endif #endif
typedef enum { typedef enum { SILENT, ERROR, WARNING, VERBOSE, INFO } UDPC_LoggingType;
SILENT,
ERROR,
WARNING,
VERBOSE,
INFO
} UDPC_LoggingType;
typedef struct { typedef struct {
char data[UDPC_PACKET_MAX_SIZE]; char data[UDPC_PACKET_MAX_SIZE];
@ -61,8 +55,9 @@ typedef struct {
uint16_t receiverPort; uint16_t receiverPort;
} PacketInfo; } PacketInfo;
void* UDPC_init(uint16_t listenPort, uint32_t listenAddr, int isClient); void *UDPC_init(uint16_t listenPort, uint32_t listenAddr, int isClient);
void* UDPC_init_threaded_update(uint16_t listenPort, uint32_t listenAddr, int isClient); void *UDPC_init_threaded_update(uint16_t listenPort, uint32_t listenAddr,
int isClient);
void UDPC_destroy(void *ctx); void UDPC_destroy(void *ctx);
@ -70,7 +65,8 @@ void UDPC_update(void *ctx);
int UDPC_get_queue_send_available(void *ctx, uint32_t addr); int UDPC_get_queue_send_available(void *ctx, uint32_t addr);
void UDPC_queue_send(void *ctx, uint32_t destAddr, uint16_t destPort, uint32_t isChecked, void *data, uint32_t size); void UDPC_queue_send(void *ctx, uint32_t destAddr, uint16_t destPort,
uint32_t isChecked, void *data, uint32_t size);
int UDPC_set_accept_new_connections(void *ctx, int isAccepting); int UDPC_set_accept_new_connections(void *ctx, int isAccepting);
@ -82,7 +78,7 @@ UDPC_LoggingType set_logging_type(void *ctx, UDPC_LoggingType loggingType);
PacketInfo UDPC_get_received(void *ctx); PacketInfo UDPC_get_received(void *ctx);
const char* UDPC_atostr(void *ctx, uint32_t addr); const char *UDPC_atostr(void *ctx, uint32_t addr);
uint32_t UDPC_strtoa(const char *addrStr); uint32_t UDPC_strtoa(const char *addrStr);