1
0
mirror of https://github.com/CPunch/Laika.git synced 2024-11-24 13:31:05 +00:00

Added laikaS_startVarPacket() & laikaS_endVarPacket()

This commit is contained in:
CPunch 2022-02-18 19:32:16 -06:00
parent f8ac74881e
commit 02c3176bc4
3 changed files with 33 additions and 9 deletions

View File

@ -56,8 +56,8 @@ enum {
*/
LAIKAPKT_VARPKT_REQ,
/* layout of LAIKAPKT_VARPKT_REQ:
* LAIKAPKT_ID pktID;
* LAIKAPKT_SIZE pktSize;
* LAIKAPKT_ID pktID;
*/
LAIKAPKT_MAXNONE
};

View File

@ -39,8 +39,10 @@ struct sLaika_peer *laikaS_newPeer(PeerPktHandler *handlers, LAIKAPKT_SIZE *pktS
void laikaS_freePeer(struct sLaika_peer *peer);
void laikaS_setSecure(struct sLaika_peer *peer, bool flag);
void laikaS_startOutPacket(struct sLaika_peer *peer, uint8_t id);
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);

View File

@ -28,7 +28,7 @@ void laikaS_freePeer(struct sLaika_peer *peer) {
laikaM_free(peer);
}
void laikaS_startOutPacket(struct sLaika_peer *peer, uint8_t id) {
void laikaS_startOutPacket(struct sLaika_peer *peer, LAIKAPKT_ID id) {
struct sLaika_socket *sock = &peer->sock;
if (peer->outStart != -1) { /* sanity check */
@ -72,6 +72,28 @@ int laikaS_endOutPacket(struct sLaika_peer *peer) {
return sz;
}
void laikaS_startVarPacket(struct sLaika_peer *peer, LAIKAPKT_ID id) {
struct sLaika_socket *sock = &peer->sock;
if (peer->outStart != -1) { /* sanity check */
LAIKA_ERROR("unended OUT packet!\n")
}
laikaS_writeByte(sock, LAIKAPKT_VARPKT_REQ);
laikaS_zeroWrite(sock, sizeof(LAIKAPKT_SIZE)); /* allocate space for packet size to patch later */
laikaS_startOutPacket(peer, id);
}
int laikaS_endVarPacket(struct sLaika_peer *peer) {
struct sLaika_socket *sock = &peer->sock;
int patchIndx = peer->outStart - 3; /* gets index of packet size */
LAIKAPKT_SIZE sz = (LAIKAPKT_SIZE)laikaS_endOutPacket(peer);
/* patch packet size */
memcpy((void*)&sock->outBuf[patchIndx], (void*)&sz, sizeof(LAIKAPKT_SIZE));
return sz;
}
void laikaS_startInPacket(struct sLaika_peer *peer) {
struct sLaika_socket *sock = &peer->sock;
@ -151,6 +173,12 @@ bool laikaS_handlePeerIn(struct sLaika_peer *peer) {
if (recvd != sizeof(LAIKAPKT_ID) + sizeof(LAIKAPKT_SIZE))
LAIKA_ERROR("couldn't read whole LAIKAPKT_VARPKT_REQ")
/* read packet size */
laikaS_readInt(&peer->sock, (void*)&peer->pktSize, sizeof(LAIKAPKT_SIZE));
if (peer->pktSize > LAIKA_MAX_PKTSIZE)
LAIKA_ERROR("variable packet too large!")
/* read pktID */
peer->pktID = laikaS_readByte(&peer->sock);
@ -158,12 +186,6 @@ bool laikaS_handlePeerIn(struct sLaika_peer *peer) {
if (peer->pktID >= LAIKAPKT_MAXNONE || peer->pktSizeTable[peer->pktID] != 0 || peer->handlers[peer->pktID] == NULL)
LAIKA_ERROR("requested packet id [%d] is not variadic!", peer->pktID)
/* read packet size */
laikaS_readInt(&peer->sock, (void*)&peer->pktSize, sizeof(LAIKAPKT_SIZE));
if (peer->pktSize > LAIKA_MAX_PKTSIZE)
LAIKA_ERROR("variable packet too large!")
/* if peer->useSecure is true, body is encrypted */
laikaS_startInPacket(peer);
break;