From 53e4be0439dd8e216097a6b0ffe648b00f61f7b9 Mon Sep 17 00:00:00 2001 From: Kiri Date: Sun, 14 Jul 2024 14:49:44 -0700 Subject: [PATCH] Pre-stream commit. --- .../KiriPacketSocket/KiriPacketSocket.gd | 67 ++++++++++--------- .../KiriPythonRPCWrapper/__init__.py | 7 -- .../KiriPythonRPCWrapper_start.py | 10 --- .../KiriPythonWrapperInstance.gd | 3 +- addons/KiriPythonRPCWrapper/TODO.md | 29 +++++--- addons/KiriPythonRPCWrapper/test_rpc.py | 28 -------- 6 files changed, 56 insertions(+), 88 deletions(-) delete mode 100755 addons/KiriPythonRPCWrapper/KiriPythonRPCWrapper_start.py delete mode 100755 addons/KiriPythonRPCWrapper/test_rpc.py diff --git a/addons/KiriPythonRPCWrapper/KiriPythonRPCWrapper/KiriPacketSocket/KiriPacketSocket.gd b/addons/KiriPythonRPCWrapper/KiriPythonRPCWrapper/KiriPacketSocket/KiriPacketSocket.gd index ad47700..385f5bc 100644 --- a/addons/KiriPythonRPCWrapper/KiriPythonRPCWrapper/KiriPacketSocket/KiriPacketSocket.gd +++ b/addons/KiriPythonRPCWrapper/KiriPythonRPCWrapper/KiriPacketSocket/KiriPacketSocket.gd @@ -144,39 +144,37 @@ func stop(): func is_running(): return not (_worker_thread == null) -func _normal_communication_loop(sock : StreamPeer, address): +func _normal_communication_loop_iteration(sock : StreamPeer, address): - while not _should_quit: + if sock.poll() != OK: + return FAILED - if sock.poll() != OK: - break + if sock.get_status() != StreamPeerTCP.STATUS_CONNECTED: + return FAILED - if sock.get_status() != StreamPeerTCP.STATUS_CONNECTED: - break + # Get new data. + _state_lock.lock() + var available_bytes = sock.get_available_bytes() + if available_bytes > 0: + var incoming_bytes = sock.get_data(available_bytes) + _packet_buffer.add_bytes(PackedByteArray(incoming_bytes[1])) + if incoming_bytes[0] != OK: + return FAILED + _state_lock.unlock() - # Get new data. - _state_lock.lock() - var available_bytes = sock.get_available_bytes() - if available_bytes > 0: - var incoming_bytes = sock.get_data(available_bytes) - _packet_buffer.add_bytes(PackedByteArray(incoming_bytes[1])) - if incoming_bytes[0] != OK: - break - _state_lock.unlock() + # Send all packets from queue. + _state_lock.lock() + while len(self._outgoing_packet_queue): + var next_outgoing_packet = _outgoing_packet_queue.pop_front() + var len_to_send = len(next_outgoing_packet) + sock.put_u8((len_to_send & 0x000000ff) >> 0) + sock.put_u8((len_to_send & 0x0000ff00) >> 8) + sock.put_u8((len_to_send & 0x00ff0000) >> 16) + sock.put_u8((len_to_send & 0xff000000) >> 24) + sock.put_data(next_outgoing_packet) + _state_lock.unlock() - # Send all packets from queue. - _state_lock.lock() - while len(self._outgoing_packet_queue): - var next_outgoing_packet = _outgoing_packet_queue.pop_front() - var len_to_send = len(next_outgoing_packet) - sock.put_u8((len_to_send & 0x000000ff) >> 0) - sock.put_u8((len_to_send & 0x0000ff00) >> 8) - sock.put_u8((len_to_send & 0x00ff0000) >> 16) - sock.put_u8((len_to_send & 0xff000000) >> 24) - sock.put_data(next_outgoing_packet) - _state_lock.unlock() - - OS.delay_usec(1) + return OK func _client_thread_func(address): @@ -189,7 +187,11 @@ func _client_thread_func(address): if connect_err == OK: _set_state(KiriSocketState.CONNECTED) - _normal_communication_loop(sock, address) + while not _should_quit: + var err = _normal_communication_loop_iteration(sock, address) + if err != OK: + break + OS.delay_usec(1) # We are now disconnected. _set_state(KiriSocketState.DISCONNECTED) @@ -214,7 +216,12 @@ func _set_state(state : KiriSocketState, error_string=null): func _server_to_client_thread_func(connection : StreamPeerTCP, address): _set_state(KiriSocketState.CONNECTED) - _normal_communication_loop(connection, address) + + while not _should_quit: + var err = _normal_communication_loop_iteration(connection, address) + if err != OK: + break + OS.delay_usec(1) # FIXME: Missing some error handling here due to exception differences # between Python and GDScript. diff --git a/addons/KiriPythonRPCWrapper/KiriPythonRPCWrapper/__init__.py b/addons/KiriPythonRPCWrapper/KiriPythonRPCWrapper/__init__.py index a241bca..cc6ebe7 100755 --- a/addons/KiriPythonRPCWrapper/KiriPythonRPCWrapper/__init__.py +++ b/addons/KiriPythonRPCWrapper/KiriPythonRPCWrapper/__init__.py @@ -4,7 +4,6 @@ import importlib.util import sys import argparse import time -# import psutil import json import KiriPacketSocket @@ -22,7 +21,6 @@ if True: arg_parser.add_argument("--script", type=str, required=True) arg_parser.add_argument("--port", type=int, required=True) - arg_parser.add_argument("--parent_pid", type=int, required=True) args = arg_parser.parse_args() @@ -103,11 +101,6 @@ if True: packet_socket.stop() raise Exception("Disconnected from RPC host.") - # # Watch parent PID so we can clean up when needed. - # if not psutil.pid_exists(args.parent_pid): - # packet_socket.stop() - # raise Exception("RPC host process died") - next_packet = packet_socket.get_next_packet() while next_packet: this_packet = next_packet diff --git a/addons/KiriPythonRPCWrapper/KiriPythonRPCWrapper_start.py b/addons/KiriPythonRPCWrapper/KiriPythonRPCWrapper_start.py deleted file mode 100755 index 9880d27..0000000 --- a/addons/KiriPythonRPCWrapper/KiriPythonRPCWrapper_start.py +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/python3 - -import time - -try: - import KiriPythonRPCWrapper -except Exception as e: - print(e) - time.sleep(5) - diff --git a/addons/KiriPythonRPCWrapper/KiriPythonWrapperInstance.gd b/addons/KiriPythonRPCWrapper/KiriPythonWrapperInstance.gd index 7242ebe..be8636a 100644 --- a/addons/KiriPythonRPCWrapper/KiriPythonWrapperInstance.gd +++ b/addons/KiriPythonRPCWrapper/KiriPythonWrapperInstance.gd @@ -125,8 +125,7 @@ func start_process(): python_exe_path, wrapper_script_path, "--script", python_script_path, - "--port", open_port, - "--parent_pid", OS.get_process_id()] + "--port", open_port] print("startup command: ", startup_command) diff --git a/addons/KiriPythonRPCWrapper/TODO.md b/addons/KiriPythonRPCWrapper/TODO.md index 66c03d3..2b7d46b 100644 --- a/addons/KiriPythonRPCWrapper/TODO.md +++ b/addons/KiriPythonRPCWrapper/TODO.md @@ -1,21 +1,28 @@ -The big ones: - - Handle bundling of the actual Python modules we want to use. - - First-time setup of requirements (pip, etc). +Done: + x Handle bundling of the actual Python modules we want to use. x Remove dependency on psutil. - - Clean up removal of psutil. - - Remove xterm dependency, or make it like a debug-only thing. - - Test on WINE/Windows. - - Documentation. - - how to use .kiri_export_python + x Clean up removal of psutil. + x remove parent_pid from wrapper script + x remove KiriPythonRPCWrapper_start.py + x remove test_rpc.py + +The big ones: - Un-thread the GDScript side of PacketSocket. - - Fix whatever this is: + - Fix whatever this is: SCRIPT ERROR: Assertion failed. at: KiriPacketSocket._notification (res://addons/KiriPythonRPCWrapper/KiriPacketSocket/KiriPacketSocket.gd:70) WARNING: A Thread object is being destroyed without its completion having been realized. Please call wait_to_finish() on it to ensure correct cleanup. at: ~Thread (core/os/thread.cpp:102) - - remove KiriPythonRPCWrapper_start.py - - remove test_rpc.py + - First-time setup of requirements (pip, etc). + - Remove xterm dependency, or make it like a debug-only thing. + - Test on WINE/Windows. + - Documentation. + - how to use .kiri_export_python + + + - example Python module from OUTSIDE the addon + diff --git a/addons/KiriPythonRPCWrapper/test_rpc.py b/addons/KiriPythonRPCWrapper/test_rpc.py deleted file mode 100755 index 1668b24..0000000 --- a/addons/KiriPythonRPCWrapper/test_rpc.py +++ /dev/null @@ -1,28 +0,0 @@ -#!/usr/bin/python3 - -import time - -import KiriPacketSocket - -ps = KiriPacketSocket.PacketSocket() -ps.start_server(("127.0.0.1", 9506)) - -connections = [] - -while True: - - psc = ps.get_next_server_connection() - if psc: - print("Got connection.") - connections.append(psc) - psc.send_packet(b'ABCDEFGHIJ') - - - for conn in connections: - p = conn.get_next_packet() - while p: - print(p) - conn.send_packet(p + b'1') - p = conn.get_next_packet() - - time.sleep(0.0001)