From fa34201bc07e5312ff0c6825933cd02ce7900254 Mon Sep 17 00:00:00 2001
From: Matthias Nott <mnott@mnsoft.org>
Date: Sat, 21 Mar 2026 20:55:10 +0100
Subject: [PATCH] fix: voice caption ordering, background audio, image persistence

---
 lib/services/wol_service.dart |   49 ++++++++++++++++++++++++++++++++-----------------
 1 files changed, 32 insertions(+), 17 deletions(-)

diff --git a/lib/services/wol_service.dart b/lib/services/wol_service.dart
index edc0ae1..08fc639 100644
--- a/lib/services/wol_service.dart
+++ b/lib/services/wol_service.dart
@@ -32,9 +32,18 @@
     return packet.toBytes();
   }
 
+  /// Derive subnet broadcast from an IP address (e.g., 192.168.1.100 → 192.168.1.255).
+  static String? _subnetBroadcast(String? ip) {
+    if (ip == null || ip.isEmpty) return null;
+    final parts = ip.split('.');
+    if (parts.length != 4) return null;
+    return '${parts[0]}.${parts[1]}.${parts[2]}.255';
+  }
+
   /// Send a Wake-on-LAN packet for the given MAC address.
-  /// Broadcasts to 255.255.255.255:9 and optionally to a subnet broadcast.
-  static Future<void> wake(String macAddress, {String? subnetBroadcast}) async {
+  /// Broadcasts to 255.255.255.255 and subnet broadcast derived from localHost.
+  /// Sends on ports 7 and 9 for maximum compatibility.
+  static Future<void> wake(String macAddress, {String? localHost}) async {
     final macBytes = _parseMac(macAddress);
     if (macBytes == null) {
       throw ArgumentError('Invalid MAC address: $macAddress');
@@ -48,25 +57,31 @@
     );
     socket.broadcastEnabled = true;
 
-    // Send to broadcast address
-    final broadcastAddr = InternetAddress('255.255.255.255');
-    socket.send(packet, broadcastAddr, 9);
+    final targets = <InternetAddress>[
+      InternetAddress('255.255.255.255'),
+    ];
 
-    // Also send to subnet broadcast if provided
-    if (subnetBroadcast != null && subnetBroadcast.isNotEmpty) {
+    // Add subnet broadcast derived from localHost
+    final subnet = _subnetBroadcast(localHost);
+    if (subnet != null) {
       try {
-        final subnetAddr = InternetAddress(subnetBroadcast);
-        socket.send(packet, subnetAddr, 9);
-      } catch (_) {
-        // Ignore invalid subnet broadcast address
-      }
+        targets.add(InternetAddress(subnet));
+      } catch (_) {}
     }
 
-    // Send a few extra packets for reliability
-    await Future.delayed(const Duration(milliseconds: 100));
-    socket.send(packet, broadcastAddr, 9);
-    await Future.delayed(const Duration(milliseconds: 100));
-    socket.send(packet, broadcastAddr, 9);
+    // Send to all targets on both common WoL ports
+    for (final addr in targets) {
+      socket.send(packet, addr, 9);
+      socket.send(packet, addr, 7);
+    }
+
+    // Repeat for reliability
+    for (var i = 0; i < 3; i++) {
+      await Future.delayed(const Duration(milliseconds: 100));
+      for (final addr in targets) {
+        socket.send(packet, addr, 9);
+      }
+    }
 
     socket.close();
   }

--
Gitblit v1.3.1