1
0
mirror of https://github.com/CPunch/Laika.git synced 2024-09-28 22:48:47 +00:00

Renamed 'CERROR' & 'CWARN' to LAIKA_ERROR & LAIKA_WARN

This commit is contained in:
CPunch 2022-01-24 09:51:29 -06:00
parent 8133a8d3cb
commit c9fdeba4c5
8 changed files with 30 additions and 26 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
build
.vscode

View File

@ -2,6 +2,7 @@
#define LAIKA_CNC_H #define LAIKA_CNC_H
#include "laika.h" #include "laika.h"
#include "lpacket.h"
#include "lsocket.h" #include "lsocket.h"
#include "lpolllist.h" #include "lpolllist.h"

View File

@ -1 +1,2 @@
#include "cnc.h" #include "cnc.h"

View File

@ -15,21 +15,21 @@
/* if eLaika_errIndx is >= 0, we have a safe spot to jump too if an error is thrown */ /* if eLaika_errIndx is >= 0, we have a safe spot to jump too if an error is thrown */
#define LAIKA_ISPROTECTED (eLaika_errIndx >= 0) #define LAIKA_ISPROTECTED (eLaika_errIndx >= 0)
/* CERROR(printf args): /* LAIKA_ERROR(printf args):
if called after a LAIKA_TRY block will jump to the previous LAIKA_CATCH/LAIKA_TRYEND block, if called after a LAIKA_TRY block will jump to the previous LAIKA_CATCH/LAIKA_TRYEND block,
otherwise program is exit()'d. if DEBUG is defined printf is called with passed args, else otherwise program is exit()'d. if DEBUG is defined printf is called with passed args, else
arguments are ignored. arguments are ignored.
*/ */
#ifndef DEBUG #ifndef DEBUG
#define CERROR(...) { \ #define LAIKA_ERROR(...) { \
if (LAIKA_ISPROTECTED) \ if (LAIKA_ISPROTECTED) \
longjmp(eLaika_errStack[eLaika_errIndx], 1); \ longjmp(eLaika_errStack[eLaika_errIndx], 1); \
else \ else \
exit(1); \ exit(1); \
} }
#define CWARN(...) #define LAIKA_WARN(...)
#else #else
#define CERROR(...) { \ #define LAIKA_ERROR(...) { \
printf("[ERROR] : " __VA_ARGS__); \ printf("[ERROR] : " __VA_ARGS__); \
if (LAIKA_ISPROTECTED) \ if (LAIKA_ISPROTECTED) \
longjmp(eLaika_errStack[eLaika_errIndx], 1); \ longjmp(eLaika_errStack[eLaika_errIndx], 1); \
@ -37,7 +37,7 @@
exit(1); \ exit(1); \
} }
#define CWARN(...) \ #define LAIKA_WARN(...) \
printf("[WARN] : " __VA_ARGS__); printf("[WARN] : " __VA_ARGS__);
#endif #endif

View File

@ -12,7 +12,7 @@ void *laikaM_realloc(void *buf, size_t sz) {
/* if NULL is passed, realloc() acts like malloc() */ /* if NULL is passed, realloc() acts like malloc() */
if ((newBuf = realloc(buf, sz)) == NULL) if ((newBuf = realloc(buf, sz)) == NULL)
CERROR("failed to allocate memory!"); LAIKA_ERROR("failed to allocate memory!");
return newBuf; return newBuf;
} }

View File

@ -35,7 +35,7 @@ bool laikaS_handlePeerIn(struct sLaika_peer *peer) {
/* sanity check packet ID */ /* sanity check packet ID */
if (peer->pktID >= LAIKAPKT_MAXNONE) if (peer->pktID >= LAIKAPKT_MAXNONE)
CERROR("received evil pktID!") LAIKA_ERROR("received evil pktID!")
peer->pktSize = peer->pktSizeTable[peer->pktID]; peer->pktSize = peer->pktSizeTable[peer->pktID];
break; break;

View File

@ -32,7 +32,7 @@ void laikaP_initPList(struct sLaika_pollList *pList) {
/* setup our epoll */ /* setup our epoll */
memset(&pList->ev, 0, sizeof(struct epoll_event)); memset(&pList->ev, 0, sizeof(struct epoll_event));
if ((pList->epollfd = epoll_create(POLLSTARTCAP)) == -1) if ((pList->epollfd = epoll_create(POLLSTARTCAP)) == -1)
CERROR("epoll_create() failed!"); LAIKA_ERROR("epoll_create() failed!");
#else #else
pList->fds = NULL; /* laikaP_addSock will allocate the buffer */ pList->fds = NULL; /* laikaP_addSock will allocate the buffer */
@ -62,7 +62,7 @@ void laikaP_addSock(struct sLaika_pollList *pList, struct sLaika_socket *sock) {
pList->ev.data.ptr = (void*)sock; pList->ev.data.ptr = (void*)sock;
if (epoll_ctl(pList->epollfd, EPOLL_CTL_ADD, sock->sock, &pList->ev) == -1) if (epoll_ctl(pList->epollfd, EPOLL_CTL_ADD, sock->sock, &pList->ev) == -1)
CERROR("epoll_ctl [ADD] failed"); LAIKA_ERROR("epoll_ctl [ADD] failed");
#else #else
/* allocate space in array & add PollFD */ /* allocate space in array & add PollFD */
@ -79,7 +79,7 @@ void laikaP_rmvSock(struct sLaika_pollList *pList, struct sLaika_socket *sock) {
/* epoll_event* isn't needed with EPOLL_CTL_DEL, however we still need to pass a NON-NULL pointer. [see: https://man7.org/linux/man-pages/man2/epoll_ctl.2.html#BUGS] */ /* epoll_event* isn't needed with EPOLL_CTL_DEL, however we still need to pass a NON-NULL pointer. [see: https://man7.org/linux/man-pages/man2/epoll_ctl.2.html#BUGS] */
if (epoll_ctl(pList->epollfd, EPOLL_CTL_DEL, sock->sock, &pList->ev) == -1) { if (epoll_ctl(pList->epollfd, EPOLL_CTL_DEL, sock->sock, &pList->ev) == -1) {
/* non-fatal error, socket probably just didn't exist, so ignore it. */ /* non-fatal error, socket probably just didn't exist, so ignore it. */
CWARN("epoll_ctl [DEL] failed"); LAIKA_WARN("epoll_ctl [DEL] failed");
} }
#else #else
int i; int i;
@ -101,7 +101,7 @@ void laikaP_addPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock
pList->ev.data.ptr = (void*)sock; pList->ev.data.ptr = (void*)sock;
if (epoll_ctl(pList->epollfd, EPOLL_CTL_MOD, sock->sock, &pList->ev) == -1) { if (epoll_ctl(pList->epollfd, EPOLL_CTL_MOD, sock->sock, &pList->ev) == -1) {
/* non-fatal error, socket probably just didn't exist, so ignore it. */ /* non-fatal error, socket probably just didn't exist, so ignore it. */
CWARN("epoll_ctl [MOD] failed"); LAIKA_WARN("epoll_ctl [MOD] failed");
} }
#else #else
int i; int i;
@ -122,7 +122,7 @@ void laikaP_rmvPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock
pList->ev.data.ptr = (void*)sock; pList->ev.data.ptr = (void*)sock;
if (epoll_ctl(pList->epollfd, EPOLL_CTL_MOD, sock->sock, &pList->ev) == -1) { if (epoll_ctl(pList->epollfd, EPOLL_CTL_MOD, sock->sock, &pList->ev) == -1) {
/* non-fatal error, socket probably just didn't exist, so ignore it. */ /* non-fatal error, socket probably just didn't exist, so ignore it. */
CWARN("epoll_ctl [MOD] failed"); LAIKA_WARN("epoll_ctl [MOD] failed");
} }
#else #else
int i; int i;
@ -148,7 +148,7 @@ struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout,
nEvents = epoll_wait(pList->epollfd, pList->ep_events, MAX_EPOLL_EVENTS, timeout); nEvents = epoll_wait(pList->epollfd, pList->ep_events, MAX_EPOLL_EVENTS, timeout);
if (SOCKETERROR(nEvents)) if (SOCKETERROR(nEvents))
CERROR("epoll_wait() failed!"); LAIKA_ERROR("epoll_wait() failed!");
for (i = 0; i < nEvents; i++) { for (i = 0; i < nEvents; i++) {
/* add event to revent array */ /* add event to revent array */
@ -163,7 +163,7 @@ struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout,
nEvents = poll(pList->fds, pList->fdCount, timeout); /* poll returns -1 for error, or the number of events */ nEvents = poll(pList->fds, pList->fdCount, timeout); /* poll returns -1 for error, or the number of events */
if (SOCKETERROR(nEvents)) if (SOCKETERROR(nEvents))
CERROR("poll() failed!"); LAIKA_ERROR("poll() failed!");
/* walk through the returned poll fds, if they have an event, add it to our revents array */ /* walk through the returned poll fds, if they have an event, add it to our revents array */
for (i = 0; i < pList->fdCount && nEvents > 0; i++) { for (i = 0; i < pList->fdCount && nEvents > 0; i++) {

View File

@ -13,7 +13,7 @@ void laikaS_init(void) {
WSADATA wsaData; WSADATA wsaData;
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
CERROR("WSAStartup failed!") LAIKA_ERROR("WSAStartup failed!")
#endif #endif
} }
@ -68,7 +68,7 @@ void laikaS_connect(struct sLaika_socket *sock, char *ip, char *port) {
struct addrinfo res, *result, *curr; struct addrinfo res, *result, *curr;
if (!SOCKETINVALID(sock->sock)) if (!SOCKETINVALID(sock->sock))
CERROR("socket already setup!"); LAIKA_ERROR("socket already setup!");
/* zero out our address info and setup the type */ /* zero out our address info and setup the type */
memset(&res, 0, sizeof(struct addrinfo)); memset(&res, 0, sizeof(struct addrinfo));
@ -77,7 +77,7 @@ void laikaS_connect(struct sLaika_socket *sock, char *ip, char *port) {
/* grab the address info */ /* grab the address info */
if (getaddrinfo(ip, port, &res, &result) != 0) if (getaddrinfo(ip, port, &res, &result) != 0)
CERROR("getaddrinfo() failed!"); LAIKA_ERROR("getaddrinfo() failed!");
/* getaddrinfo returns a list of possible addresses, step through them and try them until we find a valid address */ /* getaddrinfo returns a list of possible addresses, step through them and try them until we find a valid address */
for (curr = result; curr != NULL; curr = curr->ai_next) { for (curr = result; curr != NULL; curr = curr->ai_next) {
@ -97,7 +97,7 @@ void laikaS_connect(struct sLaika_socket *sock, char *ip, char *port) {
/* if we reached the end of the linked list, we failed looking up the addr */ /* if we reached the end of the linked list, we failed looking up the addr */
if (curr == NULL) if (curr == NULL)
CERROR("couldn't connect a valid address handle to socket!"); LAIKA_ERROR("couldn't connect a valid address handle to socket!");
} }
void laikaS_bind(struct sLaika_socket *sock, uint16_t port) { void laikaS_bind(struct sLaika_socket *sock, uint16_t port) {
@ -105,12 +105,12 @@ void laikaS_bind(struct sLaika_socket *sock, uint16_t port) {
struct sockaddr_in address; struct sockaddr_in address;
if (!SOCKETINVALID(sock)) if (!SOCKETINVALID(sock))
CERROR("socket already setup!") LAIKA_ERROR("socket already setup!")
/* open our socket */ /* open our socket */
sock->sock = socket(AF_INET, SOCK_STREAM, 0); sock->sock = socket(AF_INET, SOCK_STREAM, 0);
if (SOCKETINVALID(sock)) if (SOCKETINVALID(sock))
CERROR("socket() failed!"); LAIKA_ERROR("socket() failed!");
/* attach socket to the port */ /* attach socket to the port */
int opt = 1; int opt = 1;
@ -119,7 +119,7 @@ void laikaS_bind(struct sLaika_socket *sock, uint16_t port) {
#else #else
if (setsockopt(sock->sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)) != 0) if (setsockopt(sock->sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)) != 0)
#endif #endif
CERROR("setsockopt() failed!"); LAIKA_ERROR("setsockopt() failed!");
address.sin_family = AF_INET; address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY; address.sin_addr.s_addr = INADDR_ANY;
@ -129,10 +129,10 @@ void laikaS_bind(struct sLaika_socket *sock, uint16_t port) {
/* bind to the port */ /* bind to the port */
if (SOCKETERROR(bind(sock->sock, (struct sockaddr *)&address, addressSize))) if (SOCKETERROR(bind(sock->sock, (struct sockaddr *)&address, addressSize)))
CERROR("bind() failed!"); LAIKA_ERROR("bind() failed!");
if (SOCKETERROR(listen(sock->sock, SOMAXCONN))) if (SOCKETERROR(listen(sock->sock, SOMAXCONN)))
CERROR("listen() failed!"); LAIKA_ERROR("listen() failed!");
} }
void laikaS_acceptFrom(struct sLaika_socket *sock, struct sLaika_socket *from) { void laikaS_acceptFrom(struct sLaika_socket *sock, struct sLaika_socket *from) {
@ -141,7 +141,7 @@ void laikaS_acceptFrom(struct sLaika_socket *sock, struct sLaika_socket *from) {
sock = accept(from->sock, &address, &addressSize); sock = accept(from->sock, &address, &addressSize);
if (SOCKETINVALID(sock)) if (SOCKETINVALID(sock))
CERROR("accept() failed!") LAIKA_ERROR("accept() failed!")
} }
bool laikaS_setNonBlock(struct sLaika_socket *sock) { bool laikaS_setNonBlock(struct sLaika_socket *sock) {
@ -151,7 +151,7 @@ bool laikaS_setNonBlock(struct sLaika_socket *sock) {
#else #else
if (fcntl(sock->sock, F_SETFL, (fcntl(sock->sock, F_GETFL, 0) | O_NONBLOCK)) != 0) { if (fcntl(sock->sock, F_SETFL, (fcntl(sock->sock, F_GETFL, 0) | O_NONBLOCK)) != 0) {
#endif #endif
CWARN("fcntl failed on new connection"); LAIKA_WARN("fcntl failed on new connection");
laikaS_kill(sock); laikaS_kill(sock);
return false; return false;
} }