Skip to content

Commit

Permalink
server: ipc via stdout
Browse files Browse the repository at this point in the history
  • Loading branch information
LizzyFleckenstein03 committed Aug 23, 2024
1 parent 90397d4 commit aab05e6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
36 changes: 29 additions & 7 deletions dragonblocks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ load_version() {
;;

/*)
version_exec_dir="$1/"
version_exec_dir="./"
version_working_dir="$1"
;;

Expand Down Expand Up @@ -79,16 +79,27 @@ launch() {
local which="$1"
local config="$(realpath $DRAGONBLOCKS_CONFIG/$which.conf)"
local log="$(realpath $log_path/$which-$(timestamp).log)"

shift

cd "$version_working_dir"
stdbuf -o0 "${version_exec_dir}dragonblocks-$which" --config "$config" $@ |& tee -i "$log"
exec 3> >(tee -i "$log" >&2)
"${version_exec_dir}dragonblocks-$which" --config "$config" $@ 2>&3
}

unlaunch() {
pkill -g 0 -f dragonblocks-$1
}

server_ipc() {
while read -ra cmd; do
case "${cmd[0]}" in
listen)
echo "${cmd[@]:1}" > "$1"
;;
esac
done
}

case $1 in
help|--help)
cat << EOF
Expand Down Expand Up @@ -143,8 +154,16 @@ EOF

trap "unlaunch server" SIGINT SIGTERM

launch server --world "$(realpath $world)" --write-address "$addrfile" "${4:-::1:}" &
launch client --screenshot-dir "$(realpath $screenshot_path)" "${3:-singleplayer}" "$(<$addrfile)"
launch server \
--world "$(realpath $world)" \
--ipc \
"${4:-::1:}" \
| server_ipc "$addrfile" &

launch client \
--screenshot-dir "$(realpath $screenshot_path)" \
"${3:-singleplayer}" \
"$(<$addrfile)"

unlaunch server
;;
Expand All @@ -154,7 +173,9 @@ EOF

trap "unlaunch client" SIGINT SIGTERM

launch client --screenshot-dir "$(realpath $screenshot_path)" "${2:?missing player (see '$script_name --help')}" \
launch client \
--screenshot-dir "$(realpath $screenshot_path)" \
"${2:?missing player (see '$script_name --help')}" \
"${3:?missing address (see '$script_name --help')}"
;;

Expand All @@ -164,7 +185,8 @@ EOF

trap "unlaunch server" SIGINT SIGTERM

launch server --world "$(realpath $world)" \
launch server \
--world "$(realpath $world)" \
"${3:?missing address (see '$script_name --help')}"
;;

Expand Down
3 changes: 3 additions & 0 deletions src/common/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

void dragonblocks_init()
{
setvbuf(stdout, NULL, _IOLBF, 0);
setvbuf(stderr, NULL, _IOLBF, 0);

#ifdef __GLIBC__ // check whether bloat is enabled
pthread_setname_np(pthread_self(), "main");
#endif // __GLIBC__
Expand Down
24 changes: 7 additions & 17 deletions src/server/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,21 @@ int main(int argc, char **argv)

char *config_path = "server.conf";
char *world_path = ".";
char *write_address = NULL;
bool ipc = false;

struct option long_options[] = {
{"config", required_argument, 0, 'c' },
{"world", required_argument, 0, 'w' },
{"write-address", required_argument, 0, 'a' },
{"config", required_argument, 0, 'c' },
{"world", required_argument, 0, 'w' },
{"ipc", no_argument, 0, 'i' },
{}
};

int option;
while ((option = getopt_long(argc, argv, "c:w:a:", long_options, NULL)) != -1) {
while ((option = getopt_long(argc, argv, "c:w:i", long_options, NULL)) != -1) {
switch (option) {
case 'c': config_path = optarg; break;
case 'w': world_path = optarg; break;
case 'a': write_address = optarg; break;
case 'i': ipc = true; break;
}
}

Expand All @@ -94,17 +94,7 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}

if (write_address) {
FILE *addrfile = fopen(write_address, "a");
if (!addrfile) {
fprintf(stderr, "[error] failed to open address file\n");
return EXIT_FAILURE;
}

fprintf(addrfile, "%s\n", server->address);
fclose(addrfile);
}

if (ipc) printf("listen %s\n", server->address);
fprintf(stderr, "[info] listening on %s\n", server->address);

server->on_connect = &server_player_add;
Expand Down

0 comments on commit aab05e6

Please sign in to comment.