Skip to content

Commit

Permalink
stability improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jthistle committed Mar 24, 2021
1 parent 3c3f407 commit 1a3c124
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 23 deletions.
16 changes: 12 additions & 4 deletions linux/plugins/read_util.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
/**
* @brief Utility that will output the value of the position in the shared memory.
*
* Return codes:
* 0: ok - exited normally (currently not reachable)
* 1: error - could not open shared memory
* 2: error - failed to mmap shared memory
*/

#include <stdio.h>
Expand All @@ -19,11 +24,14 @@
#include "amtrucks/scssdk_ats.h"
#include "amtrucks/scssdk_telemetry_ats.h"

#define ERR_COULD_NOT_OPEN_MEMORY 1
#define ERR_MMAP_FAILED 2

void print_info(telemetry_state_t info) {
scs_value_dvector_t pos = info.world_position.position;
int elec = info.electricity.value > 0 ? 1 : 0;
fprintf(stdout, "%f,%f,%f;%d\n", pos.x, pos.y, pos.z, elec);
fflush(stdout);
fprintf(stdout, "INFO %f,%f,%f;%d\n", pos.x, pos.y, pos.z, elec);
fflush(stdout);
}

int main() {
Expand All @@ -34,7 +42,7 @@ int main() {
int handle = shm_open(shm_name, O_RDONLY, S_IRUSR);
if (handle < 0) {
fprintf(stderr, "ERR Could not open shared memory\n");
return 1;
return ERR_COULD_NOT_OPEN_MEMORY;
}

void* mapped_region = mmap(
Expand All @@ -48,7 +56,7 @@ int main() {

if (mapped_region == MAP_FAILED) {
fprintf(stderr, "ERR Could not mmap shared memory\n");
return 1;
return ERR_MMAP_FAILED;
}

telemetry_state_t telemetry;
Expand Down
14 changes: 14 additions & 0 deletions linux/plugins/uninstall.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

rm -f -- ../server/read_util

if [ ! -d "$HOME/.local/share/Steam/steamapps/common/Euro Truck Simulator 2/bin/linux_x64/" ]; then
echo "WARN: Could not find ETS2 installation! Please manually move ets2-telemetry-lin.so to your plugins directory."
echo "Uninstalled with partial success"
exit 1
else
rm -f -- "$HOME/.local/share/Steam/steamapps/common/Euro Truck Simulator 2/bin/linux_x64/plugins/ets2-telemetry-lin.so"
rm -f -- "$HOME/.local/share/Steam/steamapps/common/Euro Truck Simulator 2/bin/linux_x86/plugins/ets2-telemetry-lin.so"
fi

echo "Uninstalled successfully"
77 changes: 58 additions & 19 deletions linux/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,43 @@ def start_read():
global last_value, has_elec
global alive

read_util = subprocess.Popen(["./read_util"], stdout=subprocess.PIPE, bufsize=1, universal_newlines=True)
while read_util.poll() is None and alive:
txt = read_util.stdout.readline().strip()
if not txt:
continue

parts = txt.split(";")
last_value = tuple([float(x) for x in parts[0].split(",")])
has_elec[0] = int(parts[1]) > 0

read_util.stdout.close()
read_util.send_signal(signal.SIGKILL)
timeout = 1

while alive:
try:
read_util = subprocess.Popen(["./read_util"], stdout=subprocess.PIPE, bufsize=1, universal_newlines=True)
except Exception as e:
print("Couldn't start read util:", e)
alive = False
break

while read_util.poll() is None and alive:
txt = read_util.stdout.readline().strip()
if not txt:
continue

if txt[:4] != "INFO":
continue

# reset timeout
timeout = 1

txt = txt[5:]
parts = txt.split(";")
last_value = tuple([float(x) for x in parts[0].split(",")])
has_elec[0] = int(parts[1]) > 0

if read_util.returncode == 1:
print(f"Your game doesn't seem to be running, trying again in 5 seconds...")
time.sleep(5)
elif read_util > 0:
# Generic error handling
print(f"Read util encountered an error, trying again in {timeout} seconds...")
time.sleep(timeout)
timeout *= 2

read_util.stdout.close()
read_util.send_signal(signal.SIGKILL)

class CustomHandler(SimpleHTTPRequestHandler):
def __init__(self, *args):
Expand Down Expand Up @@ -76,24 +101,38 @@ def do_GET(self):
else:
super().do_GET()

# Comment this out if you want to debug issues - for now this just prevents the
# handler cluttering stdout with logs.
def log_message(self, format, *args):
pass

def run_server():
global last_value
global alive
global httpd
global PORT

with socketserver.TCPServer(("", PORT), CustomHandler) as httpd:
print("Server open on port", PORT)
httpd.serve_forever()

def signal_handler(sig, frame):
while alive:
try:
with socketserver.TCPServer(("", PORT), CustomHandler) as httpd:
print(f"Server open on port {PORT}, visit it at: http://localhost:{PORT}/")
httpd.serve_forever()
except OSError as e:
if e.errno == 98: # Address in use
print("That port is already in use (possibly from previous run of this script)")
print("Waiting 5 seconds before trying again...")
time.sleep(5)
else:
raise # reraise

def shutdown(sig=None, frame=None):
global alive
global httpd

if not alive:
return

print("Interrupted, shutting down")
print("Interrupted, shutting down...")
alive = False

if httpd is not None:
Expand All @@ -104,7 +143,7 @@ def signal_handler(sig, frame):

print("Done.")

signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGINT, shutdown)

read_thread = threading.Thread(target=start_read)
server_thread = threading.Thread(target=run_server)
Expand Down

0 comments on commit 1a3c124

Please sign in to comment.