From 770e34463e9602951642127bc983ab243d55668e Mon Sep 17 00:00:00 2001 From: CPunch Date: Wed, 3 Aug 2022 15:20:00 -0500 Subject: [PATCH] CNC: use laikaC_iterPeersNext for sweepPeersTask --- cnc/include/cnc.h | 3 ++- cnc/src/cnc.c | 35 ++++++++++++++++++----------------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/cnc/include/cnc.h b/cnc/include/cnc.h index 44a9df4..1b491c1 100644 --- a/cnc/include/cnc.h +++ b/cnc/include/cnc.h @@ -54,6 +54,7 @@ struct sLaika_peer *laikaC_getPeerByPub(struct sLaika_cnc *cnc, uint8_t *pub); void laikaC_sweepPeersTask(struct sLaika_taskService *service, struct sLaika_task *task, clock_t currTick, void *uData); +bool laikaC_iterPeersNext(struct sLaika_cnc *cnc, size_t *i, struct sLaika_peer **peer); void laikaC_iterPeers(struct sLaika_cnc *cnc, tLaika_peerIter iter, void *uData); -#endif \ No newline at end of file +#endif diff --git a/cnc/src/cnc.c b/cnc/src/cnc.c index c694ce3..a6992a6 100644 --- a/cnc/src/cnc.c +++ b/cnc/src/cnc.c @@ -426,27 +426,28 @@ struct sLaika_peer *laikaC_getPeerByPub(struct sLaika_cnc *cnc, uint8_t *pub) return elem ? elem->peer : NULL; } -bool sweepPeers(struct sLaika_peer *peer, void *uData) -{ - struct sLaika_peerInfo *pInfo = GETPINFOFROMPEER(peer); - struct sLaika_cnc *cnc = (struct sLaika_cnc *)uData; - long currTime = laikaT_getTime(); - - /* peer has been silent for a while, kill 'em */ - if (currTime - pInfo->lastPing > LAIKA_PEER_TIMEOUT) { - LAIKA_DEBUG("timeout reached for %p! [%d]\n", peer, currTime - pInfo->lastPing); - laikaC_killPeer(cnc, peer); - } - - return true; -} - void laikaC_sweepPeersTask(struct sLaika_taskService *service, struct sLaika_task *task, clock_t currTick, void *uData) { struct sLaika_cnc *cnc = (struct sLaika_cnc *)uData; + struct sLaika_peer *peer; + struct sLaika_peerInfo *pInfo; + size_t i = 0; + long currTime; - laikaC_iterPeers(cnc, sweepPeers, (void *)cnc); + while (laikaC_iterPeersNext(cnc, &i, &peer)) { + pInfo = GETPINFOFROMPEER(peer); + currTime = laikaT_getTime(); + + /* peer has been silent for a while, kill 'em */ + if (currTime - pInfo->lastPing > LAIKA_PEER_TIMEOUT) { + LAIKA_DEBUG("timeout reached for %p! [%ld]\n", peer, currTime); + laikaC_killPeer(cnc, peer); + + /* reset peer iterator (since the hashmap mightve been reallocated/changed) */ + i = 0; + } + } } /* =======================================[[ Peer Iter ]]======================================= */ @@ -472,4 +473,4 @@ void laikaC_iterPeers(struct sLaika_cnc *cnc, tLaika_peerIter iter, void *uData) /* call iter for every peer in cnc->peers */ while (laikaC_iterPeersNext(cnc, &i, &peer)) iter(peer, uData); -} \ No newline at end of file +}