Connection
open class Connection: ReceiverProtocol, TransmitterProtocol
Objects of this class represents a connection with another computer.
It is intended to be subclassed for connections that are able to receive data. A Connection object has default implementations for the ReceiverProtocol and TransmitterProtocol. A subclass should override the methods it needs.
Note
Every connection object is made ready for use with the prepare
method. The init
is ineffective for that.
Note
By default a connection stays open until the peer closes it. This is normally unacceptable for a server!
-
Initialization options for a Connection object.
See moreDeclaration
Swift
public enum Option
-
The queue on which the transmissions will take place, if present.
Declaration
Swift
public private(set) var transmitterQueue: DispatchQueue?
-
Undocumented
Declaration
Swift
open class Connection: ReceiverProtocol, TransmitterProtocol
-
The timeout for transmission on this connection.
Declaration
Swift
public private(set) var transmitterTimeoutValue: TimeInterval = 10
-
Undocumented
Declaration
Swift
open class Connection: ReceiverProtocol, TransmitterProtocol
-
Undocumented
Declaration
Swift
open class Connection: ReceiverProtocol, TransmitterProtocol
-
The queue on which the receiver will run
Declaration
Swift
public private(set) var receiverQueue: DispatchQueue?
-
Undocumented
Declaration
Swift
open class Connection: ReceiverProtocol, TransmitterProtocol
-
The duration of a single receiver loop when no activity takes place
Declaration
Swift
public private(set) var receiverLoopDuration: TimeInterval = 5
-
The size of the reciever buffer
Declaration
Swift
public private(set) var receiverBufferSize: Int = 20 * 1024
-
When no activity was detected for this amount of time, the inactivity action will be started.
Declaration
Swift
public private(set) var inactivityDetectionThreshold: TimeInterval?
-
The handler for the inactivity detection
Declaration
Swift
public private(set) var inactivityAction: InactivityHandler?
-
The error handler that wil receive error messages (if provided)
Declaration
Swift
public private(set) var errorHandler: ErrorHandler?
-
Undocumented
Declaration
Swift
open class Connection: ReceiverProtocol, TransmitterProtocol
-
The remote computer’s address.
Declaration
Swift
public private(set) var remoteAddress: String = "-"
-
The initialiser is parameterless to be able to create untyped connetions. This allows the creation of connection pools of reusable connection objects. Connection objects must be prepeared for use by calling one of the
prepare
methods.Declaration
Swift
public init()
-
Prepares the internal status of this object for usage.
Note
Every time it is called it will first reset all internal members to their default state.
Note
Overriding: must call super.
Declaration
Swift
public func prepare(for interface: InterfaceAccess, remoteAddress address: String, options: Option...) -> Bool
Parameters
interface
An InterfaceAccess glue object.
address
The address of the peer.
options
A set of options, see Connection.Object definition.
Return Value
True if the initialization was successful. False if not. Currently the only reason for failure is if the connection object is still in use.
-
Prepares the internal status of this object for usage.
Note
Every time it is called it will first reset all internal members to their default state.
Note
Overriding: must call super.
Declaration
Swift
open func prepare(for interface: InterfaceAccess, remoteAddress address: String, options: [Option]) -> Bool
Parameters
interface
An InterfaceAccess glue object.
address
The address of the peer.
options
A set of options, see Connection.Object definition.
Return Value
True if the initialization was successful. False if not. Currently the only reason for failure is if the connection object is still in use.
-
Resets the internal members of this object to their default state.
Note
Overriding: must call super.Declaration
Swift
open func reset()
-
Call this function to indicate that the inactivity timeout detection has to be restarted
Declaration
Swift
public func inactivityDetectionRestart()
-
Transfer the content of the given buffer to the peer.
Declaration
Swift
public func transfer( _ buffer: UnsafeBufferPointer<UInt8>, timeout: TimeInterval? = nil, callback: TransmitterProtocol? = nil, progress: TransmitterProgressMonitor? = nil) -> TransferResult
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.
callback
The receiver for the TransmitterProtocol method calls.
progress
The closure that is invoked after partial transfers.
Return Value
If the operation takes place on a dispatch queue, nil will be returned. If the operation is executed in-line the return value will indicate the success/failure conditions that occured. Note that this will be the duplicate information a potential callback operation will have received.
-
Transfer the content of the given data object to the peer.
Note
The callee must ensure that this object remains allocated until the transfer is complete.
Declaration
Swift
public func transfer( _ data: Data, timeout: TimeInterval? = nil, callback: TransmitterProtocol? = nil, progress: TransmitterProgressMonitor? = nil) -> TransferResult
Parameters
data
A data object containing the bytes to be transferred.
timeout
The timeout for the data transfer.
callback
The receiver for the TransmitterProtocol method calls.
progress
The closure that is invoked after partial transfers.
Return Value
If the operation takes place on a dispatch queue, .queued(id) will be returned. If the operation is executed in-line the return value will indicate the success/failure conditions that occured. Note that this will be the duplicate information a potential callback operation will have received.
-
Transfer the content of the given string to the peer.
Note
The callee must ensure that this object remains allocated until the transfer is complete.
Declaration
Swift
public func transfer( _ string: String, timeout: TimeInterval? = nil, callback: TransmitterProtocol? = nil, progress: TransmitterProgressMonitor? = nil) -> TransferResult
Parameters
string
The string to be transferred coded in UTF-8.
timeout
The timeout for the data transfer.
callback
The receiver for the TransmitterProtocol method calls.
progress
The closure that is invoked after partial transfers.
Return Value
If the operation takes place on a dispatch queue, .queued(id) will be returned. If the operation is executed in-line the return value will indicate the success/failure conditions that occured. Note that this will be the duplicate information a potential callback operation will have received.
-
Copy the content of the given buffer and transfer that to the peer. The original buffer can immediately be used again or deallocated.
Declaration
Swift
public func bufferedTransfer( _ buffer: UnsafeBufferPointer<UInt8>, timeout: TimeInterval? = nil, callback: TransmitterProtocol? = nil, progress: TransmitterProgressMonitor? = nil) -> TransferResult
Parameters
buffer
The pointer to a buffer with the bytes to be transferred.
timeout
The timeout for the data transfer.
callback
The receiver for the TransmitterProtocol method calls.
progress
The closure that is invoked after partial transfers.
Return Value
If the operation takes place on a dispatch queue, nil will be returned. If the operation is executed in-line the return value will indicate the success/failure conditions that occured. Note that this will be the duplicate information a potential callback operation will have received.
-
Copy the content of the given data object and transfer that to the peer. The original data object can immediately be used again or deallocated.
Declaration
Swift
public func bufferedTransfer( _ data: Data, timeout: TimeInterval? = nil, callback: TransmitterProtocol? = nil, progress: TransmitterProgressMonitor? = nil) -> TransferResult
Parameters
data
A data object containing the bytes to be transferred.
timeout
The timeout for the data transfer.
callback
The receiver for the TransmitterProtocol method calls.
progress
The closure that is invoked after partial transfers.
Return Value
If the operation takes place on a dispatch queue, .queued(id) will be returned. If the operation is executed in-line the return value will indicate the success/failure conditions that occured. Note that this will be the duplicate information a potential callback operation will have received.
-
Copy the content of the given string and transfer that to the peer. The original string can immediately be used again or deallocated.
Declaration
Swift
public func bufferedTransfer( _ string: String, timeout: TimeInterval? = nil, callback: TransmitterProtocol? = nil, progress: TransmitterProgressMonitor? = nil) -> TransferResult
Parameters
string
The string to be transferred coded in UTF-8.
timeout
The timeout for the data transfer.
callback
The receiver for the TransmitterProtocol method calls.
progress
The closure that is invoked after partial transfers.
Return Value
If the operation takes place on a dispatch queue, .queued(id) will be returned. If the operation is executed in-line the return value will indicate the success/failure conditions that occured. Note that this will be the duplicate information a potential callback operation will have received.
-
If a transmitter queue is used, the abortConnection will be scheduled on the transmitter queue after all scheduled transfers have taken place.
Note
Overriding: must call super.Declaration
Swift
public func closeConnection()
-
Immediately closes the connection and frees resources.
Note
Child classes should override this function to release any additional resources that have been allocated. Must call super.Declaration
Swift
open func abortConnection()
-
Starts the receiver loop. From now on the receiver protocol will be used to handle data transfer related issues.
Declaration
Swift
public func startReceiverLoop()
-
Default implementation: Does nothing.
Note
No need to call super when overriden.Declaration
Swift
open func transmitterReady(_ id: Int)
-
Default implementation: Closes the connection to the client from the server side immediately.
Note
If overriden, call super.transmitterClosed at the end.Declaration
Swift
open func transmitterClosed(_ id: Int)
-
Default implementation: Closes the connection to the client from the server side immediately.
Note
If overriden, call super.transmitterTimeout at the end.Declaration
Swift
open func transmitterTimeout(_ id: Int)
-
Default implementation: Closes the connection to the client from the server side immediately.
Note
If overriden, call super.transmitterError at the end.Declaration
Swift
open func transmitterError(_ id: Int, _ message: String)
-
Default implementation: Closes the connection to the client from the server side immediately.
Note
If overriden, call super.receiverClosed at the end.Declaration
Swift
open func receiverClosed()
-
Default implementation: Does nothing.
Note
No need to call super when overriden.Declaration
Swift
open func receiverLoop() -> Bool
-
Default implementation: Closes the connection to the client from the server side immediately.
Note
If overriden, call super.receiverError at the end.Declaration
Swift
open func receiverError(_ message: String)
-
Default implementation: Does nothing.
Note
Must call super when overriden or alternatively callsuper.inactivityDetectionRestart
Declaration
Swift
open func receiverData(_ buffer: UnsafeBufferPointer<UInt8>) -> Bool