Skip to content

Commit

Permalink
Added inactivityDetectionRestart to transfer calls
Browse files Browse the repository at this point in the history
  • Loading branch information
Balancingrock committed Apr 9, 2017
1 parent 2ff1ec0 commit 7fc9cde
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ Note: Planned releases are for information only, they are subject to change with

- The current verion will be upgraded to 1.0.0 status when the full set necessary for Swiftfire 1.0.0 has been completed.

#### v0.10.4 (Current)
#### v0.10.5 (Current)

- Added affectInactivityDetection parameter (default = true) to the transfer calls.

#### v0.10.4

- Bugfix: The sQueue in SwifterSocket.Connection could be deallocated before its processes were complete.

Expand Down
41 changes: 30 additions & 11 deletions Sources/SwifterSockets.Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// File: SwifterSockets.Connection.swift
// Project: SwifterSockets
//
// Version: 0.10.4
// Version: 0.10.5
//
// Author: Marinus van der Lugt
// Company: http://balancingrock.nl
Expand Down Expand Up @@ -48,6 +48,7 @@
//
// History
//
// 0.10.5 - Added affectInactivityDetection to the transfer calls.
// 0.10.4 - Fixed sQueue deallocation problem by making it static.
// 0.9.15 - Added inactivity detection.
// 0.9.14 - Updated the transfer protocol methods to include the buffer pointer.
Expand Down Expand Up @@ -606,6 +607,7 @@ open class Connection: ReceiverProtocol, TransmitterProtocol {
/// - Parameters:
/// - buffer: The pointer to a buffer with the bytes to be transferred. The callee must ensure that the buffer remains allocated until the transfer is complete.
/// - timeout: The timeout for the data transfer.
/// - affectInactivityDetection: Set to 'false' to not affect the inactivity timeout detection logic.
/// - callback: The receiver for the TransmitterProtocol method calls.
/// - progress: The closure that is invoked after partial transfers.
///
Expand All @@ -615,12 +617,15 @@ open class Connection: ReceiverProtocol, TransmitterProtocol {
public func transfer(
_ buffer: UnsafeBufferPointer<UInt8>,
timeout: TimeInterval? = nil,
affectInactivityDetection: Bool = true,
callback: TransmitterProtocol? = nil,
progress: TransmitterProgressMonitor? = nil) -> TransferResult {

if let queue = tqueue() {

Connection.sQueue.sync { pendingTransfers.increment() }
if affectInactivityDetection {
Connection.sQueue.sync { pendingTransfers.increment() }
}

queue.async {

Expand All @@ -632,7 +637,9 @@ open class Connection: ReceiverProtocol, TransmitterProtocol {
callback: callback ?? self?.transmitterProtocol ?? self,
progress: progress ?? self?.transmitterProgressMonitor)

self?.inactivityDetectionRestartForEndOfQueuedTransfer()
if affectInactivityDetection {
self?.inactivityDetectionRestartForEndOfQueuedTransfer()
}
}

return .queued(id: Int(bitPattern: buffer.baseAddress))
Expand All @@ -647,7 +654,7 @@ open class Connection: ReceiverProtocol, TransmitterProtocol {
callback: callback ?? transmitterProtocol ?? self,
progress: progress ?? transmitterProgressMonitor) ?? .error(message: "Interface no longer available")

inactivityDetectionRestart()
if affectInactivityDetection { inactivityDetectionRestart() }

return result
}
Expand All @@ -661,6 +668,7 @@ open class Connection: ReceiverProtocol, TransmitterProtocol {
/// - Parameters:
/// - data: A data object containing the bytes to be transferred. ___The callee must ensure that this object remains allocated until the transfer is complete.___
/// - timeout: The timeout for the data transfer.
/// - affectInactivityDetection: Set to 'false' to not affect the inactivity timeout detection logic.
/// - callback: The receiver for the TransmitterProtocol method calls.
/// - progress: The closure that is invoked after partial transfers.
///
Expand All @@ -670,12 +678,13 @@ open class Connection: ReceiverProtocol, TransmitterProtocol {
public func transfer(
_ data: Data,
timeout: TimeInterval? = nil,
affectInactivityDetection: Bool = true,
callback: TransmitterProtocol? = nil,
progress: TransmitterProgressMonitor? = nil) -> TransferResult {

return data.withUnsafeBytes { (ptr: UnsafePointer<UInt8>) -> TransferResult in
let buffer = UnsafeBufferPointer<UInt8>.init(start: ptr, count: data.count)
return self.transfer(buffer, timeout: timeout, callback: callback, progress: progress)
return self.transfer(buffer, timeout: timeout, affectInactivityDetection: affectInactivityDetection, callback: callback, progress: progress)
}
}

Expand All @@ -687,6 +696,7 @@ open class Connection: ReceiverProtocol, TransmitterProtocol {
/// - Parameters:
/// - string: The string to be transferred coded in UTF-8. ___The callee must ensure that this object remains allocated until the transfer is complete.___
/// - timeout: The timeout for the data transfer.
/// - affectInactivityDetection: Set to 'false' to not affect the inactivity timeout detection logic.
/// - callback: The receiver for the TransmitterProtocol method calls.
/// - progress: The closure that is invoked after partial transfers.
///
Expand All @@ -696,11 +706,12 @@ open class Connection: ReceiverProtocol, TransmitterProtocol {
public func transfer(
_ string: String,
timeout: TimeInterval? = nil,
affectInactivityDetection: Bool = true,
callback: TransmitterProtocol? = nil,
progress: TransmitterProgressMonitor? = nil) -> TransferResult {

if let data = string.data(using: String.Encoding.utf8) {
return self.transfer(data, timeout: timeout, callback: callback, progress: progress)
return self.transfer(data, timeout: timeout, affectInactivityDetection: affectInactivityDetection, callback: callback, progress: progress)
} else {
_ = transmitterProgressMonitor?(0, 0)
(callback ?? self).transmitterError(0, "Cannot convert string to UTF8")
Expand All @@ -714,6 +725,7 @@ open class Connection: ReceiverProtocol, TransmitterProtocol {
/// - Parameters:
/// - buffer: The pointer to a buffer with the bytes to be transferred.
/// - timeout: The timeout for the data transfer.
/// - affectInactivityDetection: Set to 'false' to not affect the inactivity timeout detection logic.
/// - callback: The receiver for the TransmitterProtocol method calls.
/// - progress: The closure that is invoked after partial transfers.
///
Expand All @@ -723,12 +735,15 @@ open class Connection: ReceiverProtocol, TransmitterProtocol {
public func bufferedTransfer(
_ buffer: UnsafeBufferPointer<UInt8>,
timeout: TimeInterval? = nil,
affectInactivityDetection: Bool = true,
callback: TransmitterProtocol? = nil,
progress: TransmitterProgressMonitor? = nil) -> TransferResult {

if let queue = tqueue() {

Connection.sQueue.sync { pendingTransfers.increment() }
if affectInactivityDetection {
Connection.sQueue.sync { pendingTransfers.increment() }
}

let copy = UnsafeMutableRawBufferPointer.allocate(count: buffer.count)
memcpy(copy.baseAddress, buffer.baseAddress, buffer.count)
Expand All @@ -743,7 +758,7 @@ open class Connection: ReceiverProtocol, TransmitterProtocol {
callback: callback ?? self?.transmitterProtocol ?? self,
progress: progress ?? self?.transmitterProgressMonitor)

self?.inactivityDetectionRestartForEndOfQueuedTransfer()
if affectInactivityDetection { self?.inactivityDetectionRestartForEndOfQueuedTransfer() }

copy.deallocate()
}
Expand All @@ -760,7 +775,7 @@ open class Connection: ReceiverProtocol, TransmitterProtocol {
callback: callback ?? transmitterProtocol ?? self,
progress: progress ?? transmitterProgressMonitor) ?? .error(message: "Interface no longer available")

inactivityDetectionRestart()
if affectInactivityDetection { inactivityDetectionRestart() }

return result
}
Expand All @@ -772,6 +787,7 @@ open class Connection: ReceiverProtocol, TransmitterProtocol {
/// - Parameters:
/// - data: A data object containing the bytes to be transferred.
/// - timeout: The timeout for the data transfer.
/// - affectInactivityDetection: Set to 'false' to not affect the inactivity timeout detection logic.
/// - callback: The receiver for the TransmitterProtocol method calls.
/// - progress: The closure that is invoked after partial transfers.
///
Expand All @@ -781,12 +797,13 @@ open class Connection: ReceiverProtocol, TransmitterProtocol {
public func bufferedTransfer(
_ data: Data,
timeout: TimeInterval? = nil,
affectInactivityDetection: Bool = true,
callback: TransmitterProtocol? = nil,
progress: TransmitterProgressMonitor? = nil) -> TransferResult {

return data.withUnsafeBytes { (ptr: UnsafePointer<UInt8>) -> TransferResult in
let buffer = UnsafeBufferPointer<UInt8>.init(start: ptr, count: data.count)
return self.bufferedTransfer(buffer, timeout: timeout, callback: callback, progress: progress)
return self.bufferedTransfer(buffer, timeout: timeout, affectInactivityDetection: affectInactivityDetection, callback: callback, progress: progress)
}
}

Expand All @@ -796,6 +813,7 @@ open class Connection: ReceiverProtocol, TransmitterProtocol {
/// - Parameters:
/// - string: The string to be transferred coded in UTF-8.
/// - timeout: The timeout for the data transfer.
/// - affectInactivityDetection: Set to 'false' to not affect the inactivity timeout detection logic.
/// - callback: The receiver for the TransmitterProtocol method calls.
/// - progress: The closure that is invoked after partial transfers.
///
Expand All @@ -805,11 +823,12 @@ open class Connection: ReceiverProtocol, TransmitterProtocol {
public func bufferedTransfer(
_ string: String,
timeout: TimeInterval? = nil,
affectInactivityDetection: Bool = true,
callback: TransmitterProtocol? = nil,
progress: TransmitterProgressMonitor? = nil) -> TransferResult {

if let data = string.data(using: String.Encoding.utf8) {
return self.bufferedTransfer(data, timeout: timeout, callback: callback, progress: progress)
return self.bufferedTransfer(data, timeout: timeout, affectInactivityDetection: affectInactivityDetection, callback: callback, progress: progress)
} else {
_ = transmitterProgressMonitor?(0, 0)
(callback ?? self).transmitterError(0, "Cannot convert string to UTF8")
Expand Down

0 comments on commit 7fc9cde

Please sign in to comment.