mirror of
https://github.com/CPunch/Laika.git
synced 2024-11-22 04:50:06 +00:00
Added laikaS_readInt() & laikaS_writeInt()
- new member for the sLaika_sock struct, 'flipEndian'
This commit is contained in:
parent
7cec181f61
commit
ed3efdaf11
@ -7,7 +7,6 @@
|
|||||||
|
|
||||||
#define laikaM_malloc(sz) laikaM_realloc(NULL, sz)
|
#define laikaM_malloc(sz) laikaM_realloc(NULL, sz)
|
||||||
#define laikaM_free(buf) laikaM_realloc(buf, 0)
|
#define laikaM_free(buf) laikaM_realloc(buf, 0)
|
||||||
#define laikaM_free(buf) laikaM_realloc(buf, 0)
|
|
||||||
|
|
||||||
#define laikaM_growarray(type, buf, count, capacity) \
|
#define laikaM_growarray(type, buf, count, capacity) \
|
||||||
if (count >= capacity || buf == NULL) { \
|
if (count >= capacity || buf == NULL) { \
|
||||||
|
@ -7,16 +7,16 @@
|
|||||||
#include "lpolllist.h"
|
#include "lpolllist.h"
|
||||||
|
|
||||||
struct sLaika_peer {
|
struct sLaika_peer {
|
||||||
struct sLaika_socket sock;
|
struct sLaika_socket sock; /* DO NOT MOVE THIS. this member HAS TO BE FIRST so that typecasting sLaika_peer* to sLaika_sock* works as intended */
|
||||||
struct sLaika_pollList *pList; /* pollList we're active in */
|
struct sLaika_pollList *pList; /* pollList we're active in */
|
||||||
void (*pktHandler)(struct sLaika_peer *peer, LAIKAPKT_ID id);
|
void (*pktHandler)(struct sLaika_peer *peer, LAIKAPKT_ID id);
|
||||||
size_t pktSize; /* current pkt size */
|
size_t pktSize; /* current pkt size */
|
||||||
LAIKAPKT_ID pktID; /* current pkt ID */
|
LAIKAPKT_ID pktID; /* current pkt ID */
|
||||||
size_t *pktSizeTable; /* const table to pull pkt size data from */
|
size_t *pktSizeTable; /* const table to pull pkt size data from */
|
||||||
bool setPollOut; /* if EPOLLOUT/POLLOUT is set on sock's pollfd */
|
bool setPollOut; /* is EPOLLOUT/POLLOUT is set on sock's pollfd ? */
|
||||||
};
|
};
|
||||||
|
|
||||||
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);
|
||||||
void laikaS_freePeer(struct sLaika_peer *peer);
|
void laikaS_freePeer(struct sLaika_peer *peer);
|
||||||
|
|
||||||
bool laikaS_handlePeerIn(struct sLaika_peer *peer);
|
bool laikaS_handlePeerIn(struct sLaika_peer *peer);
|
||||||
|
@ -62,10 +62,20 @@ struct sLaika_socket {
|
|||||||
int inCount;
|
int inCount;
|
||||||
int outCap;
|
int outCap;
|
||||||
int inCap;
|
int inCap;
|
||||||
|
bool flipEndian
|
||||||
};
|
};
|
||||||
|
|
||||||
#define laikaS_isAlive(arg) (arg->sock != INVALID_SOCKET)
|
#define laikaS_isAlive(arg) (arg->sock != INVALID_SOCKET)
|
||||||
|
|
||||||
|
inline bool laikaS_isBigEndian(void) {
|
||||||
|
union {
|
||||||
|
uint32_t i;
|
||||||
|
uint8_t c[4];
|
||||||
|
} _indxint = {0xDEADB33F};
|
||||||
|
|
||||||
|
return _indxint.c[0] == 0xDE;
|
||||||
|
}
|
||||||
|
|
||||||
void laikaS_init(void);
|
void laikaS_init(void);
|
||||||
void laikaS_cleanUp(void);
|
void laikaS_cleanUp(void);
|
||||||
|
|
||||||
@ -79,9 +89,10 @@ bool laikaS_setNonBlock(struct sLaika_socket *sock);
|
|||||||
|
|
||||||
void laikaS_read(struct sLaika_socket *sock, void *buf, size_t sz); /* reads from inBuf */
|
void laikaS_read(struct sLaika_socket *sock, void *buf, size_t sz); /* reads from inBuf */
|
||||||
void laikaS_write(struct sLaika_socket *sock, void *buf, size_t sz); /* writes to outBuf */
|
void laikaS_write(struct sLaika_socket *sock, void *buf, size_t sz); /* writes to outBuf */
|
||||||
|
|
||||||
void laikaS_writeByte(struct sLaika_socket *sock, uint8_t data);
|
void laikaS_writeByte(struct sLaika_socket *sock, uint8_t data);
|
||||||
uint8_t laikaS_readByte(struct sLaika_socket *sock);
|
uint8_t laikaS_readByte(struct sLaika_socket *sock);
|
||||||
|
void laikaS_readInt(struct sLaika_socket *sock, void *buf, size_t sz); /* reads INT, respecting endianness */
|
||||||
|
void laikaS_writeInt(struct sLaika_socket *sock, void *buf, size_t sz); /* writes INT, respecting endianness */
|
||||||
|
|
||||||
RAWSOCKCODE laikaS_rawRecv(struct sLaika_socket *sock, size_t sz, int *processed);
|
RAWSOCKCODE laikaS_rawRecv(struct sLaika_socket *sock, size_t sz, int *processed);
|
||||||
RAWSOCKCODE laikaS_rawSend(struct sLaika_socket *sock, size_t sz, int *processed);
|
RAWSOCKCODE laikaS_rawSend(struct sLaika_socket *sock, size_t sz, int *processed);
|
@ -2,7 +2,7 @@
|
|||||||
#include "lmem.h"
|
#include "lmem.h"
|
||||||
#include "lpeer.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));
|
struct sLaika_peer *peer = laikaM_malloc(sizeof(struct sLaika_peer));
|
||||||
|
|
||||||
laikaS_initSocket(&peer->sock);
|
laikaS_initSocket(&peer->sock);
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
#include <alloca.h>
|
||||||
|
|
||||||
#include "lerror.h"
|
#include "lerror.h"
|
||||||
#include "lmem.h"
|
#include "lmem.h"
|
||||||
#include "lpolllist.h"
|
#include "lpolllist.h"
|
||||||
@ -34,6 +36,7 @@ void laikaS_initSocket(struct sLaika_socket *sock) {
|
|||||||
sock->outBuf = NULL;
|
sock->outBuf = NULL;
|
||||||
sock->outCap = ARRAY_START;
|
sock->outCap = ARRAY_START;
|
||||||
sock->outCount = 0;
|
sock->outCount = 0;
|
||||||
|
sock->flipEndian = false;
|
||||||
|
|
||||||
laikaS_init();
|
laikaS_init();
|
||||||
return sock;
|
return sock;
|
||||||
@ -186,6 +189,38 @@ uint8_t laikaS_readByte(struct sLaika_socket *sock) {
|
|||||||
return tmp;
|
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 laikaS_rawRecv(struct sLaika_socket *sock, size_t sz, int *processed) {
|
||||||
RAWSOCKCODE errCode = RAWSOCK_OK;
|
RAWSOCKCODE errCode = RAWSOCK_OK;
|
||||||
int rcvd, start = sock->inCount;
|
int rcvd, start = sock->inCount;
|
||||||
|
Loading…
Reference in New Issue
Block a user