FIX: Overflow handling when forwarding messages

This commit is contained in:
Rodrigo Alfonso
2025-02-03 08:12:51 -03:00
parent b88b0271df
commit 6f2ec52e8d
2 changed files with 19 additions and 13 deletions

View File

@@ -305,7 +305,7 @@ You can update these values at any time without creating a new instance:
- This affects how much memory is allocated. With the default value, it's around `480` bytes. There's a double-buffered incoming queue and a double-buffered outgoing queue (to avoid data races).
- You can approximate the memory usage with:
- `LINK_WIRELESS_QUEUE_SIZE * sizeof(Message) * 4` <=> `LINK_WIRELESS_QUEUE_SIZE * 16`
- `LINK_WIRELESS_MAX_SERVER_TRANSFER_LENGTH` and `LINK_WIRELESS_MAX_CLIENT_TRANSFER_LENGTH`: to set the biggest allowed transfer per timer tick. Higher values will use the bandwidth more efficiently but consume more CPU! These values must be in the range `[6;21]` for servers and `[2;4]` for clients. The default values are `11` and `4`, but you might want to set them a bit lower to reduce CPU usage.
- `LINK_WIRELESS_MAX_SERVER_TRANSFER_LENGTH` and `LINK_WIRELESS_MAX_CLIENT_TRANSFER_LENGTH`: to set the biggest allowed transfer per timer tick. Higher values will use the bandwidth more efficiently but also consume more CPU! These values must be in the range `[6;21]` for servers and `[2;4]` for clients. The default values are `11` and `4`, but you might want to set them a bit lower to reduce CPU usage.
- This is measured in words (1 message = 1 halfword). One word is used as a header, so a max transfer length of 11 could transfer up to 20 messages.
- `LINK_WIRELESS_PUT_ISR_IN_IWRAM`: to put critical functions in IWRAM, which can significantly improve performance due to its faster access. This is disabled by default to conserve IWRAM space, which is limited, but it's enabled in demos to showcase its performance benefits.
- If you enable this, make sure that `LinkWireless.cpp` gets compiled! For example, in a Makefile-based project, verify that the file is in your `SRCDIRS` list.

View File

@@ -73,7 +73,7 @@
/**
* @brief Max server transfer length per timer tick. Must be in the range
* `[6;21]`. The default value is `11`. Higher values will use the bandwidth
* more efficiently but consume more CPU!
* more efficiently but also consume more CPU!
* \warning This is measured in words (1 message = 1 halfword). One word is used
* as a header, so a max transfer length of 11 could transfer up to 20 messages.
*/
@@ -1163,7 +1163,7 @@ class LinkWireless {
&firstPacketId, &firstMsg, &msgCount, &highPart,
&pendingForwardedCount, &currentPlayerBitMapIndex,
&playerBitMapCount](Message* message) {
// create packet id if the packet can be sent
// create packet ID if the packet can be sent
if (message->packetId == NO_ID_ASSIGNED_YET) {
if (sessionState.inflightCount < maxInflightPackets) {
message->packetId = newPacketId(maxPacketIds);
@@ -1173,7 +1173,7 @@ class LinkWireless {
}
}
// get first added packet id and add first msg if needed
// get first added packet ID and add first msg if needed
if (firstPacketId == NO_ID_ASSIGNED_YET) {
firstPacketId = message->packetId;
if (!isServer) {
@@ -1248,7 +1248,7 @@ class LinkWireless {
transferHeader.playerCount = sessionState.localHeartbeat;
}
// first packet id, or 0 if there are no messages
// first packet ID, or 0 if there are no messages
if (msgCount > 0)
transferHeader.firstPacketId = firstPacketId;
@@ -1460,7 +1460,7 @@ class LinkWireless {
u32 packetId = sessionState.outgoingMessages.peek().packetId;
// if the current message is not inflight, we've entered the section of
// 'new' messages (with no id assigned), so we quit!
// 'new' messages (with no ID assigned), so we quit!
if (packetId == NO_ID_ASSIGNED_YET)
break;
@@ -1482,7 +1482,7 @@ class LinkWireless {
u32& currentPacketId,
u32& playerBitMap,
int& playerBitMapCount) { // (irq only)
// store the packet id and increment (msgs are consecutive inside transfers)
// store the packet ID and increment (msgs are consecutive inside transfers)
u32 packetId = currentPacketId;
currentPacketId =
(currentPacketId + 1) %
@@ -1506,7 +1506,7 @@ class LinkWireless {
sessionState.lastPacketIdFromServer = packetId;
sessionState.didReceiveFirstPacketFromServer = true;
} else {
// if retransmission is enabled, the packet id needs to be expected
// if retransmission is enabled, the packet ID needs to be expected
if (config.retransmission) {
u32 expectedPacketId =
playerId > 0
@@ -1538,13 +1538,19 @@ class LinkWireless {
// forward to other clients if needed
if (playerId > 0 && config.forwarding &&
linkRawWireless.sessionState.playerCount > 2) {
Message forwardedMessage;
forwardedMessage.data = message.data;
forwardedMessage.playerId = message.playerId;
linkRawWireless.sessionState.playerCount > 2)
forwardMessage(message);
}
void forwardMessage(Message& message) { // (irq only)
Message forwardedMessage;
forwardedMessage.data = message.data;
forwardedMessage.playerId = message.playerId;
if (!sessionState.outgoingMessages.isFull()) {
sessionState.outgoingMessages.push(forwardedMessage);
sessionState.forwardedCount++;
}
} else
sessionState.outgoingMessages.overflow = true;
}
bool checkRemoteTimeouts() { // (irq only)