From 39f3538f7ee56dab414d62201235f8427b4a9592 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Tue, 24 Nov 2020 12:15:01 +0100 Subject: Added userspace-based network timeout The network in my whole city is down right now, so I've done some error catching using timeouts etc without the kernel blocking everything. Not having internet is exhausting though :( --- apps/browser.c | 5 +++-- apps/server.c | 7 +++++-- 2 files changed, 8 insertions(+), 4 deletions(-) (limited to 'apps') diff --git a/apps/browser.c b/apps/browser.c index 7f4fc6e..157ddbd 100644 --- a/apps/browser.c +++ b/apps/browser.c @@ -148,11 +148,12 @@ void on_submit(void *event, struct element *box) struct element_label *c = code_label->data; struct socket *socket = net_open(S_TCP); - if (socket && net_connect(socket, ip, 80)) { + if (socket && net_connect(socket, ip, 80, NET_TIMEOUT)) { net_send(socket, query, strlen(query)); char buf[4096] = { 0 }; char parsed[4096] = { 0 }; - net_receive(socket, buf, 4096); + if (!net_receive(socket, buf, 4096, NET_TIMEOUT)) + return; parse(http_data(buf), 4096, parsed); l->text = parsed[0] ? parsed : http_data(buf); c->text = http_code(buf); diff --git a/apps/server.c b/apps/server.c index 4ab3369..6aca617 100644 --- a/apps/server.c +++ b/apps/server.c @@ -21,7 +21,8 @@ int main() socket->state = S_CONNECTED; char buf[4096] = { 0 }; - net_receive(socket, buf, 4096); + if (!net_receive(socket, buf, 4096, NET_NO_TIMEOUT)) + break; char path[128] = { 0 }; strcat(path, PATH); @@ -41,5 +42,7 @@ int main() net_close(socket); } - return 0; + print("Server closed!\n"); + + return 1; } -- cgit v1.2.3