-Subproject commit 39b52e8cfa889d55008f2edd0933d6b421556c34
+Subproject commit 4e1758fbf2261950df0d323460e7fbf35d402dd5
"${DEPS_DIR}/dragonport/asprintf.c"
"${DEPS_DIR}/dragonstd/array.c"
"${DEPS_DIR}/dragonstd/bintree.c"
+ "${DEPS_DIR}/dragonstd/flag.c"
"${DEPS_DIR}/dragonstd/list.c"
"${DEPS_DIR}/dragonstd/queue.c"
"${DEPS_DIR}/linenoise/linenoise.c"
config.c
day.c
environment.c
+ interrupt.c
map.c
node.c
perlin.c
- signal_handlers.c
types.c
util.c
)
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
+#include <dragonstd/flag.h>
#include "client/client.h"
#include "client/client_auth.h"
#include "client/client_map.h"
#include "client/game.h"
#include "client/input.h"
#include "day.h"
-#include "signal_handlers.h"
+#include "interrupt.h"
#include "perlin.h"
#include "types.h"
#include "util.h"
DragonnetPeer *client;
-
-static volatile bool finished = false;
+static Flag *finish;
static bool on_recv(unused DragonnetPeer *peer, DragonnetTypeId type, unused void *pkt)
{
- while (client_auth.state == AUTH_INIT)
- sched_yield();
-
return (client_auth.state == AUTH_WAIT) == (type == DRAGONNET_TYPE_ToClientAuth);
}
static void on_disconnect(unused DragonnetPeer *peer)
{
- interrupted = true;
-
- while (! finished)
- sched_yield();
+ flag_set(interrupt);
+ flag_wait(finish);
}
static void on_ToClientAuth(unused DragonnetPeer *peer, ToClientAuth *pkt)
client->on_recv_type[DRAGONNET_TYPE_ToClientPos ] = (void *) &on_ToClientPos;
client->on_recv_type[DRAGONNET_TYPE_ToClientTimeOfDay] = (void *) &on_ToClientTimeOfDay;
- signal_handlers_init();
+ finish = flag_create();
+
+ interrupt_init();
client_map_init();
client_player_init();
dragonnet_peer_run(client);
client_auth_deinit();
client_player_deinit();
client_map_deinit();
+ interrupt_deinit();
pthread_t recv_thread = client->recv_thread;
- finished = true;
+
+ flag_set(finish);
pthread_join(recv_thread, NULL);
+ flag_delete(finish);
+
return EXIT_SUCCESS;
}
#include <linenoise/linenoise.h>
#include "client.h"
#include "client_auth.h"
-#include "signal_handlers.h"
+#include "interrupt.h"
#include "types.h"
volatile struct ClientAuth client_auth;
if (! (client_auth.name = linenoise("Enter name: ")))
return false;
+ printf("Authenticating as %s...\n", client_auth.name);
+ client_auth.state = AUTH_WAIT;
+
dragonnet_peer_send_ToServerAuth(client, &(ToServerAuth) {
.name = client_auth.name,
});
- printf("Authenticating as %s...\n", client_auth.name);
- client_auth.state = AUTH_WAIT;
-
return true;
}
{
client_auth.state = AUTH_INIT;
- while (! interrupted) {
+ while (! interrupt->done) {
switch (client_auth.state) {
case AUTH_INIT:
if (name_prompt())
#include "client/sky.h"
#include "client/window.h"
#include "day.h"
-#include "signal_handlers.h"
+#include "interrupt.h"
int window_width, window_height;
struct timespec ts, ts_old;
clock_gettime(CLOCK_REALTIME, &ts_old);
- while (! glfwWindowShouldClose(window.handle) && ! interrupted) {
+ while (! glfwWindowShouldClose(window.handle) && ! interrupt->done) {
clock_gettime(CLOCK_REALTIME, &ts);
f64 dtime = (f64) (ts.tv_sec - ts_old.tv_sec) + (f64) (ts.tv_nsec - ts_old.tv_nsec) / 1.0e9;
ts_old = ts;
--- /dev/null
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+#include "interrupt.h"
+
+Flag *interrupt;
+static struct sigaction sa = {0};
+
+static void interrupt_handler(int sig)
+{
+ fprintf(stderr, "%s\n", strsignal(sig));
+ flag_set(interrupt);
+}
+
+void interrupt_init()
+{
+ interrupt = flag_create();
+
+ sa.sa_handler = &interrupt_handler;
+ sigaction(SIGINT, &sa, NULL);
+ sigaction(SIGTERM, &sa, NULL);
+}
+
+void interrupt_deinit()
+{
+ flag_delete(interrupt);
+}
--- /dev/null
+#ifndef _INTERRUPT_H_
+#define _INTERRUPT_H_
+
+#include <dragonstd/flag.h>
+
+extern Flag *interrupt;
+
+void interrupt_init();
+void interrupt_deinit();
+
+#endif
#include <stdio.h>
#include <stdlib.h>
#include <dragonnet/addr.h>
+#include "interrupt.h"
#include "server/database.h"
#include "server/server.h"
#include "server/server_map.h"
#include "server/server_player.h"
-#include "signal_handlers.h"
#include "util.h"
DragonnetListener *server;
server->on_recv_type[DRAGONNET_TYPE_ToServerPos] = (void *) &on_ToServerPos;
server->on_recv_type[DRAGONNET_TYPE_ToServerRequestBlock] = (void *) &on_ToServerRequestBlock;
- signal_handlers_init();
-
+ interrupt_init();
server_player_init();
database_init();
server_map_init();
+
server_map_prepare_spawn();
dragonnet_listener_run(server);
- while (! interrupted)
- sched_yield();
+ flag_wait(interrupt);
printf("Shutting down\n");
-
dragonnet_listener_close(server);
+
server_map_deinit();
database_deinit();
server_player_deinit();
+ interrupt_deinit();
dragonnet_listener_delete(server);
#include <string.h>
#include <unistd.h>
#include <stdio.h>
+#include "interrupt.h"
#include "map.h"
#include "server/database.h"
#include "server/mapgen.h"
#include "server/server_config.h"
#include "server/server_map.h"
-#include "signal_handlers.h"
#include "util.h"
struct ServerMap server_map;
for (s32 x = -dist; x <= (s32) dist; x++) {
for (s32 y = -dist; y <= (s32) dist; y++) {
for (s32 z = -dist; z <= (s32) dist; z++) {
- if (interrupted) {
+ if (interrupt->done) {
join_mapgen_threads();
return;
}
+++ /dev/null
-#include <signal.h>
-#include <stdio.h>
-#include <string.h>
-#include "signal_handlers.h"
-#include "util.h"
-
-bool interrupted = false;
-
-static void interrupt_handler(int sig)
-{
- interrupted = true;
- fprintf(stderr, "%s\n", strsignal(sig));
-}
-
-static void silent_handler(unused int sig)
-{
-}
-
-static struct sigaction sigact_interrupt = {0};
-static struct sigaction sigact_silent = {0};
-
-void signal_handlers_init()
-{
- sigact_interrupt.sa_handler = &interrupt_handler;
- sigaction(SIGINT, &sigact_interrupt, NULL);
- sigaction(SIGTERM, &sigact_interrupt, NULL);
-
- sigact_silent.sa_handler = &silent_handler;
- sigaction(SIGPIPE, &sigact_silent, NULL);
-}
+++ /dev/null
-#ifndef _SIGNAL_HANDLERS_H_
-#define _SIGNAL_HANDLERS_H_
-
-#include <stdbool.h>
-
-extern bool interrupted;
-void signal_handlers_init();
-
-#endif
#ifndef _UTIL_H_
#define _UTIL_H_
-#define ever (;;) // infinite for loop with style
-#define INBRACES(str) (str) ? "(" : "", (str) ? (str) : "", (str) ? ")" : "" // wrapper for printf to optionally add a message in braces if message is not NULL
#define CMPBOUNDS(x) ((x) == 0 ? 0 : (x) > 0 ? 1 : -1) // resolves to 1 if x > 0, 0 if x == 0 and -1 if x < 0
#define fallthrough __attribute__ ((fallthrough)) // prevent compiler warning about implicit fallthrough with style
#define unused __attribute__ ((unused))