Skip to content

Commit

Permalink
Make L2CAPSocket non-blocking
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Sep 12, 2018
1 parent de0eb50 commit 251d6ef
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
19 changes: 11 additions & 8 deletions Sources/ATTConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ public final class ATTConnection {
// MARK: - Methods

/// Performs the actual IO for recieving data.
public func read() throws {
public func read() throws -> Bool {

log?("Attempt read")
//log?("Attempt read")

let recievedData = try socket.recieve(Int(maximumTransmissionUnit.rawValue))
guard let recievedData = try socket.recieve(Int(maximumTransmissionUnit.rawValue))
else { return false } // no data availible to read

log?("Recieved data (\(recievedData.count) bytes)")
//log?("Recieved data (\(recievedData.count) bytes)")

// valid PDU data length
guard recievedData.count >= ATT.minimumPDULength
Expand All @@ -82,7 +83,7 @@ public final class ATTConnection {
guard let opcode = ATT.Opcode(rawValue: opcodeByte)
else { throw Error.garbageResponse(recievedData) }

log?("Recieved opcode \(opcode)")
//log?("Recieved opcode \(opcode)")

// Act on the received PDU based on the opcode type
switch opcode.type {
Expand All @@ -106,25 +107,27 @@ public final class ATTConnection {
// For all other opcodes notify the upper layer of the PDU and let them act on it.
try handle(notify: recievedData, opcode: opcode)
}

return true
}

/// Performs the actual IO for sending data.
public func write() throws -> Bool {

log?("Attempt write")
//log?("Attempt write")

guard let sendOperation = pickNextSendOpcode()
else { return false }

assert(sendOperation.data.count <= Int(maximumTransmissionUnit.rawValue), "Trying to send \(sendOperation.data.count) bytes when MTU is \(maximumTransmissionUnit)")

log?("Sending data... (\(sendOperation.data.count) bytes)")
//log?("Sending data... (\(sendOperation.data.count) bytes)")

try socket.send(sendOperation.data)

let opcode = sendOperation.opcode

log?("Did write \(opcode)")
//log?("Did write \(opcode)")

/* Based on the operation type, set either the pending request or the
* pending indication. If it came from the write queue, then there is
Expand Down
8 changes: 4 additions & 4 deletions Sources/GATTClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ public final class GATTClient {
// MARK: - Methods

/// Performs the actual IO for recieving data.
public func read() throws {
public func read() throws -> Bool {

try connection.read()
return try connection.read()
}

/// Performs the actual IO for sending data.
Expand Down Expand Up @@ -346,8 +346,8 @@ public final class GATTClient {
private func registerATTHandlers() {

// value notifications / indications
connection.register(notification)
connection.register(indication)
connection.register { [weak self] in self?.notification($0) }
connection.register { [weak self] in self?.indication($0) }
}

@inline(__always)
Expand Down
4 changes: 2 additions & 2 deletions Sources/GATTServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public final class GATTServer {
// MARK: - Methods

/// Performs the actual IO for sending data.
public func read() throws {
public func read() throws -> Bool {

try connection.read()
return try connection.read()
}

/// Performs the actual IO for recieving data.
Expand Down
2 changes: 1 addition & 1 deletion Sources/L2CAPSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public protocol L2CAPSocketProtocol {
var channelIdentifier: UInt16 { get }

/// Reads from the socket.
func recieve(_ bufferSize: Int) throws -> Data
func recieve(_ bufferSize: Int) throws -> Data?

/// Write to the socket.
func send(_ data: Data) throws
Expand Down

0 comments on commit 251d6ef

Please sign in to comment.