Pre-stream commit.

This commit is contained in:
Kiri 2024-07-14 14:49:44 -07:00
parent 4cad6a574f
commit 53e4be0439
6 changed files with 56 additions and 88 deletions

View File

@ -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.

View File

@ -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

View File

@ -1,10 +0,0 @@
#!/usr/bin/python3
import time
try:
import KiriPythonRPCWrapper
except Exception as e:
print(e)
time.sleep(5)

View File

@ -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)

View File

@ -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

View File

@ -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)