Disconnect only after sending last frame

This commit is contained in:
Leon Böttger authored and Leon Böttger committed 2026-03-08 14:32:07 +01:00
1 parent a1b4a075a6
commit c10facbedd
1 file changed
+18 -7
+18 -7
View File
@@ -263,7 +263,10 @@ class NearbyConnection {
UInt8(truncatingIfNeeded: length), UInt8(truncatingIfNeeded: length),
]) ])
lengthPrefixedData.append(frame) lengthPrefixedData.append(frame)
connection.send(content: lengthPrefixedData, completion: .contentProcessed { _ in connection.send(content: lengthPrefixedData, completion: .contentProcessed { error in
if let error = error {
log("[NearbyConnection \(self.id)] Error while sending frame: \(error)")
}
if let completion = completion { if let completion = completion {
completion() completion()
} }
@@ -557,14 +560,22 @@ class NearbyConnection {
offlineFrame.v1.type = .disconnection offlineFrame.v1.type = .disconnection
offlineFrame.v1.disconnection = Location_Nearby_Connections_DisconnectionFrame() offlineFrame.v1.disconnection = Location_Nearby_Connections_DisconnectionFrame()
if encryptionDone { if connectionClosed {
try encryptAndSendOfflineFrame(offlineFrame) disconnect()
} else { return
try sendFrameAsync(offlineFrame.serializedData())
} }
log("[NearbyConnection \(self.id)] Sent disconnection frame during sendDisconnectionAndDisconnect") let finishDisconnect: () -> Void = {
disconnect() log("[NearbyConnection \(self.id)] Sent disconnection frame during sendDisconnectionAndDisconnect")
self.disconnect()
}
if encryptionDone {
try encryptAndSendOfflineFrame(offlineFrame, completion: finishDisconnect)
} else {
let data = try offlineFrame.serializedData()
sendFrameAsync(data, completion: finishDisconnect)
}
} }