mirror of
https://github.com/CPunch/Laika.git
synced 2025-11-27 21:41:04 +00:00
Minor lpeer refactoring
- Packets are now dispatched based on a packet function handler map which is passed to laikaS_newPeer()
This commit is contained in:
@@ -2,11 +2,11 @@
|
||||
#include "lmem.h"
|
||||
#include "lpeer.h"
|
||||
|
||||
struct sLaika_peer *laikaS_newPeer(void (*pktHandler)(struct sLaika_peer *peer, LAIKAPKT_ID id, void *uData), LAIKAPKT_SIZE *pktSizeTable, struct sLaika_pollList *pList, void *uData) {
|
||||
struct sLaika_peer *laikaS_newPeer(PeerPktHandler *handlers, LAIKAPKT_SIZE *pktSizeTable, struct sLaika_pollList *pList, void *uData) {
|
||||
struct sLaika_peer *peer = laikaM_malloc(sizeof(struct sLaika_peer));
|
||||
|
||||
laikaS_initSocket(&peer->sock);
|
||||
peer->pktHandler = pktHandler;
|
||||
peer->handlers = handlers;
|
||||
peer->pktSizeTable = pktSizeTable;
|
||||
peer->pList = pList;
|
||||
peer->uData = uData;
|
||||
@@ -78,7 +78,12 @@ bool laikaS_handlePeerIn(struct sLaika_peer *peer) {
|
||||
|
||||
/* have we received the full packet? */
|
||||
if (peer->pktSize == peer->sock.inCount) {
|
||||
peer->pktHandler(peer, peer->pktID, peer->uData); /* dispatch to packet handler */
|
||||
PeerPktHandler hndlr = peer->handlers[peer->pktID];
|
||||
|
||||
if (hndlr != NULL) {
|
||||
hndlr(peer, peer->pktID, peer->uData); /* dispatch to packet handler */
|
||||
} else
|
||||
LAIKA_ERROR("peer %x doesn't support packet id [%d]!\n", peer, peer->pktID);
|
||||
|
||||
/* reset */
|
||||
peer->sock.inCount = 0;
|
||||
|
||||
@@ -191,7 +191,7 @@ void laikaS_writeENC(struct sLaika_socket *sock, void *buf, size_t sz, uint8_t *
|
||||
|
||||
/* encrypt the buffer into outBuf */
|
||||
if (crypto_box_seal(&sock->outBuf[sock->outCount], buf, sz, pub) != 0)
|
||||
LAIKA_ERROR("Failed to encrypt!");
|
||||
LAIKA_ERROR("Failed to encrypt!\n");
|
||||
|
||||
sock->outCount += LAIKAENC_SIZE(sz);
|
||||
}
|
||||
@@ -199,7 +199,7 @@ void laikaS_writeENC(struct sLaika_socket *sock, void *buf, size_t sz, uint8_t *
|
||||
void laikaS_readENC(struct sLaika_socket *sock, void *buf, size_t sz, uint8_t *pub, uint8_t *priv) {
|
||||
/* decrypt into buf */
|
||||
if (crypto_box_seal_open(buf, sock->inBuf, LAIKAENC_SIZE(sz), pub, priv) != 0)
|
||||
LAIKA_ERROR("Failed to decrypt!");
|
||||
LAIKA_ERROR("Failed to decrypt!\n");
|
||||
|
||||
laikaM_rmvarray(uint8_t, sock->inBuf, sock->inCount, 0, LAIKAENC_SIZE(sz));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user