1
0
mirror of https://github.com/CPunch/Laika.git synced 2025-10-07 16:40:05 +00:00

Added laikaS_readInt() & laikaS_writeInt()

- new member for the sLaika_sock struct, 'flipEndian'
This commit is contained in:
2022-01-24 20:50:14 -06:00
parent 7cec181f61
commit ed3efdaf11
5 changed files with 51 additions and 6 deletions

View File

@@ -2,7 +2,7 @@
#include "lmem.h"
#include "lpeer.h"
struct sLaika_peer *laikaS_newPeer(void (*pktHandler)(struct sLaika_peer *peer, LAIKAPKT_ID id), struct sLaika_pollList *pList, size_t *pktSizeTable) {
struct sLaika_peer *laikaS_newPeer(void (*pktHandler)(struct sLaika_peer *peer, LAIKAPKT_ID id), size_t *pktSizeTable, struct sLaika_pollList *pList) {
struct sLaika_peer *peer = laikaM_malloc(sizeof(struct sLaika_peer));
laikaS_initSocket(&peer->sock);

View File

@@ -1,3 +1,5 @@
#include <alloca.h>
#include "lerror.h"
#include "lmem.h"
#include "lpolllist.h"
@@ -34,6 +36,7 @@ void laikaS_initSocket(struct sLaika_socket *sock) {
sock->outBuf = NULL;
sock->outCap = ARRAY_START;
sock->outCount = 0;
sock->flipEndian = false;
laikaS_init();
return sock;
@@ -186,6 +189,38 @@ uint8_t laikaS_readByte(struct sLaika_socket *sock) {
return tmp;
}
void laikaS_readInt(struct sLaika_socket *sock, void *buf, size_t sz) {
if (sock->flipEndian) {
uint8_t tmp[sz]; /* allocate tmp buffer to hold data while we switch endianness */
int k;
laikaS_read(sock, (void*)tmp, sz);
/* copy tmp buffer to user buffer, flipping endianness */
for (k = 0; k < sz; k++)
*(uint8_t*)(buf + k) = tmp[sz - k - 1];
} else {
/* just a wrapper for laikaS_read */
laikaS_read(sock, buf, sz);
}
}
void laikaS_writeInt(struct sLaika_socket *sock, void *buf, size_t sz) {
if (sock->flipEndian) {
uint8_t tmp[sz]; /* allocate tmp buffer to hold data while we switch endianness */
int k;
/* copy user buffer to tmp buffer, flipping endianness */
for (k = 0; k < sz; k++)
tmp[k] = *(uint8_t*)(buf + (sz - k - 1));
laikaS_write(sock, (void*)tmp, sz);
} else {
/* just a wrapper for laikaS_write */
laikaS_write(sock, buf, sz);
}
}
RAWSOCKCODE laikaS_rawRecv(struct sLaika_socket *sock, size_t sz, int *processed) {
RAWSOCKCODE errCode = RAWSOCK_OK;
int rcvd, start = sock->inCount;