mirror of
https://github.com/CPunch/Laika.git
synced 2025-11-30 22:50:04 +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:
@@ -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