Adding onWait callback to getServers

This commit is contained in:
Rodrigo Alfonso
2023-02-05 05:29:51 -03:00
parent aa4652337d
commit 03fb983073
3 changed files with 31 additions and 7 deletions

View File

@@ -170,7 +170,8 @@ Name | Return type | Description
`acceptConnections()` | **bool** | Accepts new clients and updates the player count.
`connect(serverId)` | **bool** | Starts a connection with `serverId` and changes the state to `CONNECTING`.
`keepConnecting()` | **bool** | When connecting, needs to be called until the state is `CONNECTED`. It assigns a player id.
`getServers(servers)` | **bool** | Fills the `servers` vector with all the currently broadcasting servers.
`getServers(servers)` | **bool** | Fills the `servers` vector with all the currently broadcasting servers. This action takes 1 second to complete.
`getServers(servers, onWait)` | **bool** | Like `getServers(servers)`, but accepts an `onWait` function. The library will continuously invoke it each time VBlank starts, to let the user do something while waiting (like updating the screen).
`send(data)` | **bool** | Enqueues `data` to be sent to other nodes. Note that this data will be sent in the next `receive(...)` call.
`receive(messages)` | **bool** | Sends the pending data and fills the `messages` vector with incoming messages, checking for timeouts and forwarding if needed. This call doesn't block the hardware waiting for messages, it returns if there are no incoming messages.
`receive(messages, times)` | **bool** | Performs multiple `receive(...)` calls until successfully exchanging data a number of `times`. This can only be called if `retransmission` is on.

View File

@@ -1,4 +1,5 @@
#include <tonc.h>
#include <functional>
#include <string>
// (0) Include the header
@@ -129,7 +130,7 @@ void serve() {
do {
u16 keys = ~REG_KEYS & KEY_ANY;
if (keys & KEY_SELECT) {
log("Canceled");
log("Canceled!");
linkWireless->disconnect();
hang();
return;
@@ -145,11 +146,20 @@ void serve() {
}
void connect() {
log("Searching...");
u32 dotsCount, timer = 0;
std::function<void()> animate = [&dotsCount, &timer]() {
if (timer++ % 10 == 0)
dotsCount = 1 + (dotsCount) % 3;
std::string dots = "";
for (u32 i = 0; i < dotsCount; i++)
dots += ".";
log("Searching" + dots);
};
// (4) Connect to a server
std::vector<LinkWireless::Server> servers;
linkWireless->getServers(servers);
linkWireless->getServers(servers, animate);
CHECK_ERRORS("Search failed :(")
if (servers.size() == 0) {
@@ -181,7 +191,7 @@ void connect() {
while (linkWireless->getState() == LinkWireless::State::CONNECTING) {
u16 keys = ~REG_KEYS & KEY_ANY;
if (keys & KEY_SELECT) {
log("Canceled");
log("Canceled!");
linkWireless->disconnect();
hang();
return;

View File

@@ -238,6 +238,11 @@ class LinkWireless {
}
bool getServers(std::vector<Server>& servers) {
return getServers(servers, []() {});
}
template <typename F>
bool getServers(std::vector<Server>& servers, F onWait) {
LINK_WIRELESS_RESET_IF_NEEDED
if (state != AUTHENTICATED) {
lastError = WRONG_STATE;
@@ -253,7 +258,7 @@ class LinkWireless {
return false;
}
wait(LINK_WIRELESS_BROADCAST_SEARCH_WAIT);
wait(LINK_WIRELESS_BROADCAST_SEARCH_WAIT, onWait);
auto result = sendCommand(LINK_WIRELESS_COMMAND_BROADCAST_READ_POLL);
bool success2 =
@@ -968,13 +973,21 @@ class LinkWireless {
}
void wait(u32 verticalLines) {
wait(verticalLines, []() {});
}
template <typename F>
void wait(u32 verticalLines, F onVBlank) {
u32 lines = 0;
u32 vCount = REG_VCOUNT;
while (lines < verticalLines) {
if (REG_VCOUNT != vCount) {
lines++;
lines += std::max((s32)REG_VCOUNT - (s32)vCount, 0);
vCount = REG_VCOUNT;
if (REG_VCOUNT == 160)
onVBlank();
}
};
}