Merge pull request #2 from s0liton/v1.0.1-rest-mode-hotfix

This commit is contained in:
Pedro Santos committed 2026-10-02 09:28:21 -04:00
commit c989b2e10d
3 files changed
+87 -23

No files matched your search

+25 -2
View File
@@ -22,6 +22,12 @@
#include "web.h"
#define MONITOR_INTERVAL_SEC 3
/* The loop ticks every few seconds, so a longer gap means the console slept. */
#define RESUME_GAP_SEC 60
/* Ticks to wait for the network after waking before notifying anyway. */
#define RESUME_NOTIFY_TICKS 10
/* Both resume signals usually fire on the same wake; notify only once. */
#define RESUME_NOTIFY_QUIET_SEC 120
/* System apps, the browser and our own tile don't count as games. */
static int is_game_title(const char *t) {
@@ -112,11 +118,28 @@ int main(void) {
sync_request("startup");
char current[32] = "";
time_t game_start = 0, exit_at = 0, now;
int have_title = 1;
time_t game_start = 0, exit_at = 0, now, last_tick = time(NULL), resume_notified = 0;
int have_title = 1, resume_ticks = 0;
for (;;) {
sleep(MONITOR_INTERVAL_SEC);
now = time(NULL);
int resumed = 0;
if (now - last_tick > RESUME_GAP_SEC) {
LOGI("resumed from rest mode after %lds", (long)(now - last_tick));
resumed = 1;
}
if (web_take_reopened()) resumed = 1;
last_tick = now;
if (resumed && !resume_ticks && now - resume_notified > RESUME_NOTIFY_QUIET_SEC)
resume_ticks = RESUME_NOTIFY_TICKS;
if (resume_ticks > 0) {
plat_local_ip(ip, sizeof ip);
if (ip[0] || --resume_ticks == 0) {
resume_ticks = 0;
resume_notified = now;
plat_notify("RomM Sync resumed - http://%s:%d", ip[0] ? ip : "127.0.0.1", port);
}
}
char title[32] = "";
int fg = have_title ? plat_running_title(title, sizeof title) : -1;
+59 -21
View File
@@ -622,23 +622,7 @@ static void *conn_thread(void *arg) {
return NULL;
}
static void *accept_thread(void *arg) {
int srv = (int)(long)arg;
for (;;) {
int fd = accept(srv, NULL, NULL);
if (fd < 0) {
if (errno == EINTR) continue;
LOGE("accept failed: %s", strerror(errno));
sleep(1);
continue;
}
if (thread_start(conn_thread, (void *)(long)fd) != 0) close(fd);
}
return NULL;
}
int web_start(int port) {
g_started = time(NULL);
static int open_listener(int port) {
int srv = socket(AF_INET, SOCK_STREAM, 0);
if (srv < 0) return -1;
int one = 1;
@@ -649,11 +633,65 @@ int web_start(int port) {
a.sin_port = htons((uint16_t)port);
a.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(srv, (struct sockaddr *)&a, sizeof a) != 0 || listen(srv, 16) != 0) {
LOGE("cannot listen on port %d: %s", port, strerror(errno));
int err = errno;
close(srv);
errno = err;
return -1;
}
thread_start(accept_thread, (void *)(long)srv);
LOGI("web UI listening on port %d", port);
return 0;
return srv;
}
static int g_port, g_srv = -1, g_reopened;
int web_take_reopened(void) {
return __atomic_exchange_n(&g_reopened, 0, __ATOMIC_SEQ_CST);
}
/* Issue #1 - Rest mode tears down the console's sockets, and the listener never accepts
* again after wake-up. When accept keeps failing, open a fresh listener (ftpsrv
* does the same). A single failure (an aborted connection, running out of fds)
* is not enough, or we would announce a resume that never happened. */
#define ACCEPT_FAILS_BEFORE_REOPEN 3
static void *accept_thread(void *arg) {
(void)arg;
int fails = 0;
for (;;) {
if (g_srv < 0) {
g_srv = open_listener(g_port);
if (g_srv < 0) {
sleep(3);
continue;
}
LOGI("web UI listening again on port %d", g_port);
__atomic_store_n(&g_reopened, 1, __ATOMIC_SEQ_CST);
}
int fd = accept(g_srv, NULL, NULL);
if (fd < 0) {
if (errno == EINTR) continue;
if (++fails < ACCEPT_FAILS_BEFORE_REOPEN) {
LOGW("accept failed: %s", strerror(errno));
usleep(250 * 1000);
continue;
}
LOGW("accept failed (%s), reopening the listener", strerror(errno));
close(g_srv);
g_srv = -1;
fails = 0;
continue;
}
fails = 0;
if (thread_start(conn_thread, (void *)(long)fd) != 0) close(fd);
}
return NULL;
}
int web_start(int port) {
g_started = time(NULL);
g_port = port;
g_srv = open_listener(port);
if (g_srv < 0) LOGE("cannot listen on port %d: %s; retrying", port, strerror(errno));
else LOGI("web UI listening on port %d", port);
thread_start(accept_thread, NULL);
return g_srv < 0 ? -1 : 0;
}
+3
View File
@@ -7,4 +7,7 @@ int web_start(int port);
/* Foreground title shown on the status page. */
void web_set_foreground(const char *title_id);
/* 1 once after the listener had to be reopened (e.g. after rest mode), else 0. */
int web_take_reopened(void);
#endif