diff --git a/Sources/BluetoothLinux/L2CAP.swift b/Sources/BluetoothLinux/L2CAP.swift index b225a13..15dbde5 100644 --- a/Sources/BluetoothLinux/L2CAP.swift +++ b/Sources/BluetoothLinux/L2CAP.swift @@ -229,9 +229,14 @@ public final class L2CAPSocket: L2CAPSocketProtocol { // error accepting new connection guard client >= 0 else { throw POSIXError.fromErrno! } - return L2CAPSocket(clientSocket: client, - remoteAddress: remoteAddress, - securityLevel: securityLevel) + let newSocket = L2CAPSocket(clientSocket: client, + remoteAddress: remoteAddress, + securityLevel: securityLevel) + + // make socket non-blocking + try newSocket.setNonblocking() + + return newSocket } /// Connect to another L2CAP server. @@ -252,6 +257,9 @@ public final class L2CAPSocket: L2CAPSocketProtocol { connect(internalSocket, $0, socklen_t(MemoryLayout.size)) == 0 }) }) else { throw POSIXError.fromErrno! } + + // make socket non-blocking + try setNonblocking() } /// Reads from the socket. @@ -286,9 +294,22 @@ public final class L2CAPSocket: L2CAPSocketProtocol { guard fdCount != -1 else { throw POSIXError.fromErrno! } - return readSockets.contains(internalSocket) + return fdCount > 0 } - + + private func setNonblocking() throws { + + var flags = fcntl(internalSocket, F_GETFL, 0) + + guard flags != -1 + else { throw POSIXError.fromErrno! } + + flags = fcntl(internalSocket, F_SETFL, flags | O_NONBLOCK); + + guard flags != -1 + else { throw POSIXError.fromErrno! } + } + /// Write to the socket. public func send(_ data: Data) throws { diff --git a/Sources/CSwiftBluetoothLinux/include/bluetooth.h b/Sources/CSwiftBluetoothLinux/include/bluetooth.h index f946e68..5c4b9f7 100644 --- a/Sources/CSwiftBluetoothLinux/include/bluetooth.h +++ b/Sources/CSwiftBluetoothLinux/include/bluetooth.h @@ -83,3 +83,4 @@ __attribute__((swift_name("FileDescriptorSet.contains(self:_:)"))) { return FD_ISSET(fileDescriptor, set); } +