magpipe 1.0.0
magpipe: ^1.0.0 copied to clipboard
Magpie Client - Messaging Protocol on UDP.
// Magpie Bridge pipe transport - minimal usage example.
//
// Start the relay server (see the server project) on 127.0.0.1:9527,
// then run:
//
// dart run example/main.dart
//
// The flow follows docs/client.md: hub -> bridge handshake -> permits
// -> data exchange. The peer's bid is announced out-of-band here; the
// direct mode (B=0) connects to the peer's endpoint without the relay.
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:magpipe/magpipe.dart';
/// the app's delegate: observes every packet received through the links
class PacketPrinter implements MagpieDelegate {
@override
void onPacketReceived(Packet packet, MagPipe? pipe, MagLink link) {
// a message from the peer (A=0, D=1): print the payload
if (packet.ack == 0 && packet.dsn == 1 && packet.payload.isNotEmpty) {
print('<< ${utf8.decode(packet.payload, allowMalformed: true)}');
} else {
print('<< ${packet.runtimeType} (${packet.payloadLength}B)');
}
}
@override
void onPacketSent(Magpie packet, MagPipe? pipe, MagLink link) {}
@override
void onPacketFailed(Magpie packet, MagPipe? pipe, MagLink link) {}
@override
void onUnknownPeer(Uint8List data, SocketAddress remote) {
// most likely a peer's direct-connect "SYN?": the app decides
// whether to dial (hub.connect(remote)) or ignore
print('<< ${data.length}B from unknown peer $remote');
}
}
Future<void> main() async {
final server = (host: InternetAddress.loopbackIPv4, port: 9527);
const peer = 42; // the peer's bid, announced out-of-band
// 1. the hub binds one shared UDP socket for every connection; the
// delegate observes every packet received through the links.
// The demo server (sdk-py) registers the same demo secret 'TW9reS'
// out of the box; for production, load your shared app secrets
// from a file instead. An empty Enigma = plain handshake mode,
// accepted only by a server with encrypted mode OFF.
final enigma = Enigma();
enigma.updateSecrets({
'TW9reS': base64Decode('TW9reSBsb3ZlcyBNYXkgTGVlIGZvcmV2ZXIh'),
});
final hub = await MagHub.create(
enigma: enigma, config: Config(), delegate: PacketPrinter());
// 2. connect to the relay server (B=1): the three-way handshake runs
// in the background until the link is [ready]
final bridge = hub.connectBridge(server);
var waited = 0;
while (!bridge.ready && waited < 50) { // 50 x 100ms = 5s deadline
await Future.delayed(const Duration(milliseconds: 100));
waited++;
}
if (!bridge.ready) {
print('no relay server on ${server.host}:${server.port} - start one first');
hub.close();
return;
}
print('bridge ready, bid = ${bridge.bid}');
// 3. accept the peer's bid: data from bid 42 will be forwarded to us
bridge.accept([peer]);
// 4. send a message to the peer (sn increments per message)
var sn = 0;
final payload = Uint8List.fromList(utf8.encode('hello magpie'));
final ok = hub.getBridgePipe(server, peer)?.sendData(payload, sn: ++sn) ?? false;
print(ok ? '>> hello magpie' : '>> send failed');
// 5. direct connection to a peer (B=0, stateless handshake): the
// peer's socket address comes from out-of-band too (e.g. the
// 'observedAddress' carried by the bridge handshake)
final peerAddr = (host: InternetAddress.loopbackIPv4, port: 9528);
final directPipe = hub.getDirectPipe(peerAddr);
final directOk = directPipe?.sendData(payload, sn: ++sn) ?? false;
print(directOk ? '>> direct hello' : '>> direct send failed');
// ... keep the process alive to receive the peer's replies
for (var i = 0; i < 5; i++) {
hub.keepAlive(DateTime.now()); // drive heartbeats / re-connects
await Future.delayed(const Duration(seconds: 1));
}
hub.disconnect(server);
hub.disconnect(peerAddr);
// done: disconnects every link and closes the shared socket
hub.close();
}