LocalBridge wraps StreamPeerTCP with 4-byte big-endian length-prefix framing matching server/src/bridge/framing.rs. ServerProcess manages the Rust server as a subprocess via OS.create_process(). Together they form the D-020 IPC transport layer for ticket #79. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
144 lines
4.3 KiB
GDScript
144 lines
4.3 KiB
GDScript
class_name LocalBridge
|
|
## TCP transport with length-prefixed framing for IPC (D-020).
|
|
##
|
|
## Wraps StreamPeerTCP with 4-byte big-endian length-prefix framing
|
|
## matching server/src/bridge/framing.rs.
|
|
|
|
const MAX_MESSAGE_SIZE: int = 16 * 1024 * 1024 # 16 MB, matching server
|
|
|
|
var _stream: StreamPeerTCP
|
|
var _read_buffer: PackedByteArray = PackedByteArray()
|
|
var _pending_length: int = -1 # -1 = awaiting header, >= 0 = awaiting payload
|
|
|
|
|
|
func _init() -> void:
|
|
_stream = StreamPeerTCP.new()
|
|
|
|
|
|
## Initiate connection to host:port. Non-blocking — poll get_status() for result.
|
|
func connect_to_server(host: String, port: int) -> Error:
|
|
return _stream.connect_to_host(host, port)
|
|
|
|
|
|
## Poll the TCP stream. Call every frame to drive connection and reads.
|
|
func poll() -> void:
|
|
_stream.poll()
|
|
|
|
|
|
## Current TCP connection status.
|
|
func get_status() -> StreamPeerTCP.Status:
|
|
return _stream.get_status()
|
|
|
|
|
|
## Whether the TCP stream is in the connected state.
|
|
func is_connected_to_server() -> bool:
|
|
return _stream.get_status() == StreamPeerTCP.STATUS_CONNECTED
|
|
|
|
|
|
## Send a framed message: [4-byte BE length][payload].
|
|
func send_message(payload: PackedByteArray) -> Error:
|
|
if not is_connected_to_server():
|
|
return ERR_CONNECTION_ERROR
|
|
|
|
var len := payload.size()
|
|
if len > MAX_MESSAGE_SIZE:
|
|
push_error("LocalBridge: message too large: %d bytes (max %d)" % [len, MAX_MESSAGE_SIZE])
|
|
return ERR_PARAMETER_RANGE_ERROR
|
|
|
|
# 4-byte big-endian length header
|
|
var header := PackedByteArray()
|
|
header.resize(4)
|
|
header[0] = (len >> 24) & 0xFF
|
|
header[1] = (len >> 16) & 0xFF
|
|
header[2] = (len >> 8) & 0xFF
|
|
header[3] = len & 0xFF
|
|
|
|
var err := _stream.put_data(header)
|
|
if err != OK:
|
|
return err
|
|
return _stream.put_data(payload)
|
|
|
|
|
|
## Non-blocking poll for a complete framed message.
|
|
## Returns the payload if a complete message is available, empty array otherwise.
|
|
## Call in a loop until empty to drain all buffered messages.
|
|
func poll_message() -> PackedByteArray:
|
|
if not is_connected_to_server():
|
|
return PackedByteArray()
|
|
|
|
# Read any available bytes into the buffer
|
|
var available := _stream.get_available_bytes()
|
|
if available > 0:
|
|
var result := _stream.get_data(available)
|
|
if result[0] != OK:
|
|
return PackedByteArray()
|
|
_read_buffer.append_array(result[1])
|
|
|
|
return _try_extract_message()
|
|
|
|
|
|
## Try to extract a complete message from the read buffer.
|
|
func _try_extract_message() -> PackedByteArray:
|
|
# Phase 1: read 4-byte header if we don't have a pending length
|
|
if _pending_length < 0:
|
|
if _read_buffer.size() < 4:
|
|
return PackedByteArray()
|
|
_pending_length = (_read_buffer[0] << 24) | (_read_buffer[1] << 16) | \
|
|
(_read_buffer[2] << 8) | _read_buffer[3]
|
|
_read_buffer = _read_buffer.slice(4)
|
|
|
|
if _pending_length > MAX_MESSAGE_SIZE:
|
|
push_error("LocalBridge: incoming message too large: %d bytes (max %d)" % [_pending_length, MAX_MESSAGE_SIZE])
|
|
_pending_length = -1
|
|
_read_buffer.clear()
|
|
return PackedByteArray()
|
|
|
|
# Phase 2: read payload
|
|
if _read_buffer.size() < _pending_length:
|
|
return PackedByteArray()
|
|
|
|
var payload := _read_buffer.slice(0, _pending_length)
|
|
_read_buffer = _read_buffer.slice(_pending_length)
|
|
_pending_length = -1
|
|
return payload
|
|
|
|
|
|
## Close the connection and reset read state.
|
|
func disconnect_from_server() -> void:
|
|
_stream.disconnect_from_host()
|
|
_read_buffer.clear()
|
|
_pending_length = -1
|
|
|
|
|
|
# -- Static helpers for framing (used in tests without a live TCP connection) --
|
|
|
|
## Encode a payload into a framed byte array: [4-byte BE length][payload].
|
|
static func frame_encode(payload: PackedByteArray) -> PackedByteArray:
|
|
var len := payload.size()
|
|
var frame := PackedByteArray()
|
|
frame.resize(4 + len)
|
|
frame[0] = (len >> 24) & 0xFF
|
|
frame[1] = (len >> 16) & 0xFF
|
|
frame[2] = (len >> 8) & 0xFF
|
|
frame[3] = len & 0xFF
|
|
for i in range(len):
|
|
frame[4 + i] = payload[i]
|
|
return frame
|
|
|
|
|
|
## Decode the first framed message from raw bytes.
|
|
## Returns { "payload": PackedByteArray, "remainder": PackedByteArray } or null if incomplete.
|
|
static func frame_decode(data: PackedByteArray) -> Variant:
|
|
if data.size() < 4:
|
|
return null
|
|
var len := (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]
|
|
if len > MAX_MESSAGE_SIZE:
|
|
push_error("LocalBridge: frame too large: %d bytes" % len)
|
|
return null
|
|
if data.size() < 4 + len:
|
|
return null
|
|
return {
|
|
"payload": data.slice(4, 4 + len),
|
|
"remainder": data.slice(4 + len),
|
|
}
|