Skip to content

Commit c39a90a

Browse files
committed
Fix GATTPeripheral error handling
1 parent 870413f commit c39a90a

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

Sources/GATT/GATTPeripheral.swift

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -118,30 +118,35 @@ public final class GATTPeripheral <HostController: BluetoothHostControllerInterf
118118
self?.log?("Started GATT Server")
119119
do {
120120
while let socket = await self?.storage.socket, let self = self {
121-
try Task.checkCancellation()
122-
let newSocket = try await socket.accept()
123-
self.log?("[\(newSocket.address)]: New connection")
124-
let central = Central(id: socket.address)
125-
Task.detached { [weak self] in
126-
for await event in newSocket.event {
127-
switch event {
128-
case let .error(error):
129-
self?.log(central, error.localizedDescription)
130-
case .close:
131-
break
132-
default:
133-
break
121+
do {
122+
try Task.checkCancellation()
123+
let newSocket = try await socket.accept()
124+
self.log?("[\(newSocket.address)]: New connection")
125+
let central = Central(id: socket.address)
126+
Task.detached { [weak self] in
127+
for await event in newSocket.event {
128+
switch event {
129+
case let .error(error):
130+
self?.log(central, error.localizedDescription)
131+
case .close:
132+
break
133+
default:
134+
break
135+
}
134136
}
137+
await self?.didDisconnect(central)
135138
}
136-
await self?.didDisconnect(central)
139+
await self.storage.newConnection(newSocket, options: self.options, delegate: self)
140+
}
141+
catch _ as CancellationError {
142+
return
143+
}
144+
catch {
145+
self.log?("Error waiting for new connection: \(error)")
146+
try? await Task.sleep(nanoseconds: 100_000_000)
137147
}
138-
await self.storage.newConnection(newSocket, options: self.options, delegate: self)
139148
}
140149
}
141-
catch _ as CancellationError { }
142-
catch {
143-
self?.log?("Error waiting for new connection: \(error)")
144-
}
145150
})
146151
await self.storage.start(socket, task)
147152
}

0 commit comments

Comments
 (0)