Skip to content

Commit 2a69fb0

Browse files
committed
Let compiler handle endianness for headers
1 parent b78768b commit 2a69fb0

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

src/bridge/BridgeTransport.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ void BridgeTransport::OnRecv(const uvw::data_event& event) {
8080

8181
char len_buf[4];
8282
recv_buf_.Peek(len_buf, 4);
83-
uint32_t size = LE32_TO_NATIVE(*reinterpret_cast<uint32_t*>(len_buf));
83+
uint32_t size = 0;
84+
size |= static_cast<uint32_t>(len_buf[0]) << 0;
85+
size |= static_cast<uint32_t>(len_buf[1]) << 8;
86+
size |= static_cast<uint32_t>(len_buf[2]) << 16;
87+
size |= static_cast<uint32_t>(len_buf[3]) << 24;
8488

8589
if (size > VRBRIDGE_MAX_MESSAGE_SIZE) {
8690
logger_->Log("message size overflow");
@@ -116,8 +120,12 @@ void BridgeTransport::SendBridgeMessage(const messages::ProtobufMessage& message
116120
uint32_t wrapped_size = size + 4;
117121

118122
auto message_buf = std::make_unique<char[]>(wrapped_size);
119-
*reinterpret_cast<uint32_t*>(message_buf.get()) = NATIVE_TO_LE32(wrapped_size);
123+
message_buf.get()[0] = (wrapped_size >> 0) & 0xFF;
124+
message_buf.get()[1] = (wrapped_size >> 8) & 0xFF;
125+
message_buf.get()[2] = (wrapped_size >> 16) & 0xFF;
126+
message_buf.get()[3] = (wrapped_size >> 24) & 0xFF;
120127
message.SerializeToArray(message_buf.get() + 4, size);
128+
121129
if (!send_buf_.Push(message_buf.get(), wrapped_size)) {
122130
ResetConnection();
123131
return;

src/bridge/BridgeTransport.hpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,6 @@
3131
#include "CircularBuffer.hpp"
3232
#include "ProtobufMessages.pb.h"
3333

34-
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
35-
#define LE32_TO_NATIVE(x) (x)
36-
#else
37-
#define LE32_TO_NATIVE(x) ( \
38-
((uint32_t)(x) << 24) | \
39-
(((uint32_t)(x) << 8) & 0x00FF0000) | \
40-
(((uint32_t)(x) >> 8) & 0x0000FF00) | \
41-
((uint32_t)(x) >> 24) \
42-
)
43-
#endif
44-
#define NATIVE_TO_LE32 LE32_TO_NATIVE
45-
4634
#define VRBRIDGE_MAX_MESSAGE_SIZE 1024
4735
#define VRBRIDGE_BUFFERS_SIZE 8192
4836

0 commit comments

Comments
 (0)