X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=plomrogue-server.py;h=cd54c86965c46d9cdd87530f2948d15c8df61e16;hb=2b4005f1f4b4ca64d86af9ac35a92d60f6e7bc48;hp=b921f4baa0ee9c6e8c3a122270f65ab718dfe73d;hpb=958b6c83c660169d9f0dfc15079a413e05b89197;p=plomrogue diff --git a/plomrogue-server.py b/plomrogue-server.py index b921f4b..cd54c86 100755 --- a/plomrogue-server.py +++ b/plomrogue-server.py @@ -6,6 +6,13 @@ import shutil import time +def strong_write(file, string): + """Apply write(string), flush() and os.fsync() to file.""" + file.write(string) + file.flush() + os.fsync(file) + + def setup_server_io(): """Fill IO files DB with proper file( path)s. Write process IO test string. @@ -25,8 +32,7 @@ def setup_server_io(): io_db["teststring"] = str(os.getpid()) + " " + str(time.time()) os.makedirs(io_db["path_server"], exist_ok=True) io_db["file_out"] = open(io_db["path_out"], "w") - io_db["file_out"].write(io_db["teststring"] + "\n") - io_db["file_out"].flush() + strong_write(io_db["file_out"], io_db["teststring"] + "\n") if os.access(io_db["path_in"], os.F_OK): os.remove(io_db["path_in"]) io_db["file_in"] = open(io_db["path_in"], "w") @@ -101,9 +107,7 @@ def atomic_write(path, text, do_append=False): if os.access(path, os.F_OK): shutil.copyfile(path, path_tmp) file = open(path_tmp, mode) - file.write(text) - file.flush() - os.fsync(file.fileno()) + strong_write(file, text) file.close() if os.access(path, os.F_OK): os.remove(path) @@ -270,7 +274,7 @@ def try_worldstate_update(): string = string + line + "\n" # TODO: no proper user-subjective map atomic_write(io_db["path_worldstate"], string) - atomic_write(io_db["path_out"], "WORLD_UPDATED\n", do_append=True) + strong_write(io_db["file_out"], "WORLD_UPDATED\n") io_db["worldstate_updateable"] = False @@ -414,8 +418,7 @@ def id_setter(id, category, id_store=False, start_at_1=False): def command_ping(): """Send PONG line to server output file.""" - io_db["file_out"].write("PONG\n") - io_db["file_out"].flush() + strong_write(io_db["file_out"], "PONG\n") def command_quit():