Updating error checking, canSend is not reliable anymore

This commit is contained in:
Rodrigo Alfonso 2023-02-10 20:10:50 -03:00
parent b1196edfa6
commit 1197883f7a
2 changed files with 18 additions and 9 deletions

View File

@ -240,22 +240,30 @@ void messageLoop() {
u16 keys = ~REG_KEYS & KEY_ANY;
// (6) Send data
if (linkWireless->canSend() &&
((keys & KEY_B) || (!sending && (keys & KEY_A)))) {
if ((keys & KEY_B) || (!sending && (keys & KEY_A))) {
bool doubleSend = false;
sending = true;
again:
counters[linkWireless->currentPlayerId()]++;
linkWireless->send(
std::vector<u32>{counters[linkWireless->currentPlayerId()]});
CHECK_ERRORS("Send failed :(")
u32 newValue = counters[linkWireless->currentPlayerId()] + 1;
bool success = linkWireless->send(std::vector<u32>{newValue});
if (!doubleSend && (keys & KEY_LEFT) && linkWireless->canSend()) {
if (success) {
counters[linkWireless->currentPlayerId()] = newValue;
} else {
if (linkWireless->getLastError(false) == LinkWireless::BUFFER_IS_FULL) {
linkWireless->getLastError();
goto sendEnd;
}
CHECK_ERRORS("Send failed :(")
}
if (!doubleSend && (keys & KEY_LEFT)) {
doubleSend = true;
goto again;
}
}
sendEnd:
if (sending && (!(keys & KEY_A)))
sending = false;

View File

@ -429,9 +429,10 @@ class LinkWireless {
u8 currentPlayerId() { return sessionState.currentPlayerId; }
bool canSend() { return !sessionState.outgoingMessages.isFull(); }
u32 getPendingCount() { return sessionState.outgoingMessages.size(); }
Error getLastError() {
Error getLastError(bool clear = true) {
Error error = lastError;
lastError = NONE;
if (clear)
lastError = NONE;
return error;
}