mirror of
https://github.com/CPunch/Laika.git
synced 2025-12-08 01:30:06 +00:00
Refactored polling, lpolllist.c now handles poll events & flushes the poll queue
- Sockets now have event callbacks, onPollIn, onPollOut & onPollFail. If these are set to NULL they're ignored
This commit is contained in:
@@ -60,7 +60,7 @@ struct sLaika_peer {
|
||||
bool useSecure; /* if true, peer will transmit/receive encrypted data using inKey & outKey */
|
||||
};
|
||||
|
||||
struct sLaika_peer *laikaS_newPeer(struct sLaika_peerPacketInfo *packetTbl, struct sLaika_pollList *pList, void *uData);
|
||||
struct sLaika_peer *laikaS_newPeer(struct sLaika_peerPacketInfo *packetTbl, struct sLaika_pollList *pList, pollFailEvent onPollFail, void *onPollFailUData, void *uData);
|
||||
void laikaS_freePeer(struct sLaika_peer *peer);
|
||||
|
||||
void laikaS_setSecure(struct sLaika_peer *peer, bool flag);
|
||||
@@ -69,7 +69,7 @@ void laikaS_startOutPacket(struct sLaika_peer *peer, LAIKAPKT_ID id);
|
||||
int laikaS_endOutPacket(struct sLaika_peer *peer);
|
||||
void laikaS_startVarPacket(struct sLaika_peer *peer, LAIKAPKT_ID id);
|
||||
int laikaS_endVarPacket(struct sLaika_peer *peer);
|
||||
bool laikaS_handlePeerIn(struct sLaika_peer *peer);
|
||||
bool laikaS_handlePeerOut(struct sLaika_peer *peer);
|
||||
bool laikaS_handlePeerIn(struct sLaika_socket *sock);
|
||||
bool laikaS_handlePeerOut(struct sLaika_socket *sock);
|
||||
|
||||
#endif
|
||||
@@ -1,6 +1,8 @@
|
||||
#ifndef LAIKA_POLLLIST_H
|
||||
#define LAIKA_POLLLIST_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "laika.h"
|
||||
#include "lsocket.h"
|
||||
#include "hashmap.h"
|
||||
@@ -14,11 +16,9 @@ struct sLaika_pollEvent {
|
||||
bool pollOut;
|
||||
};
|
||||
|
||||
struct sLaika_peer;
|
||||
|
||||
struct sLaika_pollList {
|
||||
struct hashmap *sockets;
|
||||
struct sLaika_peer **outQueue; /* holds peers which have data needed to be sent */
|
||||
struct sLaika_socket **outQueue; /* holds sockets which have data needed to be sent */
|
||||
struct sLaika_pollEvent *revents;
|
||||
#ifdef LAIKA_USE_EPOLL
|
||||
/* epoll */
|
||||
@@ -42,9 +42,11 @@ void laikaP_addSock(struct sLaika_pollList *pList, struct sLaika_socket *sock);
|
||||
void laikaP_rmvSock(struct sLaika_pollList *pList, struct sLaika_socket *sock);
|
||||
void laikaP_addPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock);
|
||||
void laikaP_rmvPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock);
|
||||
void laikaP_pushOutQueue(struct sLaika_pollList *pList, struct sLaika_peer *peer);
|
||||
void laikaP_pushOutQueue(struct sLaika_pollList *pList, struct sLaika_socket *sock);
|
||||
void laikaP_resetOutQueue(struct sLaika_pollList *pList);
|
||||
void laikaP_flushOutQueue(struct sLaika_pollList *pList);
|
||||
|
||||
struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout, int *nevents);
|
||||
bool laikaP_handleEvent(struct sLaika_pollEvent *evnt);
|
||||
|
||||
#endif
|
||||
@@ -1,7 +1,6 @@
|
||||
#ifndef LAIKA_SOCKET_H
|
||||
#define LAIKA_SOCKET_H
|
||||
|
||||
|
||||
/* socket/winsock headers */
|
||||
#ifdef _WIN32
|
||||
/* windows */
|
||||
@@ -50,6 +49,7 @@
|
||||
#define SOCKETERROR(x) (x == -1)
|
||||
#endif
|
||||
#include <fcntl.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "lsodium.h"
|
||||
|
||||
@@ -60,8 +60,16 @@ typedef enum {
|
||||
RAWSOCK_POLL
|
||||
} RAWSOCKCODE;
|
||||
|
||||
struct sLaika_socket;
|
||||
typedef bool (*pollEvent)(struct sLaika_socket *sock);
|
||||
typedef void (*pollFailEvent)(struct sLaika_socket *sock, void *uData);
|
||||
|
||||
struct sLaika_socket {
|
||||
SOCKET sock; /* raw socket fd */
|
||||
pollFailEvent onPollFail;
|
||||
pollEvent onPollIn;
|
||||
pollEvent onPollOut;
|
||||
void *uData; /* passed to onPollFail */
|
||||
uint8_t *outBuf; /* raw data to be sent() */
|
||||
uint8_t *inBuf; /* raw data we recv()'d */
|
||||
int outCount;
|
||||
@@ -78,7 +86,7 @@ bool laikaS_isBigEndian(void);
|
||||
void laikaS_init(void);
|
||||
void laikaS_cleanUp(void);
|
||||
|
||||
void laikaS_initSocket(struct sLaika_socket *sock);
|
||||
void laikaS_initSocket(struct sLaika_socket *sock, pollEvent onPollIn, pollEvent onPollOut, pollFailEvent onPollFail, void *uData);
|
||||
void laikaS_cleanSocket(struct sLaika_socket *sock);
|
||||
void laikaS_kill(struct sLaika_socket *sock); /* kills a socket */
|
||||
void laikaS_connect(struct sLaika_socket *sock, char *ip, char *port); /* connect to ip & port */
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
#include "lmem.h"
|
||||
#include "lpeer.h"
|
||||
|
||||
struct sLaika_peer *laikaS_newPeer(struct sLaika_peerPacketInfo *pktTbl, struct sLaika_pollList *pList, void *uData) {
|
||||
struct sLaika_peer *laikaS_newPeer(struct sLaika_peerPacketInfo *pktTbl, struct sLaika_pollList *pList, pollFailEvent onPollFail, void *onPollFailUData, void *uData) {
|
||||
struct sLaika_peer *peer = laikaM_malloc(sizeof(struct sLaika_peer));
|
||||
|
||||
laikaS_initSocket(&peer->sock);
|
||||
laikaS_initSocket(&peer->sock, laikaS_handlePeerIn, laikaS_handlePeerOut, onPollFail, onPollFailUData);
|
||||
peer->packetTbl = pktTbl;
|
||||
peer->pList = pList;
|
||||
peer->uData = uData;
|
||||
@@ -36,7 +36,7 @@ void laikaS_emptyOutPacket(struct sLaika_peer *peer, LAIKAPKT_ID id) {
|
||||
laikaS_writeByte(sock, id);
|
||||
|
||||
/* add to pollList's out queue */
|
||||
laikaP_pushOutQueue(peer->pList, peer);
|
||||
laikaP_pushOutQueue(peer->pList, &peer->sock);
|
||||
}
|
||||
|
||||
void laikaS_startOutPacket(struct sLaika_peer *peer, LAIKAPKT_ID id) {
|
||||
@@ -76,7 +76,7 @@ int laikaS_endOutPacket(struct sLaika_peer *peer) {
|
||||
}
|
||||
|
||||
/* add to pollList's out queue */
|
||||
laikaP_pushOutQueue(peer->pList, peer);
|
||||
laikaP_pushOutQueue(peer->pList, &peer->sock);
|
||||
|
||||
/* return packet size and prepare for next outPacket */
|
||||
sz = sock->outCount - peer->outStart;
|
||||
@@ -149,7 +149,8 @@ void laikaS_setSecure(struct sLaika_peer *peer, bool flag) {
|
||||
peer->useSecure = flag;
|
||||
}
|
||||
|
||||
bool laikaS_handlePeerIn(struct sLaika_peer *peer) {
|
||||
bool laikaS_handlePeerIn(struct sLaika_socket *sock) {
|
||||
struct sLaika_peer *peer = (struct sLaika_peer*)sock;
|
||||
RAWSOCKCODE err;
|
||||
int recvd;
|
||||
|
||||
@@ -227,7 +228,8 @@ bool laikaS_handlePeerIn(struct sLaika_peer *peer) {
|
||||
return laikaS_isAlive((&peer->sock));
|
||||
}
|
||||
|
||||
bool laikaS_handlePeerOut(struct sLaika_peer *peer) {
|
||||
bool laikaS_handlePeerOut(struct sLaika_socket *sock) {
|
||||
struct sLaika_peer *peer = (struct sLaika_peer*)sock;
|
||||
RAWSOCKCODE err;
|
||||
int sent;
|
||||
|
||||
|
||||
@@ -150,23 +150,37 @@ void laikaP_rmvPollOut(struct sLaika_pollList *pList, struct sLaika_socket *sock
|
||||
#endif
|
||||
}
|
||||
|
||||
void laikaP_pushOutQueue(struct sLaika_pollList *pList, struct sLaika_peer *peer) {
|
||||
void laikaP_pushOutQueue(struct sLaika_pollList *pList, struct sLaika_socket *sock) {
|
||||
int i;
|
||||
|
||||
/* first, check that we don't have this peer in the queue already */
|
||||
for (i = 0; i < pList->outCount; i++) {
|
||||
if (pList->outQueue[i] == peer)
|
||||
if (pList->outQueue[i] == sock)
|
||||
return; /* found it :) */
|
||||
}
|
||||
|
||||
laikaM_growarray(struct sLaika_peer*, pList->outQueue, 1, pList->outCount, pList->outCap);
|
||||
pList->outQueue[pList->outCount++] = peer;
|
||||
laikaM_growarray(struct sLaika_socket*, pList->outQueue, 1, pList->outCount, pList->outCap);
|
||||
pList->outQueue[pList->outCount++] = sock;
|
||||
}
|
||||
|
||||
void laikaP_resetOutQueue(struct sLaika_pollList *pList) {
|
||||
pList->outCount = 0; /* ez lol */
|
||||
}
|
||||
|
||||
void laikaP_flushOutQueue(struct sLaika_pollList *pList) {
|
||||
struct sLaika_socket *sock;
|
||||
int i;
|
||||
|
||||
/* flush pList's outQueue */
|
||||
for (i = 0; i < pList->outCount; i++) {
|
||||
sock = pList->outQueue[i];
|
||||
LAIKA_DEBUG("sending OUT to %p\n", sock);
|
||||
if (sock->onPollOut && !sock->onPollOut(sock) && sock->onPollFail)
|
||||
sock->onPollFail(sock, sock->uData);
|
||||
}
|
||||
laikaP_resetOutQueue(pList);
|
||||
}
|
||||
|
||||
struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout, int *_nevents) {
|
||||
int nEvents, i;
|
||||
|
||||
@@ -219,4 +233,31 @@ struct sLaika_pollEvent *laikaP_poll(struct sLaika_pollList *pList, int timeout,
|
||||
|
||||
/* return revents array */
|
||||
return pList->revents;
|
||||
}
|
||||
|
||||
bool laikaP_handleEvent(struct sLaika_pollEvent *evnt) {
|
||||
bool result = true;
|
||||
|
||||
/* sanity check */
|
||||
if (evnt->sock->onPollIn == NULL || evnt->sock->onPollOut == NULL)
|
||||
return result;
|
||||
|
||||
LAIKA_TRY
|
||||
if (evnt->pollIn && !evnt->sock->onPollIn(evnt->sock))
|
||||
goto _PHNDLEEVNTFAIL;
|
||||
|
||||
if (evnt->pollOut && !evnt->sock->onPollIn(evnt->sock))
|
||||
goto _PHNDLEEVNTFAIL;
|
||||
|
||||
if (!evnt->pollIn && !evnt->pollOut)
|
||||
goto _PHNDLEEVNTFAIL;
|
||||
LAIKA_CATCH
|
||||
_PHNDLEEVNTFAIL:
|
||||
/* call onFail event */
|
||||
if (evnt->sock->onPollFail)
|
||||
evnt->sock->onPollFail(evnt->sock, evnt->sock->uData);
|
||||
result = false;
|
||||
LAIKA_TRYEND
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -37,8 +37,12 @@ void laikaS_cleanUp(void) {
|
||||
#endif
|
||||
}
|
||||
|
||||
void laikaS_initSocket(struct sLaika_socket *sock) {
|
||||
void laikaS_initSocket(struct sLaika_socket *sock, pollEvent onPollIn, pollEvent onPollOut, pollFailEvent onPollFail, void *uData) {
|
||||
sock->sock = INVALID_SOCKET;
|
||||
sock->onPollFail = onPollFail;
|
||||
sock->onPollIn = onPollIn;
|
||||
sock->onPollOut = onPollOut;
|
||||
sock->uData = uData;
|
||||
sock->inBuf = NULL;
|
||||
sock->inCap = ARRAY_START;
|
||||
sock->inCount = 0;
|
||||
|
||||
Reference in New Issue
Block a user