From cf12a50a0177d124c117b0fb047289d7e55e62b4 Mon Sep 17 00:00:00 2001 From: DevMiner Date: Thu, 3 Aug 2023 14:42:59 +0200 Subject: [PATCH] refactor(network): use correct int*_t types for `send*` in NetworkConnection --- src/network/connection.cpp | 57 +++++++++++++++++++++----------------- src/network/connection.h | 6 ++-- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/src/network/connection.cpp b/src/network/connection.cpp index 1f60ad876..31a6939d2 100644 --- a/src/network/connection.cpp +++ b/src/network/connection.cpp @@ -89,7 +89,7 @@ bool Connection::endPacket() { MUST_TRANSFER_BOOL((innerPacketSize > 0)); m_IsBundle = false; - + if (m_BundlePacketInnerCount == 0) { sendPacketType(PACKET_BUNDLE); sendPacketNumber(); @@ -128,13 +128,13 @@ bool Connection::endBundle() { MUST_TRANSFER_BOOL(m_IsBundle); m_IsBundle = false; - + MUST_TRANSFER_BOOL((m_BundlePacketInnerCount > 0)); return endPacket(); } -size_t Connection::write(const uint8_t *buffer, size_t size) { +size_t Connection::write(const uint8_t* buffer, size_t size) { if (m_IsBundle) { if (m_BundlePacketPosition + size > sizeof(m_Packet)) { return 0; @@ -146,9 +146,7 @@ size_t Connection::write(const uint8_t *buffer, size_t size) { return m_UDP.write(buffer, size); } -size_t Connection::write(uint8_t byte) { - return write(&byte, 1); -} +size_t Connection::write(uint8_t byte) { return write(&byte, 1); } bool Connection::sendFloat(float f) { convert_to_chars(f, m_Buf); @@ -158,19 +156,19 @@ bool Connection::sendFloat(float f) { bool Connection::sendByte(uint8_t c) { return write(&c, 1) != 0; } -bool Connection::sendShort(uint16_t i) { +bool Connection::sendShort(int16_t i) { convert_to_chars(i, m_Buf); return write(m_Buf, sizeof(i)) != 0; } -bool Connection::sendInt(uint32_t i) { +bool Connection::sendInt(int32_t i) { convert_to_chars(i, m_Buf); return write(m_Buf, sizeof(i)) != 0; } -bool Connection::sendLong(uint64_t l) { +bool Connection::sendU64(uint64_t l) { convert_to_chars(l, m_Buf); return write(m_Buf, sizeof(l)) != 0; @@ -185,9 +183,9 @@ bool Connection::sendPacketNumber() { return true; } - uint64_t pn = m_PacketNumber++; + auto pn = m_PacketNumber++; - return sendLong(pn); + return sendU64(pn); } bool Connection::sendShortString(const char* str) { @@ -391,7 +389,7 @@ void Connection::sendTrackerDiscovery() { MUST(sendPacketType(PACKET_HANDSHAKE)); // Packet number is always 0 for handshake - MUST(sendLong(0)); + MUST(sendU64(0)); MUST(sendInt(BOARD)); // This is kept for backwards compatibility, // but the latest SlimeVR server will not initialize trackers @@ -511,7 +509,7 @@ void Connection::returnLastPacket(int len) { MUST(endPacket()); } -void Connection::updateSensorState(std::vector & sensors) { +void Connection::updateSensorState(std::vector& sensors) { if (millis() - m_LastSensorInfoPacketTimestamp <= 1000) { return; } @@ -525,7 +523,7 @@ void Connection::updateSensorState(std::vector & sensors) { } } -void Connection::maybeRequestFeatureFlags() { +void Connection::maybeRequestFeatureFlags() { if (m_ServerFeatures.isAvailable() || m_FeatureFlagsRequestAttempts >= 15) { return; } @@ -557,6 +555,8 @@ void Connection::searchForServer() { m_UDP.remotePort() ); m_Logger.traceArray("UDP packet contents: ", m_Packet, len); +#else + (void)len; #endif // Handshake is different, it has 3 in the first byte, not the 4th, and data @@ -571,9 +571,9 @@ void Connection::searchForServer() { m_ServerPort = m_UDP.remotePort(); m_LastPacketTimestamp = millis(); m_Connected = true; - + m_FeatureFlagsRequestAttempts = 0; - m_ServerFeatures = ServerFeatures { }; + m_ServerFeatures = ServerFeatures{}; statusManager.setStatus(SlimeVR::Status::SERVER_CONNECTING, false); ledManager.off(); @@ -603,7 +603,11 @@ void Connection::searchForServer() { void Connection::reset() { m_Connected = false; - std::fill(m_AckedSensorState, m_AckedSensorState+MAX_IMU_COUNT, SensorStatus::SENSOR_OFFLINE); + std::fill( + m_AckedSensorState, + m_AckedSensorState + MAX_IMU_COUNT, + SensorStatus::SENSOR_OFFLINE + ); m_UDP.begin(m_ServerPort); @@ -611,7 +615,7 @@ void Connection::reset() { } void Connection::update() { - std::vector & sensors = sensorManager.getSensors(); + std::vector& sensors = sensorManager.getSensors(); updateSensorState(sensors); maybeRequestFeatureFlags(); @@ -625,7 +629,11 @@ void Connection::update() { statusManager.setStatus(SlimeVR::Status::SERVER_CONNECTING, true); m_Connected = false; - std::fill(m_AckedSensorState, m_AckedSensorState+MAX_IMU_COUNT, SensorStatus::SENSOR_OFFLINE); + std::fill( + m_AckedSensorState, + m_AckedSensorState + MAX_IMU_COUNT, + SensorStatus::SENSOR_OFFLINE + ); m_Logger.warn("Connection to server timed out"); return; @@ -697,16 +705,15 @@ void Connection::update() { } bool hadFlags = m_ServerFeatures.isAvailable(); - uint32_t flagsLength = len - 12; m_ServerFeatures = ServerFeatures::from(&m_Packet[12], flagsLength); if (!hadFlags) { - #if PACKET_BUNDLING != PACKET_BUNDLING_DISABLED - if (m_ServerFeatures.has(ServerFeatures::PROTOCOL_BUNDLE_SUPPORT)) { - m_Logger.debug("Server supports packet bundling"); - } - #endif +#if PACKET_BUNDLING != PACKET_BUNDLING_DISABLED + if (m_ServerFeatures.has(ServerFeatures::PROTOCOL_BUNDLE_SUPPORT)) { + m_Logger.debug("Server supports packet bundling"); + } +#endif } break; diff --git a/src/network/connection.h b/src/network/connection.h index 1231ee247..670c974db 100644 --- a/src/network/connection.h +++ b/src/network/connection.h @@ -128,9 +128,9 @@ class Connection { bool sendPacketNumber(); bool sendFloat(float f); bool sendByte(uint8_t c); - bool sendShort(uint16_t i); - bool sendInt(uint32_t i); - bool sendLong(uint64_t l); + bool sendShort(int16_t i); + bool sendInt(int32_t i); + bool sendU64(uint64_t l); bool sendBytes(const uint8_t* c, size_t length); bool sendShortString(const char* str); bool sendLongString(const char* str);