Use stdint types for ByteBuffer

This commit is contained in:
Cameron Gutman 2020-11-30 22:03:24 -06:00
parent 4b4162c1a1
commit 333382ae74
3 changed files with 42 additions and 42 deletions

View File

@ -8,7 +8,7 @@ void BbInitializeWrappedBuffer(PBYTE_BUFFER buff, char* data, int offset, int le
} }
// Get the long long in the correct byte order // Get the long long in the correct byte order
static long long byteSwapLongLong(PBYTE_BUFFER buff, long long l) { static uint64_t byteSwap64(PBYTE_BUFFER buff, uint64_t l) {
if (buff->byteOrder == BYTE_ORDER_BIG) { if (buff->byteOrder == BYTE_ORDER_BIG) {
return HTONLL(l); return HTONLL(l);
} }
@ -18,7 +18,7 @@ static long long byteSwapLongLong(PBYTE_BUFFER buff, long long l) {
} }
// Get the int in the correct byte order // Get the int in the correct byte order
static int byteSwapInt(PBYTE_BUFFER buff, int i) { static uint32_t byteSwap32(PBYTE_BUFFER buff, uint32_t i) {
if (buff->byteOrder == BYTE_ORDER_BIG) { if (buff->byteOrder == BYTE_ORDER_BIG) {
return htonl(i); return htonl(i);
} }
@ -28,7 +28,7 @@ static int byteSwapInt(PBYTE_BUFFER buff, int i) {
} }
// Get the short in the correct byte order // Get the short in the correct byte order
static int byteSwapShort(PBYTE_BUFFER buff, short s) { static uint16_t byteSwap16(PBYTE_BUFFER buff, uint16_t s) {
if (buff->byteOrder == BYTE_ORDER_BIG) { if (buff->byteOrder == BYTE_ORDER_BIG) {
return htons(s); return htons(s);
} }
@ -48,7 +48,7 @@ bool BbAdvanceBuffer(PBYTE_BUFFER buff, int offset) {
} }
// Get a byte from the byte buffer // Get a byte from the byte buffer
bool BbGet(PBYTE_BUFFER buff, char* c) { bool BbGet8(PBYTE_BUFFER buff, uint8_t* c) {
if (buff->position + sizeof(*c) > buff->length) { if (buff->position + sizeof(*c) > buff->length) {
return false; return false;
} }
@ -60,7 +60,7 @@ bool BbGet(PBYTE_BUFFER buff, char* c) {
} }
// Get a short from the byte buffer // Get a short from the byte buffer
bool BbGetShort(PBYTE_BUFFER buff, short* s) { bool BbGet16(PBYTE_BUFFER buff, uint16_t* s) {
if (buff->position + sizeof(*s) > buff->length) { if (buff->position + sizeof(*s) > buff->length) {
return false; return false;
} }
@ -68,13 +68,13 @@ bool BbGetShort(PBYTE_BUFFER buff, short* s) {
memcpy(s, &buff->buffer[buff->position], sizeof(*s)); memcpy(s, &buff->buffer[buff->position], sizeof(*s));
buff->position += sizeof(*s); buff->position += sizeof(*s);
*s = byteSwapShort(buff, *s); *s = byteSwap16(buff, *s);
return true; return true;
} }
// Get an int from the byte buffer // Get an int from the byte buffer
bool BbGetInt(PBYTE_BUFFER buff, int* i) { bool BbGet32(PBYTE_BUFFER buff, uint32_t* i) {
if (buff->position + sizeof(*i) > buff->length) { if (buff->position + sizeof(*i) > buff->length) {
return false; return false;
} }
@ -82,13 +82,13 @@ bool BbGetInt(PBYTE_BUFFER buff, int* i) {
memcpy(i, &buff->buffer[buff->position], sizeof(*i)); memcpy(i, &buff->buffer[buff->position], sizeof(*i));
buff->position += sizeof(*i); buff->position += sizeof(*i);
*i = byteSwapInt(buff, *i); *i = byteSwap32(buff, *i);
return true; return true;
} }
// Get a long from the byte buffer // Get a long from the byte buffer
bool BbGetLong(PBYTE_BUFFER buff, long long* l) { bool BbGet64(PBYTE_BUFFER buff, uint64_t* l) {
if (buff->position + sizeof(*l) > buff->length) { if (buff->position + sizeof(*l) > buff->length) {
return false; return false;
} }
@ -96,18 +96,18 @@ bool BbGetLong(PBYTE_BUFFER buff, long long* l) {
memcpy(l, &buff->buffer[buff->position], sizeof(*l)); memcpy(l, &buff->buffer[buff->position], sizeof(*l));
buff->position += sizeof(*l); buff->position += sizeof(*l);
*l = byteSwapLongLong(buff, *l); *l = byteSwap64(buff, *l);
return true; return true;
} }
// Put an int into the byte buffer // Put an int into the byte buffer
bool BbPutInt(PBYTE_BUFFER buff, int i) { bool BbPut32(PBYTE_BUFFER buff, uint32_t i) {
if (buff->position + sizeof(i) > buff->length) { if (buff->position + sizeof(i) > buff->length) {
return false; return false;
} }
i = byteSwapInt(buff, i); i = byteSwap32(buff, i);
memcpy(&buff->buffer[buff->position], &i, sizeof(i)); memcpy(&buff->buffer[buff->position], &i, sizeof(i));
buff->position += sizeof(i); buff->position += sizeof(i);
@ -116,12 +116,12 @@ bool BbPutInt(PBYTE_BUFFER buff, int i) {
} }
// Put a long into the byte buffer // Put a long into the byte buffer
bool BbPutLong(PBYTE_BUFFER buff, long long l) { bool BbPut64(PBYTE_BUFFER buff, uint64_t l) {
if (buff->position + sizeof(l) > buff->length) { if (buff->position + sizeof(l) > buff->length) {
return false; return false;
} }
l = byteSwapLongLong(buff, l); l = byteSwap64(buff, l);
memcpy(&buff->buffer[buff->position], &l, sizeof(l)); memcpy(&buff->buffer[buff->position], &l, sizeof(l));
buff->position += sizeof(l); buff->position += sizeof(l);
@ -130,12 +130,12 @@ bool BbPutLong(PBYTE_BUFFER buff, long long l) {
} }
// Put a short into the byte buffer // Put a short into the byte buffer
bool BbPutShort(PBYTE_BUFFER buff, short s) { bool BbPut16(PBYTE_BUFFER buff, uint16_t s) {
if (buff->position + sizeof(s) > buff->length) { if (buff->position + sizeof(s) > buff->length) {
return false; return false;
} }
s = byteSwapShort(buff, s); s = byteSwap16(buff, s);
memcpy(&buff->buffer[buff->position], &s, sizeof(s)); memcpy(&buff->buffer[buff->position], &s, sizeof(s));
buff->position += sizeof(s); buff->position += sizeof(s);
@ -144,7 +144,7 @@ bool BbPutShort(PBYTE_BUFFER buff, short s) {
} }
// Put a byte into the buffer // Put a byte into the buffer
bool BbPut(PBYTE_BUFFER buff, char c) { bool BbPut8(PBYTE_BUFFER buff, uint8_t c) {
if (buff->position + sizeof(c) > buff->length) { if (buff->position + sizeof(c) > buff->length) {
return false; return false;
} }

View File

@ -27,12 +27,12 @@ typedef struct _BYTE_BUFFER {
void BbInitializeWrappedBuffer(PBYTE_BUFFER buff, char* data, int offset, int length, int byteOrder); void BbInitializeWrappedBuffer(PBYTE_BUFFER buff, char* data, int offset, int length, int byteOrder);
bool BbAdvanceBuffer(PBYTE_BUFFER buff, int offset); bool BbAdvanceBuffer(PBYTE_BUFFER buff, int offset);
bool BbGet(PBYTE_BUFFER buff, char* c); bool BbGet8(PBYTE_BUFFER buff, uint8_t* c);
bool BbGetShort(PBYTE_BUFFER buff, short* s); bool BbGet16(PBYTE_BUFFER buff, uint16_t* s);
bool BbGetInt(PBYTE_BUFFER buff, int* i); bool BbGet32(PBYTE_BUFFER buff, uint32_t* i);
bool BbGetLong(PBYTE_BUFFER buff, long long* l); bool BbGet64(PBYTE_BUFFER buff, uint64_t* l);
bool BbPutInt(PBYTE_BUFFER buff, int i); bool BbPut8(PBYTE_BUFFER buff, uint8_t c);
bool BbPutShort(PBYTE_BUFFER buff, short s); bool BbPut16(PBYTE_BUFFER buff, uint16_t s);
bool BbPut(PBYTE_BUFFER buff, char c); bool BbPut32(PBYTE_BUFFER buff, uint32_t i);
bool BbPutLong(PBYTE_BUFFER buff, long long l); bool BbPut64(PBYTE_BUFFER buff, uint64_t l);

View File

@ -544,13 +544,13 @@ static void controlReceiveThreadFunc(void* context) {
BbInitializeWrappedBuffer(&bb, (char*)event.packet->data, sizeof(*ctlHdr), event.packet->dataLength - sizeof(*ctlHdr), BYTE_ORDER_LITTLE); BbInitializeWrappedBuffer(&bb, (char*)event.packet->data, sizeof(*ctlHdr), event.packet->dataLength - sizeof(*ctlHdr), BYTE_ORDER_LITTLE);
BbAdvanceBuffer(&bb, 4); BbAdvanceBuffer(&bb, 4);
unsigned short controllerNumber; uint16_t controllerNumber;
unsigned short lowFreqRumble; uint16_t lowFreqRumble;
unsigned short highFreqRumble; uint16_t highFreqRumble;
BbGetShort(&bb, (short*)&controllerNumber); BbGet16(&bb, &controllerNumber);
BbGetShort(&bb, (short*)&lowFreqRumble); BbGet16(&bb, &lowFreqRumble);
BbGetShort(&bb, (short*)&highFreqRumble); BbGet16(&bb, &highFreqRumble);
ListenerCallbacks.rumble(controllerNumber, lowFreqRumble, highFreqRumble); ListenerCallbacks.rumble(controllerNumber, lowFreqRumble, highFreqRumble);
} }
@ -559,10 +559,10 @@ static void controlReceiveThreadFunc(void* context) {
BbInitializeWrappedBuffer(&bb, (char*)event.packet->data, sizeof(*ctlHdr), event.packet->dataLength - sizeof(*ctlHdr), BYTE_ORDER_LITTLE); BbInitializeWrappedBuffer(&bb, (char*)event.packet->data, sizeof(*ctlHdr), event.packet->dataLength - sizeof(*ctlHdr), BYTE_ORDER_LITTLE);
unsigned short terminationReason; uint16_t terminationReason;
int terminationErrorCode; int terminationErrorCode;
BbGetShort(&bb, (short*)&terminationReason); BbGet16(&bb, &terminationReason);
Limelog("Server notified termination reason: 0x%04x\n", terminationReason); Limelog("Server notified termination reason: 0x%04x\n", terminationReason);
@ -603,8 +603,8 @@ static void lossStatsThreadFunc(void* context) {
char periodicPingPayload[8]; char periodicPingPayload[8];
BbInitializeWrappedBuffer(&byteBuffer, periodicPingPayload, 0, sizeof(periodicPingPayload), BYTE_ORDER_LITTLE); BbInitializeWrappedBuffer(&byteBuffer, periodicPingPayload, 0, sizeof(periodicPingPayload), BYTE_ORDER_LITTLE);
BbPutShort(&byteBuffer, 4); // Length of payload BbPut16(&byteBuffer, 4); // Length of payload
BbPutInt(&byteBuffer, 0); // Timestamp? BbPut32(&byteBuffer, 0); // Timestamp?
while (!PltIsThreadInterrupted(&lossStatsThread)) { while (!PltIsThreadInterrupted(&lossStatsThread)) {
// Send the message (and don't expect a response) // Send the message (and don't expect a response)
@ -631,13 +631,13 @@ static void lossStatsThreadFunc(void* context) {
while (!PltIsThreadInterrupted(&lossStatsThread)) { while (!PltIsThreadInterrupted(&lossStatsThread)) {
// Construct the payload // Construct the payload
BbInitializeWrappedBuffer(&byteBuffer, lossStatsPayload, 0, payloadLengths[IDX_LOSS_STATS], BYTE_ORDER_LITTLE); BbInitializeWrappedBuffer(&byteBuffer, lossStatsPayload, 0, payloadLengths[IDX_LOSS_STATS], BYTE_ORDER_LITTLE);
BbPutInt(&byteBuffer, lossCountSinceLastReport); BbPut32(&byteBuffer, lossCountSinceLastReport);
BbPutInt(&byteBuffer, LOSS_REPORT_INTERVAL_MS); BbPut32(&byteBuffer, LOSS_REPORT_INTERVAL_MS);
BbPutInt(&byteBuffer, 1000); BbPut32(&byteBuffer, 1000);
BbPutLong(&byteBuffer, lastGoodFrame); BbPut64(&byteBuffer, lastGoodFrame);
BbPutInt(&byteBuffer, 0); BbPut32(&byteBuffer, 0);
BbPutInt(&byteBuffer, 0); BbPut32(&byteBuffer, 0);
BbPutInt(&byteBuffer, 0x14); BbPut32(&byteBuffer, 0x14);
// Send the message (and don't expect a response) // Send the message (and don't expect a response)
if (!sendMessageAndForget(packetTypes[IDX_LOSS_STATS], if (!sendMessageAndForget(packetTypes[IDX_LOSS_STATS],