@@ -66,6 +66,8 @@ public struct DatagramDrops: Equatable {
6666 }
6767}
6868
69+ public typealias BridgeObserveFirstByteHandler = ( ( UInt8 ) -> Void ) ?
70+
6971@_spi ( Essentials)
7072@available ( Network 0 . 1 . 0 , * )
7173public struct BridgeDatagramProtocol : NetworkProtocol {
@@ -75,6 +77,7 @@ public struct BridgeDatagramProtocol: NetworkProtocol {
7577
7678 public struct BridgeOptions : PerProtocolOptions {
7779 public var linkDelay : NetworkDuration = . zero
80+ public var observeFirstByteHandler : BridgeObserveFirstByteHandler = nil
7881 var datagramDrops : DatagramDrops ?
7982
8083 init ( ) { }
@@ -96,6 +99,10 @@ public struct BridgeDatagramProtocol: NetworkProtocol {
9699 self == other
97100 }
98101
102+ static public func == ( lhs: BridgeOptions , rhs: BridgeOptions ) -> Bool {
103+ lhs. linkDelay == rhs. linkDelay && lhs. datagramDrops == rhs. datagramDrops
104+ }
105+
99106 var isDefault : Bool {
100107 self == BridgeOptions ( )
101108 }
@@ -142,6 +149,7 @@ public struct BridgeDatagramProtocol: NetworkProtocol {
142149
143150 var linkDelay : NetworkDuration = . zero
144151 var datagramDrops : DatagramDrops ? = nil
152+ var observeFirstByteHandler : BridgeObserveFirstByteHandler = nil
145153
146154 private var timerSet = false
147155 func deliverInboundDataAvailableEvent( ) {
@@ -183,6 +191,7 @@ public struct BridgeDatagramProtocol: NetworkProtocol {
183191 {
184192 self . linkDelay = bridgeOptions. linkDelay
185193 self . datagramDrops = bridgeOptions. datagramDrops
194+ self . observeFirstByteHandler = bridgeOptions. observeFirstByteHandler
186195 }
187196 #endif
188197
@@ -275,6 +284,14 @@ public struct BridgeDatagramProtocol: NetworkProtocol {
275284 datagrams = remainingDatagrams
276285 }
277286 log. datapath ( " forwarding \( datagrams. count) datagrams to port: \( remotePort) " )
287+ if let observeFirstByteHandler {
288+ datagrams. iterateMutableFrames { frame in
289+ if let bytes = frame. bytes, bytes. byteCount > 0 {
290+ observeFirstByteHandler ( bytes [ 0 ] )
291+ }
292+ return true
293+ }
294+ }
278295 remoteInstance. incomingFrames. add ( frames: datagrams)
279296 remoteInstance. deliverInboundDataAvailableEvent ( )
280297 }
@@ -323,6 +340,11 @@ extension ProtocolOptions<BridgeDatagramProtocol> {
323340 set { perProtocolOptions!. linkDelay = newValue }
324341 }
325342
343+ public var observeFirstByteHandler : BridgeObserveFirstByteHandler {
344+ get { perProtocolOptions!. observeFirstByteHandler }
345+ set { perProtocolOptions!. observeFirstByteHandler = newValue }
346+ }
347+
326348 public var datagramDrops : DatagramDrops ? {
327349 get { perProtocolOptions!. datagramDrops }
328350 set { perProtocolOptions!. datagramDrops = newValue }
0 commit comments