Skip to content

Commit

Permalink
Working on non-blocking reading
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Sep 12, 2018
1 parent 3376d17 commit 981076d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
31 changes: 26 additions & 5 deletions Sources/BluetoothLinux/L2CAP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -252,6 +257,9 @@ public final class L2CAPSocket: L2CAPSocketProtocol {
connect(internalSocket, $0, socklen_t(MemoryLayout<sockaddr_l2>.size)) == 0
})
}) else { throw POSIXError.fromErrno! }

// make socket non-blocking
try setNonblocking()
}

/// Reads from the socket.
Expand Down Expand Up @@ -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 {

Expand Down
1 change: 1 addition & 0 deletions Sources/CSwiftBluetoothLinux/include/bluetooth.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,4 @@ __attribute__((swift_name("FileDescriptorSet.contains(self:_:)")))
{
return FD_ISSET(fileDescriptor, set);
}

0 comments on commit 981076d

Please sign in to comment.