magpie-bridge (Python)
Reliable UDP Relay Network — wire protocol library for Python.
This package implements the Magpie Bridge protocol codec: packet packing, unpacking, validation, message segmentation, and acknowledgement helpers.
Installation
pip install magpie-bridge
Or install from source:
git clone https://github.com/moky/magpie-bridge.git
cd magpie-bridge/python
pip install -e .
Quick Start
from magpie_bridge import Packet, split_message, build_ack, TYPE_MINI
# Build and send a small packet
pkt = Packet(TYPE_MINI, target_bid=42, source_bid=7, sn=1, body=b"hello")
data = pkt.pack() # bytes ready for UDP send
# Parse a received packet
received = Packet.unpack(data)
print(received.body) # b"hello"
# Split a large message into segments
message = b"x" * 5000
segments = split_message(message, sn=2, source_bid=7, target_bid=42)
for seg in segments:
send(seg.pack())
# Build an acknowledgement
ack = build_ack(received)
send(ack.pack()) # body=b"OK", bids swapped
Protocol Overview
| Field | Size | Description |
|---|---|---|
| Magic Code | 4 B | MB\x00\x01 (Magpie Bridge v1) |
| Type | 1 B | packet type + parameter width |
| Head Length | 1 B | 20 / 22 / 24 / 28 |
| Body Length | 2 B | body size in octets |
| Target Bridge ID | 4 B | destination bid |
| Source Bridge ID | 4 B | originator bid |
| Serial Number | 4 B | message identity |
| index / count | 0/2/4/8 B | segment ordinal / total segments |
| Body | 0..1200 B | application payload |
- Maximum UDP payload (MSS): 1232 octets (1280 - 40 - 8, dual-stack safe).
- Maximum body size: 1200 octets.
- See
../docs/design-protocol.mdfor the full specification.
API Reference
Packet
Packet(type, target_bid, source_bid, sn, index=None, count=None, body=b"")
pack() -> bytes— serialize.Packet.unpack(data: bytes) -> Packet— parse.validate() -> bool— check whether the packet is well-formed.message_id -> int— deduplication key (snfor data packets,(sn << (type & 0x0F)) | indexfor ACKs).head_length,param_width,is_ack— derived properties.
Helpers
select_type(message_size) -> int— choose Type by message size.split_message(data, sn, source_bid, target_bid) -> list[Packet].build_ack(packet) -> Packet— build ab"OK"acknowledgement.build_control(body, sn, source_bid=0, target_bid=0) -> Packet— build a control packet (SYN/ACK/PING/PONG/FIN) with Type 0.
Exceptions
ProtocolError— raised on invalid packets or parsing failures.
Development
cd python
pip install -e ".[dev]" # or: pip install pytest
pytest
License
MIT — see LICENSE.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
magpie_bridge-0.1.0.tar.gz
(7.2 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file magpie_bridge-0.1.0.tar.gz.
File metadata
- Download URL: magpie_bridge-0.1.0.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/68.0.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.0b3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
04bdc8d3d6c620f1938de68ccc69fc615030ad85915e0af40a80d6e5a9d50fc2
|
|
| MD5 |
3d3e0aa54295333551c8bfc05da403f7
|
|
| BLAKE2b-256 |
fcca8aafa5050f848c0d93210c629e68938b363afa50b65019c0c2f1f3831086
|
File details
Details for the file magpie_bridge-0.1.0-py3-none-any.whl.
File metadata
- Download URL: magpie_bridge-0.1.0-py3-none-any.whl
- Upload date:
- Size: 7.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/68.0.0 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.0b3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b292f94f1c2a4270b827816dc46a6fe489fed1d830a2938efe2fed44c23adde
|
|
| MD5 |
2b1e95222c84961f145bbbbc79ae5cfb
|
|
| BLAKE2b-256 |
33655e391576335790a48ce10f2f9d448c6060a493f19fd467acf3d0fd511176
|