1
0
mirror of https://github.com/CPunch/Laika.git synced 2025-10-07 08:30:06 +00:00

First actual runnable version

- many warnings & bug fixes
- added bot/ source
This commit is contained in:
2022-01-24 21:46:29 -06:00
parent 0dee6fe3fc
commit 1bccc78117
17 changed files with 196 additions and 41 deletions

View File

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

View File

@@ -2,7 +2,7 @@
#include "lmem.h"
#include "lpeer.h"
struct sLaika_peer *laikaS_newPeer(void (*pktHandler)(struct sLaika_peer *peer, LAIKAPKT_ID id, void *uData), size_t *pktSizeTable, struct sLaika_pollList *pList, void *uData) {
struct sLaika_peer *laikaS_newPeer(void (*pktHandler)(struct sLaika_peer *peer, uint8_t id, void *uData), size_t *pktSizeTable, struct sLaika_pollList *pList, void *uData) {
struct sLaika_peer *peer = laikaM_malloc(sizeof(struct sLaika_peer));
laikaS_initSocket(&peer->sock);
@@ -35,7 +35,7 @@ bool laikaS_handlePeerIn(struct sLaika_peer *peer) {
/* sanity check packet ID */
if (peer->pktID >= LAIKAPKT_MAXNONE)
LAIKA_ERROR("received evil pktID!")
LAIKA_ERROR("received evil pktID!\n")
peer->pktSize = peer->pktSizeTable[peer->pktID];
break;
@@ -67,7 +67,7 @@ bool laikaS_handlePeerOut(struct sLaika_peer *peer) {
int sent;
if (peer->sock.outCount == 0) /* sanity check */
return;
return true;
switch (laikaS_rawSend(&peer->sock, peer->sock.outCount, &sent)) {
case RAWSOCK_OK: /* we're ok! */

View File

@@ -32,7 +32,7 @@ void laikaP_initPList(struct sLaika_pollList *pList) {
/* setup our epoll */
memset(&pList->ev, 0, sizeof(struct epoll_event));
if ((pList->epollfd = epoll_create(POLLSTARTCAP)) == -1)
LAIKA_ERROR("epoll_create() failed!");
LAIKA_ERROR("epoll_create() failed!\n");
#else
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;
if (epoll_ctl(pList->epollfd, EPOLL_CTL_ADD, sock->sock, &pList->ev) == -1)
LAIKA_ERROR("epoll_ctl [ADD] failed");
LAIKA_ERROR("epoll_ctl [ADD] failed\n");
#else
/* 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] */
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. */
LAIKA_WARN("epoll_ctl [DEL] failed");
LAIKA_WARN("epoll_ctl [DEL] failed\n");
}
#else
int i;
@@ -101,7 +101,7 @@ void laikaP_addPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock
pList->ev.data.ptr = (void*)sock;
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. */
LAIKA_WARN("epoll_ctl [MOD] failed");
LAIKA_WARN("epoll_ctl [MOD] failed\n");
}
#else
int i;
@@ -122,7 +122,7 @@ void laikaP_rmvPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock
pList->ev.data.ptr = (void*)sock;
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. */
LAIKA_WARN("epoll_ctl [MOD] failed");
LAIKA_WARN("epoll_ctl [MOD] failed\n");
}
#else
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);
if (SOCKETERROR(nEvents))
LAIKA_ERROR("epoll_wait() failed!");
LAIKA_ERROR("epoll_wait() failed!\n");
for (i = 0; i < nEvents; i++) {
/* 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 */
if (SOCKETERROR(nEvents))
LAIKA_ERROR("poll() failed!");
LAIKA_ERROR("poll() failed!\n");
/* 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++) {

View File

@@ -15,7 +15,7 @@ void laikaS_init(void) {
WSADATA wsaData;
if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0)
LAIKA_ERROR("WSAStartup failed!")
LAIKA_ERROR("WSAStartup failed!\n")
#endif
}
@@ -39,7 +39,6 @@ void laikaS_initSocket(struct sLaika_socket *sock) {
sock->flipEndian = false;
laikaS_init();
return sock;
}
void laikaS_cleanSocket(struct sLaika_socket *sock) {
@@ -71,7 +70,7 @@ void laikaS_connect(struct sLaika_socket *sock, char *ip, char *port) {
struct addrinfo res, *result, *curr;
if (!SOCKETINVALID(sock->sock))
LAIKA_ERROR("socket already setup!");
LAIKA_ERROR("socket already setup!\n");
/* zero out our address info and setup the type */
memset(&res, 0, sizeof(struct addrinfo));
@@ -80,7 +79,7 @@ void laikaS_connect(struct sLaika_socket *sock, char *ip, char *port) {
/* grab the address info */
if (getaddrinfo(ip, port, &res, &result) != 0)
LAIKA_ERROR("getaddrinfo() failed!");
LAIKA_ERROR("getaddrinfo() failed!\n");
/* 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) {
@@ -100,20 +99,20 @@ 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 (curr == NULL)
LAIKA_ERROR("couldn't connect a valid address handle to socket!");
LAIKA_ERROR("couldn't connect a valid address handle to socket!\n");
}
void laikaS_bind(struct sLaika_socket *sock, uint16_t port) {
socklen_t addressSize;
struct sockaddr_in address;
if (!SOCKETINVALID(sock))
LAIKA_ERROR("socket already setup!")
if (!SOCKETINVALID(sock->sock))
LAIKA_ERROR("socket already setup!\n")
/* open our socket */
sock->sock = socket(AF_INET, SOCK_STREAM, 0);
if (SOCKETINVALID(sock))
LAIKA_ERROR("socket() failed!");
if (SOCKETINVALID(sock->sock))
LAIKA_ERROR("socket() failed!\n");
/* attach socket to the port */
int opt = 1;
@@ -122,7 +121,7 @@ void laikaS_bind(struct sLaika_socket *sock, uint16_t port) {
#else
if (setsockopt(sock->sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int)) != 0)
#endif
LAIKA_ERROR("setsockopt() failed!");
LAIKA_ERROR("setsockopt() failed!\n");
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
@@ -132,19 +131,19 @@ void laikaS_bind(struct sLaika_socket *sock, uint16_t port) {
/* bind to the port */
if (SOCKETERROR(bind(sock->sock, (struct sockaddr *)&address, addressSize)))
LAIKA_ERROR("bind() failed!");
LAIKA_ERROR("bind() failed!\n");
if (SOCKETERROR(listen(sock->sock, SOMAXCONN)))
LAIKA_ERROR("listen() failed!");
LAIKA_ERROR("listen() failed!\n");
}
void laikaS_acceptFrom(struct sLaika_socket *sock, struct sLaika_socket *from) {
socklen_t addressSize;
struct sockaddr address;
sock = accept(from->sock, &address, &addressSize);
if (SOCKETINVALID(sock))
LAIKA_ERROR("accept() failed!")
sock->sock = accept(from->sock, &address, &addressSize);
if (SOCKETINVALID(sock->sock))
LAIKA_ERROR("accept() failed!\n")
}
bool laikaS_setNonBlock(struct sLaika_socket *sock) {
@@ -154,7 +153,7 @@ bool laikaS_setNonBlock(struct sLaika_socket *sock) {
#else
if (fcntl(sock->sock, F_SETFL, (fcntl(sock->sock, F_GETFL, 0) | O_NONBLOCK)) != 0) {
#endif
LAIKA_WARN("fcntl failed on new connection");
LAIKA_WARN("fcntl failed on new connection\n");
laikaS_kill(sock);
return false;
}