Skip to content

Commit

Permalink
Added L2CAPSocket.event
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Apr 25, 2022
1 parent 7747bec commit 7fa6109
Showing 1 changed file with 10 additions and 13 deletions.
23 changes: 10 additions & 13 deletions Sources/BluetoothLinux/L2CAP/L2CAPSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ public final class L2CAPSocket: Bluetooth.L2CAPSocket {
/// L2CAP Socket address
public let address: BluetoothAddress

public lazy var event: L2CAPSocketEventStream = { [unowned self] in
let stream = self.socket.event
var iterator = stream.makeAsyncIterator()
return L2CAPSocketEventStream(unfolding: {
await iterator
.next()
.map { L2CAPSocketEvent($0) }
})
}()

// MARK: - Initialization

deinit {
Expand Down Expand Up @@ -154,19 +164,6 @@ public final class L2CAPSocket: Bluetooth.L2CAPSocket {

// MARK: - Methods

/// Set an event handler for the socket.
public func setEvent(_ eventHandler: @escaping ((L2CAPSocketEvent) async -> ())) async {
do {
try await socket.setEvent { socketEvent in
let event = L2CAPSocketEvent(socketEvent)
Task { await eventHandler(event) }
}
}
catch {
assertionFailure("Unable to set event")
}
}

/// Attempt to accept an incoming connection.
public func accept() async throws -> L2CAPSocket {
let (clientFileDescriptor, clientAddress) = try await socket.fileDescriptor.accept(L2CAPSocketAddress.self, sleep: 100_000_000)
Expand Down

0 comments on commit 7fa6109

Please sign in to comment.