rocket-welder-sdk 1.1.32__py3-none-any.whl → 1.1.34__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,193 @@
1
+ """Stream-based transport (file, memory, etc.)."""
2
+
3
+ from typing import BinaryIO, Optional
4
+
5
+ from .frame_sink import IFrameSink
6
+ from .frame_source import IFrameSource
7
+
8
+
9
+ def _write_varint(stream: BinaryIO, value: int) -> None:
10
+ """Write unsigned integer as varint (Protocol Buffers format)."""
11
+ if value < 0:
12
+ raise ValueError(f"Varint requires non-negative value, got {value}")
13
+
14
+ while value >= 0x80:
15
+ stream.write(bytes([value & 0x7F | 0x80]))
16
+ value >>= 7
17
+ stream.write(bytes([value & 0x7F]))
18
+
19
+
20
+ def _read_varint(stream: BinaryIO) -> int:
21
+ """Read varint from stream and decode to unsigned integer."""
22
+ result = 0
23
+ shift = 0
24
+
25
+ while True:
26
+ if shift >= 35: # Max 5 bytes for uint32
27
+ raise ValueError("Varint too long (corrupted stream)")
28
+
29
+ byte_data = stream.read(1)
30
+ if not byte_data:
31
+ raise EOFError("Unexpected end of stream reading varint")
32
+
33
+ byte = byte_data[0]
34
+ result |= (byte & 0x7F) << shift
35
+ shift += 7
36
+
37
+ if not (byte & 0x80):
38
+ break
39
+
40
+ return result
41
+
42
+
43
+ class StreamFrameSink(IFrameSink):
44
+ """
45
+ Frame sink that writes to a BinaryIO stream (file, memory, etc.).
46
+
47
+ Each frame is prefixed with its length (varint encoding) for frame boundary detection.
48
+ Format: [varint length][frame data]
49
+ """
50
+
51
+ def __init__(self, stream: BinaryIO, leave_open: bool = False):
52
+ """
53
+ Create a stream-based frame sink.
54
+
55
+ Args:
56
+ stream: Binary stream to write to
57
+ leave_open: If True, doesn't close stream on close
58
+ """
59
+ self._stream = stream
60
+ self._leave_open = leave_open
61
+ self._closed = False
62
+
63
+ def write_frame(self, frame_data: bytes) -> None:
64
+ """Write frame data to stream with varint length prefix."""
65
+ if self._closed:
66
+ raise ValueError("Cannot write to closed sink")
67
+
68
+ # Write frame length as varint
69
+ _write_varint(self._stream, len(frame_data))
70
+
71
+ # Write frame data
72
+ self._stream.write(frame_data)
73
+
74
+ async def write_frame_async(self, frame_data: bytes) -> None:
75
+ """Write frame data to stream asynchronously."""
76
+ # For regular streams, just use synchronous write
77
+ # If stream supports async, could use aiofiles
78
+ self.write_frame(frame_data)
79
+
80
+ def flush(self) -> None:
81
+ """Flush buffered data to stream."""
82
+ if not self._closed:
83
+ self._stream.flush()
84
+
85
+ async def flush_async(self) -> None:
86
+ """Flush buffered data to stream asynchronously."""
87
+ self.flush()
88
+
89
+ def close(self) -> None:
90
+ """Close the sink."""
91
+ if self._closed:
92
+ return
93
+ self._closed = True
94
+ if not self._leave_open:
95
+ self._stream.close()
96
+
97
+ async def close_async(self) -> None:
98
+ """Close the sink asynchronously."""
99
+ self.close()
100
+
101
+
102
+ class StreamFrameSource(IFrameSource):
103
+ """
104
+ Frame source that reads from a BinaryIO stream (file, memory, etc.).
105
+
106
+ Reads frames prefixed with varint length for frame boundary detection.
107
+ Format: [varint length][frame data]
108
+ """
109
+
110
+ def __init__(self, stream: BinaryIO, leave_open: bool = False):
111
+ """
112
+ Create a stream-based frame source.
113
+
114
+ Args:
115
+ stream: Binary stream to read from
116
+ leave_open: If True, doesn't close stream on close
117
+ """
118
+ self._stream = stream
119
+ self._leave_open = leave_open
120
+ self._closed = False
121
+
122
+ @property
123
+ def has_more_frames(self) -> bool:
124
+ """Check if more data available in stream."""
125
+ if self._closed:
126
+ return False
127
+ current_pos = self._stream.tell()
128
+ # Try seeking to end to check size
129
+ try:
130
+ self._stream.seek(0, 2) # Seek to end
131
+ end_pos = self._stream.tell()
132
+ self._stream.seek(current_pos) # Restore position
133
+ return current_pos < end_pos
134
+ except OSError:
135
+ # Stream not seekable, assume data available
136
+ return True
137
+
138
+ def read_frame(self) -> Optional[bytes]:
139
+ """
140
+ Read frame from stream with varint length-prefix framing.
141
+
142
+ Returns:
143
+ Frame data bytes, or None if end of stream
144
+ """
145
+ if self._closed:
146
+ return None
147
+
148
+ # Check if stream has data (for seekable streams)
149
+ if hasattr(self._stream, "tell") and hasattr(self._stream, "seek"):
150
+ try:
151
+ current_pos = self._stream.tell()
152
+ self._stream.seek(0, 2) # Seek to end
153
+ end_pos = self._stream.tell()
154
+ self._stream.seek(current_pos) # Restore position
155
+ if current_pos >= end_pos:
156
+ return None
157
+ except OSError:
158
+ pass # Stream not seekable, continue
159
+
160
+ # Read frame length (varint)
161
+ try:
162
+ frame_length = _read_varint(self._stream)
163
+ except EOFError:
164
+ return None
165
+
166
+ if frame_length == 0:
167
+ return b""
168
+
169
+ # Read frame data
170
+ frame_data = self._stream.read(frame_length)
171
+ if len(frame_data) != frame_length:
172
+ raise EOFError(
173
+ f"Unexpected end of stream while reading frame. Expected {frame_length} bytes, got {len(frame_data)}"
174
+ )
175
+
176
+ return frame_data
177
+
178
+ async def read_frame_async(self) -> Optional[bytes]:
179
+ """Read frame from stream asynchronously."""
180
+ # For regular streams, just use synchronous read
181
+ return self.read_frame()
182
+
183
+ def close(self) -> None:
184
+ """Close the source."""
185
+ if self._closed:
186
+ return
187
+ self._closed = True
188
+ if not self._leave_open:
189
+ self._stream.close()
190
+
191
+ async def close_async(self) -> None:
192
+ """Close the source asynchronously."""
193
+ self.close()
@@ -0,0 +1,154 @@
1
+ """TCP transport with length-prefix framing."""
2
+
3
+ import contextlib
4
+ import socket
5
+ import struct
6
+ from typing import Optional
7
+
8
+ from .frame_sink import IFrameSink
9
+ from .frame_source import IFrameSource
10
+
11
+
12
+ class TcpFrameSink(IFrameSink):
13
+ """
14
+ Frame sink that writes to a TCP connection with length-prefix framing.
15
+
16
+ Each frame is prefixed with a 4-byte little-endian length header.
17
+
18
+ Frame format: [Length: 4 bytes LE][Frame Data: N bytes]
19
+ """
20
+
21
+ def __init__(self, sock: socket.socket, leave_open: bool = False):
22
+ """
23
+ Create a TCP frame sink.
24
+
25
+ Args:
26
+ sock: TCP socket to write to
27
+ leave_open: If True, doesn't close socket on close
28
+ """
29
+ self._socket = sock
30
+ self._leave_open = leave_open
31
+ self._closed = False
32
+
33
+ def write_frame(self, frame_data: bytes) -> None:
34
+ """Write frame with length prefix to TCP socket."""
35
+ if self._closed:
36
+ raise ValueError("Cannot write to closed sink")
37
+
38
+ # Write 4-byte length prefix (little-endian)
39
+ length_prefix = struct.pack("<I", len(frame_data))
40
+ self._socket.sendall(length_prefix)
41
+
42
+ # Write frame data
43
+ self._socket.sendall(frame_data)
44
+
45
+ async def write_frame_async(self, frame_data: bytes) -> None:
46
+ """Write frame asynchronously (uses sync socket for now)."""
47
+ self.write_frame(frame_data)
48
+
49
+ def flush(self) -> None:
50
+ """Flush is a no-op for TCP (data sent immediately)."""
51
+ pass
52
+
53
+ async def flush_async(self) -> None:
54
+ """Flush asynchronously is a no-op for TCP."""
55
+ pass
56
+
57
+ def close(self) -> None:
58
+ """Close the TCP sink."""
59
+ if self._closed:
60
+ return
61
+ self._closed = True
62
+ if not self._leave_open:
63
+ with contextlib.suppress(OSError):
64
+ self._socket.shutdown(socket.SHUT_WR)
65
+ self._socket.close()
66
+
67
+ async def close_async(self) -> None:
68
+ """Close the TCP sink asynchronously."""
69
+ self.close()
70
+
71
+
72
+ class TcpFrameSource(IFrameSource):
73
+ """
74
+ Frame source that reads from a TCP connection with length-prefix framing.
75
+
76
+ Each frame is prefixed with a 4-byte little-endian length header.
77
+
78
+ Frame format: [Length: 4 bytes LE][Frame Data: N bytes]
79
+ """
80
+
81
+ def __init__(self, sock: socket.socket, leave_open: bool = False):
82
+ """
83
+ Create a TCP frame source.
84
+
85
+ Args:
86
+ sock: TCP socket to read from
87
+ leave_open: If True, doesn't close socket on close
88
+ """
89
+ self._socket = sock
90
+ self._leave_open = leave_open
91
+ self._closed = False
92
+ self._end_of_stream = False
93
+
94
+ @property
95
+ def has_more_frames(self) -> bool:
96
+ """Check if more frames available."""
97
+ return not self._closed and not self._end_of_stream
98
+
99
+ def read_frame(self) -> Optional[bytes]:
100
+ """Read frame with length prefix from TCP socket."""
101
+ if self._closed or self._end_of_stream:
102
+ return None
103
+
104
+ # Read 4-byte length prefix
105
+ length_data = self._recv_exactly(4)
106
+ if length_data is None or len(length_data) < 4:
107
+ self._end_of_stream = True
108
+ return None
109
+
110
+ frame_length = struct.unpack("<I", length_data)[0]
111
+
112
+ if frame_length == 0:
113
+ return b""
114
+
115
+ if frame_length > 100 * 1024 * 1024: # 100 MB sanity check
116
+ raise ValueError(f"Frame length {frame_length} exceeds maximum")
117
+
118
+ # Read frame data
119
+ frame_data = self._recv_exactly(frame_length)
120
+ if frame_data is None or len(frame_data) < frame_length:
121
+ self._end_of_stream = True
122
+ raise ValueError(
123
+ f"Incomplete frame data: expected {frame_length}, got {len(frame_data) if frame_data else 0}"
124
+ )
125
+
126
+ return frame_data
127
+
128
+ async def read_frame_async(self) -> Optional[bytes]:
129
+ """Read frame asynchronously (uses sync socket for now)."""
130
+ return self.read_frame()
131
+
132
+ def _recv_exactly(self, n: int) -> Optional[bytes]:
133
+ """Receive exactly n bytes from socket."""
134
+ data = b""
135
+ while len(data) < n:
136
+ chunk = self._socket.recv(n - len(data))
137
+ if not chunk:
138
+ return data if data else None
139
+ data += chunk
140
+ return data
141
+
142
+ def close(self) -> None:
143
+ """Close the TCP source."""
144
+ if self._closed:
145
+ return
146
+ self._closed = True
147
+ if not self._leave_open:
148
+ with contextlib.suppress(OSError):
149
+ self._socket.shutdown(socket.SHUT_RD)
150
+ self._socket.close()
151
+
152
+ async def close_async(self) -> None:
153
+ """Close the TCP source asynchronously."""
154
+ self.close()
@@ -0,0 +1,339 @@
1
+ """Unix Domain Socket transport with length-prefix framing.
2
+
3
+ Frame format: [Length: 4 bytes LE][Frame Data: N bytes]
4
+ Unix Domain Sockets provide high-performance IPC on Linux/macOS.
5
+ """
6
+
7
+ import asyncio
8
+ import contextlib
9
+ import os
10
+ import socket
11
+ import struct
12
+ from typing import Optional
13
+
14
+ from .frame_sink import IFrameSink
15
+ from .frame_source import IFrameSource
16
+
17
+
18
+ class UnixSocketFrameSink(IFrameSink):
19
+ """
20
+ Frame sink that writes to a Unix Domain Socket with length-prefix framing.
21
+
22
+ Each frame is prefixed with a 4-byte little-endian length header.
23
+ """
24
+
25
+ def __init__(self, sock: socket.socket, leave_open: bool = False):
26
+ """
27
+ Create a Unix socket frame sink.
28
+
29
+ Args:
30
+ sock: Connected Unix domain socket
31
+ leave_open: If True, doesn't close socket on close
32
+ """
33
+ if sock.family != socket.AF_UNIX:
34
+ raise ValueError("Socket must be a Unix domain socket")
35
+
36
+ self._socket = sock
37
+ self._leave_open = leave_open
38
+ self._closed = False
39
+
40
+ @classmethod
41
+ def connect(cls, socket_path: str) -> "UnixSocketFrameSink":
42
+ """
43
+ Connect to a Unix socket path and create a frame sink.
44
+
45
+ Args:
46
+ socket_path: Path to Unix socket file
47
+
48
+ Returns:
49
+ Connected frame sink
50
+ """
51
+ sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
52
+ sock.connect(socket_path)
53
+ return cls(sock, leave_open=False)
54
+
55
+ @classmethod
56
+ async def connect_async(cls, socket_path: str) -> "UnixSocketFrameSink":
57
+ """
58
+ Connect to a Unix socket path asynchronously and create a frame sink.
59
+
60
+ Args:
61
+ socket_path: Path to Unix socket file
62
+
63
+ Returns:
64
+ Connected frame sink
65
+ """
66
+ sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
67
+ sock.setblocking(False)
68
+ loop = asyncio.get_event_loop()
69
+ await loop.sock_connect(sock, socket_path)
70
+ return cls(sock, leave_open=False)
71
+
72
+ def write_frame(self, frame_data: bytes) -> None:
73
+ """Write frame with 4-byte length prefix to Unix socket."""
74
+ if self._closed:
75
+ raise ValueError("Cannot write to closed sink")
76
+
77
+ # Write 4-byte length prefix (little-endian)
78
+ length_prefix = struct.pack("<I", len(frame_data))
79
+ self._socket.sendall(length_prefix)
80
+
81
+ # Write frame data
82
+ self._socket.sendall(frame_data)
83
+
84
+ async def write_frame_async(self, frame_data: bytes) -> None:
85
+ """Write frame asynchronously."""
86
+ if self._closed:
87
+ raise ValueError("Cannot write to closed sink")
88
+
89
+ loop = asyncio.get_event_loop()
90
+
91
+ # Write 4-byte length prefix (little-endian)
92
+ length_prefix = struct.pack("<I", len(frame_data))
93
+ await loop.sock_sendall(self._socket, length_prefix)
94
+
95
+ # Write frame data
96
+ await loop.sock_sendall(self._socket, frame_data)
97
+
98
+ def flush(self) -> None:
99
+ """Flush is a no-op for Unix sockets (data sent immediately)."""
100
+ pass
101
+
102
+ async def flush_async(self) -> None:
103
+ """Flush asynchronously is a no-op for Unix sockets."""
104
+ pass
105
+
106
+ def close(self) -> None:
107
+ """Close the Unix socket sink."""
108
+ if self._closed:
109
+ return
110
+ self._closed = True
111
+ if not self._leave_open:
112
+ with contextlib.suppress(OSError):
113
+ self._socket.shutdown(socket.SHUT_WR)
114
+ self._socket.close()
115
+
116
+ async def close_async(self) -> None:
117
+ """Close the Unix socket sink asynchronously."""
118
+ self.close()
119
+
120
+
121
+ class UnixSocketFrameSource(IFrameSource):
122
+ """
123
+ Frame source that reads from a Unix Domain Socket with length-prefix framing.
124
+
125
+ Each frame is prefixed with a 4-byte little-endian length header.
126
+ """
127
+
128
+ # Maximum frame size (100 MB)
129
+ MAX_FRAME_SIZE = 100 * 1024 * 1024
130
+
131
+ def __init__(self, sock: socket.socket, leave_open: bool = False):
132
+ """
133
+ Create a Unix socket frame source.
134
+
135
+ Args:
136
+ sock: Connected Unix domain socket
137
+ leave_open: If True, doesn't close socket on close
138
+ """
139
+ if sock.family != socket.AF_UNIX:
140
+ raise ValueError("Socket must be a Unix domain socket")
141
+
142
+ self._socket = sock
143
+ self._leave_open = leave_open
144
+ self._closed = False
145
+ self._end_of_stream = False
146
+
147
+ @classmethod
148
+ def connect(cls, socket_path: str) -> "UnixSocketFrameSource":
149
+ """
150
+ Connect to a Unix socket path and create a frame source.
151
+
152
+ Args:
153
+ socket_path: Path to Unix socket file
154
+
155
+ Returns:
156
+ Connected frame source
157
+ """
158
+ sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
159
+ sock.connect(socket_path)
160
+ return cls(sock, leave_open=False)
161
+
162
+ @classmethod
163
+ async def connect_async(cls, socket_path: str) -> "UnixSocketFrameSource":
164
+ """
165
+ Connect to a Unix socket path asynchronously and create a frame source.
166
+
167
+ Args:
168
+ socket_path: Path to Unix socket file
169
+
170
+ Returns:
171
+ Connected frame source
172
+ """
173
+ sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
174
+ sock.setblocking(False)
175
+ loop = asyncio.get_event_loop()
176
+ await loop.sock_connect(sock, socket_path)
177
+ return cls(sock, leave_open=False)
178
+
179
+ @property
180
+ def has_more_frames(self) -> bool:
181
+ """Check if more frames available."""
182
+ return not self._closed and not self._end_of_stream
183
+
184
+ def _recv_exactly(self, n: int) -> Optional[bytes]:
185
+ """Receive exactly n bytes from socket."""
186
+ data = b""
187
+ while len(data) < n:
188
+ chunk = self._socket.recv(n - len(data))
189
+ if not chunk:
190
+ return data if data else None
191
+ data += chunk
192
+ return data
193
+
194
+ async def _recv_exactly_async(self, n: int) -> Optional[bytes]:
195
+ """Receive exactly n bytes from socket asynchronously."""
196
+ loop = asyncio.get_event_loop()
197
+ data = b""
198
+ while len(data) < n:
199
+ chunk = await loop.sock_recv(self._socket, n - len(data))
200
+ if not chunk:
201
+ return data if data else None
202
+ data += chunk
203
+ return data
204
+
205
+ def read_frame(self) -> Optional[bytes]:
206
+ """Read frame with 4-byte length prefix from Unix socket."""
207
+ if self._closed or self._end_of_stream:
208
+ return None
209
+
210
+ # Read 4-byte length prefix
211
+ length_data = self._recv_exactly(4)
212
+ if length_data is None or len(length_data) < 4:
213
+ self._end_of_stream = True
214
+ return None
215
+
216
+ frame_length = struct.unpack("<I", length_data)[0]
217
+
218
+ if frame_length == 0:
219
+ return b""
220
+
221
+ if frame_length > self.MAX_FRAME_SIZE:
222
+ raise ValueError(f"Frame length {frame_length} exceeds maximum {self.MAX_FRAME_SIZE}")
223
+
224
+ # Read frame data
225
+ frame_data = self._recv_exactly(frame_length)
226
+ if frame_data is None or len(frame_data) < frame_length:
227
+ self._end_of_stream = True
228
+ raise ValueError(
229
+ f"Incomplete frame data: expected {frame_length}, "
230
+ f"got {len(frame_data) if frame_data else 0}"
231
+ )
232
+
233
+ return frame_data
234
+
235
+ async def read_frame_async(self) -> Optional[bytes]:
236
+ """Read frame asynchronously."""
237
+ if self._closed or self._end_of_stream:
238
+ return None
239
+
240
+ # Read 4-byte length prefix
241
+ length_data = await self._recv_exactly_async(4)
242
+ if length_data is None or len(length_data) < 4:
243
+ self._end_of_stream = True
244
+ return None
245
+
246
+ frame_length = struct.unpack("<I", length_data)[0]
247
+
248
+ if frame_length == 0:
249
+ return b""
250
+
251
+ if frame_length > self.MAX_FRAME_SIZE:
252
+ raise ValueError(f"Frame length {frame_length} exceeds maximum {self.MAX_FRAME_SIZE}")
253
+
254
+ # Read frame data
255
+ frame_data = await self._recv_exactly_async(frame_length)
256
+ if frame_data is None or len(frame_data) < frame_length:
257
+ self._end_of_stream = True
258
+ raise ValueError(
259
+ f"Incomplete frame data: expected {frame_length}, "
260
+ f"got {len(frame_data) if frame_data else 0}"
261
+ )
262
+
263
+ return frame_data
264
+
265
+ def close(self) -> None:
266
+ """Close the Unix socket source."""
267
+ if self._closed:
268
+ return
269
+ self._closed = True
270
+ if not self._leave_open:
271
+ with contextlib.suppress(OSError):
272
+ self._socket.shutdown(socket.SHUT_RD)
273
+ self._socket.close()
274
+
275
+ async def close_async(self) -> None:
276
+ """Close the Unix socket source asynchronously."""
277
+ self.close()
278
+
279
+
280
+ class UnixSocketServer:
281
+ """
282
+ Helper class to create a Unix socket server that accepts connections.
283
+ """
284
+
285
+ def __init__(self, socket_path: str):
286
+ """
287
+ Create a Unix socket server.
288
+
289
+ Args:
290
+ socket_path: Path to Unix socket file
291
+ """
292
+ self._socket_path = socket_path
293
+ self._socket: Optional[socket.socket] = None
294
+
295
+ def start(self) -> None:
296
+ """Start listening on the Unix socket."""
297
+ # Remove existing socket file if present
298
+ if os.path.exists(self._socket_path):
299
+ os.unlink(self._socket_path)
300
+
301
+ self._socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
302
+ self._socket.bind(self._socket_path)
303
+ self._socket.listen(1)
304
+
305
+ def accept(self) -> socket.socket:
306
+ """Accept a connection (blocking)."""
307
+ if self._socket is None:
308
+ raise ValueError("Server not started")
309
+
310
+ client, _ = self._socket.accept()
311
+ return client
312
+
313
+ async def accept_async(self) -> socket.socket:
314
+ """Accept a connection asynchronously."""
315
+ if self._socket is None:
316
+ raise ValueError("Server not started")
317
+
318
+ loop = asyncio.get_event_loop()
319
+ self._socket.setblocking(False)
320
+ client, _ = await loop.sock_accept(self._socket)
321
+ return client
322
+
323
+ def stop(self) -> None:
324
+ """Stop the server and clean up the socket file."""
325
+ if self._socket:
326
+ self._socket.close()
327
+ self._socket = None
328
+
329
+ if os.path.exists(self._socket_path):
330
+ os.unlink(self._socket_path)
331
+
332
+ def __enter__(self) -> "UnixSocketServer":
333
+ """Context manager entry."""
334
+ self.start()
335
+ return self
336
+
337
+ def __exit__(self, *args: object) -> None:
338
+ """Context manager exit."""
339
+ self.stop()