Skip to content

Commit

Permalink
refactor(network): use correct int*_t types for send* in NetworkCon…
Browse files Browse the repository at this point in the history
…nection
  • Loading branch information
TheDevMinerTV committed Oct 3, 2023
1 parent 9968f15 commit cf12a50
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
57 changes: 32 additions & 25 deletions src/network/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ bool Connection::endPacket() {
MUST_TRANSFER_BOOL((innerPacketSize > 0));

m_IsBundle = false;

if (m_BundlePacketInnerCount == 0) {
sendPacketType(PACKET_BUNDLE);
sendPacketNumber();
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -511,7 +509,7 @@ void Connection::returnLastPacket(int len) {
MUST(endPacket());
}

void Connection::updateSensorState(std::vector<Sensor *> & sensors) {
void Connection::updateSensorState(std::vector<Sensor*>& sensors) {
if (millis() - m_LastSensorInfoPacketTimestamp <= 1000) {
return;
}
Expand All @@ -525,7 +523,7 @@ void Connection::updateSensorState(std::vector<Sensor *> & sensors) {
}
}

void Connection::maybeRequestFeatureFlags() {
void Connection::maybeRequestFeatureFlags() {
if (m_ServerFeatures.isAvailable() || m_FeatureFlagsRequestAttempts >= 15) {
return;
}
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -603,15 +603,19 @@ 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);

statusManager.setStatus(SlimeVR::Status::SERVER_CONNECTING, true);
}

void Connection::update() {
std::vector<Sensor *> & sensors = sensorManager.getSensors();
std::vector<Sensor*>& sensors = sensorManager.getSensors();

updateSensorState(sensors);
maybeRequestFeatureFlags();
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/network/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit cf12a50

Please sign in to comment.