diff --git a/sysmodules/rosalina/include/sock_util.h b/sysmodules/rosalina/include/sock_util.h index 5e9e699..907311e 100644 --- a/sysmodules/rosalina/include/sock_util.h +++ b/sysmodules/rosalina/include/sock_util.h @@ -73,4 +73,5 @@ typedef struct sock_server Result server_init(struct sock_server *serv); void server_bind(struct sock_server *serv, u16 port); void server_run(struct sock_server *serv); +void server_kill_connections(struct sock_server *serv); void server_finalize(struct sock_server *serv); diff --git a/sysmodules/rosalina/source/menus/debugger.c b/sysmodules/rosalina/source/menus/debugger.c index 94f6752..16df912 100644 --- a/sysmodules/rosalina/source/menus/debugger.c +++ b/sysmodules/rosalina/source/menus/debugger.c @@ -148,6 +148,7 @@ void DebuggerMenu_DisableDebugger(void) if(initialized) { svcSignalEvent(gdbServer.super.shall_terminate_event); + server_kill_connections(&gdbServer.super); res = MyThread_Join(&debuggerDebugThread, 5 * 1000 * 1000 * 1000LL); if(res == 0) res = MyThread_Join(&debuggerSocketThread, 5 * 1000 * 1000 * 1000LL); diff --git a/sysmodules/rosalina/source/minisoc.c b/sysmodules/rosalina/source/minisoc.c index 2b81159..8aa0e30 100644 --- a/sysmodules/rosalina/source/minisoc.c +++ b/sysmodules/rosalina/source/minisoc.c @@ -396,11 +396,6 @@ int socPoll(struct pollfd *fds, nfds_t nfds, int timeout) if(ret == 0) ret = _net_convert_error(cmdbuf[2]); - if(ret < 0) { - //errno = -ret; - return -1; - } - return ret; } diff --git a/sysmodules/rosalina/source/sock_util.c b/sysmodules/rosalina/source/sock_util.c index 34b89c7..5314696 100644 --- a/sysmodules/rosalina/source/sock_util.c +++ b/sysmodules/rosalina/source/sock_util.c @@ -148,13 +148,14 @@ void server_run(struct sock_server *serv) { struct pollfd *fds = serv->poll_fds; Handle handles[2] = { terminationRequestEvent, serv->shall_terminate_event }; + s32 idx = -1; serv->running = true; svcSignalEvent(serv->started_event); while(serv->running && !terminationRequest) { - s32 idx = -1; + idx = -1; if(svcWaitSynchronizationN(&idx, handles, 2, false, 0LL) == 0) goto abort_connections; @@ -170,11 +171,15 @@ void server_run(struct sock_server *serv) fds[i].revents = 0; int pollres = socPoll(fds, serv->nfds, 50); + idx = -1; + if(svcWaitSynchronizationN(&idx, handles, 2, false, 0LL) == 0) + goto abort_connections; + for(nfds_t i = 0; pollres > 0 && i < serv->nfds; i++) { struct sock_ctx *curr_ctx = serv->ctx_ptrs[i]; - if((fds[i].revents & POLLHUP) || curr_ctx->should_close) + if((fds[i].revents & (POLLHUP | POLLERR | POLLNVAL)) || curr_ctx->should_close) server_close_ctx(serv, curr_ctx); else if(fds[i].revents & POLLIN) @@ -228,6 +233,10 @@ void server_run(struct sock_server *serv) } } + idx = -1; + if(svcWaitSynchronizationN(&idx, handles, 2, false, 0LL) == 0) + goto abort_connections; + if(serv->compact_needed) compact(serv); } @@ -244,18 +253,32 @@ void server_run(struct sock_server *serv) return; abort_connections: - for(unsigned int i = 0; i < serv->nfds; i++) + server_kill_connections(serv); + + serv->running = false; + svcClearEvent(serv->started_event); +} + +void server_kill_connections(struct sock_server *serv) +{ + struct pollfd *fds = serv->poll_fds; + nfds_t nfds = serv->nfds; + + for(unsigned int i = 0; i < nfds; i++) { + if(fds[i].fd == -1) + continue; + struct linger linger; linger.l_onoff = 1; linger.l_linger = 0; socSetsockopt(fds[i].fd, SOL_SOCKET, SO_LINGER, &linger, sizeof(struct linger)); socClose(fds[i].fd); - } + fds[i].fd = -1; - serv->running = false; - svcClearEvent(serv->started_event); + serv->ctx_ptrs[i]->should_close = true; + } } void server_finalize(struct sock_server *serv)