95 lines
3.3 KiB
Dart
95 lines
3.3 KiB
Dart
import 'dart:io';
|
|
import 'dart:typed_data';
|
|
import 'package:meshport_flutter/meshport_flutter.dart';
|
|
|
|
Future<void> main(List<String> arguments) async {
|
|
final path = arguments.single;
|
|
final server = await MeshPortClient.open(libraryPath: path);
|
|
final client = await MeshPortClient.open(libraryPath: path);
|
|
final osSocket = await RawDatagramSocket.bind(
|
|
InternetAddress.loopbackIPv4,
|
|
0,
|
|
);
|
|
final port = osSocket.port;
|
|
osSocket.close();
|
|
String config(String ip, bool listen) =>
|
|
'''
|
|
ipv4 = "$ip"
|
|
stun_servers = []
|
|
stun_servers_v6 = []
|
|
${listen ? 'listeners = ["udp://127.0.0.1:$port"]' : 'listeners = []\n[[peer]]\nuri = "udp://127.0.0.1:$port"'}
|
|
[network_identity]
|
|
network_name = "ffi-smoke"
|
|
network_secret = "test-only"
|
|
[flags]
|
|
default_protocol = "udp"
|
|
''';
|
|
try {
|
|
await server.start(config('10.78.0.2', true));
|
|
await client.start(config('10.78.0.11', false));
|
|
final deadline = DateTime.now().add(const Duration(seconds: 10));
|
|
while ((await client.status())['peer_count'] == 0) {
|
|
if (DateTime.now().isAfter(deadline)) {
|
|
throw StateError('Peer discovery timeout');
|
|
}
|
|
await Future<void>.delayed(const Duration(milliseconds: 100));
|
|
}
|
|
await Future<void>.delayed(const Duration(milliseconds: 500));
|
|
final a = await client.bindUdp(port: 17007);
|
|
final b = await server.bindUdp(port: 17008);
|
|
// UDP API currently receives only registered remote flows. Warm up both directions.
|
|
await server.sendUdp(b, '10.78.0.11:17007', Uint8List.fromList([0]));
|
|
await Future<void>.delayed(const Duration(milliseconds: 100));
|
|
final payload = Uint8List.fromList([1, 2, 3, 4]);
|
|
await client.sendUdp(a, '10.78.0.2:17008', payload);
|
|
final received = await server.receiveUdp(b);
|
|
if (received['bytes'].toString() != payload.toList().toString()) {
|
|
throw StateError('Data mismatch');
|
|
}
|
|
await server.sendUdp(b, '10.78.0.11:17007', payload);
|
|
var response = await client.receiveUdp(a);
|
|
if (response['bytes'].toString() == '[0]') {
|
|
response = await client.receiveUdp(a);
|
|
}
|
|
if (response['bytes'].toString() != payload.toList().toString()) {
|
|
throw StateError('Reply mismatch');
|
|
}
|
|
// Echo runs on the other context while the client waits in quality_sample.
|
|
final echo = () async {
|
|
final probe = await server.receiveUdp(b);
|
|
await server.sendUdp(
|
|
b,
|
|
probe['source'] as String,
|
|
Uint8List.fromList((probe['bytes'] as List).cast<int>()),
|
|
);
|
|
}();
|
|
final quality = await client.sampleUdpQuality(a, '10.78.0.2:17008', 42);
|
|
await echo;
|
|
if (quality['success'] != true ||
|
|
quality['elapsed_ms'] is! num ||
|
|
quality['next_hop_peer_id'] == null ||
|
|
quality['route_changed'] != false) {
|
|
throw StateError('Quality sample mismatch: $quality');
|
|
}
|
|
final timeout = await client.sampleUdpQuality(
|
|
a,
|
|
'10.78.0.2:17008',
|
|
43,
|
|
timeoutMs: 100,
|
|
);
|
|
if (timeout['success'] != false || timeout['elapsed_ms'] != null) {
|
|
throw StateError('Quality timeout mismatch: $timeout');
|
|
}
|
|
await client.closeResource(a);
|
|
await server.closeResource(b);
|
|
await client.stop();
|
|
await server.stop();
|
|
stdout.writeln(
|
|
'PASS: Dart isolate → C ABI → EasyTier UDP overlay → data verified',
|
|
);
|
|
} finally {
|
|
await client.close();
|
|
await server.close();
|
|
}
|
|
}
|