Functions

The following functions are available globally.

  • Waits for a connection request to arrive on the given socket.

    The function returns when a connection has been accepted, an error or a timeout occured. This function does not close the accepting socket, even in the case of an error.

    Declaration

    Swift

    public func tipAccept(
        onSocket socket: Int32,
        timeout: TimeInterval,
        addressHandler: AddressHandler? = nil) -> TipAcceptResult

    Parameters

    socket

    The socket on which to listen for connection requests.

    timeout

    The maximum duration this function will wait for a connection request to arrive.

    addressHandler

    A closure to be invoked when a connection request has been accepted.

    Return Value

    See the TipAcceptResult definition.

  • Transmits the data from the given buffer to the specified socket. The socket will remain open after the transfer (succesful or not).

    Declaration

    Swift

    public func tipTransfer(
        socket: Int32,
        buffer: UnsafeBufferPointer<UInt8>,
        timeout: TimeInterval,
        callback: TransmitterProtocol? = nil,
        progress: TransmitterProgressMonitor? = nil) -> TransferResult

    Parameters

    socket

    The socket on which to transfer the given data.

    buffer

    A pointer to a buffer containing the bytes to be transferred.

    timeout

    The time in seconds for the complete transfer attempt.

    callback

    An object that will receive the TransmitterProtocol method calls (if present).

    progress

    A closure that will be activated to keep track of the progress of the transfer.

    Return Value

    See the TransferResult definition.

  • Transfers the given data. The socket will remain open after the transfer (succesful or not).

    Declaration

    Swift

    public func tipTransfer(
        socket: Int32,
        data: Data,
        timeout: TimeInterval,
        callback: TransmitterProtocol? = nil,
        progress: TransmitterProgressMonitor? = nil) -> TransferResult

    Parameters

    socket

    The socket on which to transfer the given data.

    data

    Data containing the bytes to be transferred.

    timeout

    The time in seconds for the complete transfer attempt.

    callback

    An object that will receive the TransmitterProtocol methods calls (if present).

    progress

    A closure that will be activated to keep track of the progress of the transfer.

    Return Value

    See the TransferResult definition.

  • Transmits the given string. The socket will remain open after the transfer (succesful or not).

    Declaration

    Swift

    public func tipTransfer(
        socket: Int32,
        string: String,
        timeout: TimeInterval,
        callback: TransmitterProtocol? = nil,
        progress: TransmitterProgressMonitor? = nil) -> TransferResult

    Parameters

    socket

    The socket on which to transfer the given data.

    string

    The string to be transferred as UTF8 encoded data.

    timeout

    The time in seconds for the complete transfer attempt.

    callback

    An object that will receive the TransmitterProtocol methods calls (if present).

    progress

    A closure that will be activated to keep tracks of the progress of the transfer.

    Return Value

    See the TransferResult definition.

  • Connects a socket to a server.

    Declaration

    Swift

    public func connectToTipServer(atAddress address: String, atPort port: String) -> Result<Int32>

    Parameters

    address

    A string with either the server URL or its IP address.

    port

    A string with the portnumber on which to connect to the server.

    Return Value

    Either .success(socket: Int32) or .error(message: String).

  • Connects a socket to a server and returns a Connection object on success.

    Declaration

    Swift

    public func connectToTipServer(
        atAddress address: String,
        atPort port: String,
        connectionObjectFactory: ConnectionObjectFactory) -> Result<Connection>

    Parameters

    address

    A string with either the server URL or its IP address.

    port

    A string with the portnumber on which to connect to the server.

    connectionObjectFactory

    A closure returning the connection object when a connection was established. The receiver of that connection will have been started.

    Return Value

    Either .success(connection: Connection) or .error(message: String)

  • This function loops and calls out to the ReceiverProtocol data (if present) for received data and interface events. The loop does not terminate until a ReceiverProtocol method returns a status indicating termination, or an error occured.

    Declaration

    Swift

    public func tipReceiverLoop(
        socket: Int32,
        bufferSize: Int,
        duration: TimeInterval,
        receiver: ReceiverProtocol?)

    Parameters

    socket

    The socket to use for this operation.

    bufferSize

    The size of the buffer that will be allocated for the data to be received.

    duration

    The duration of the receive loop. I.e. the time between two successive ‘select’ calls if no events occur.

    receiver

    The target that implements the ReceiverProtocol. If not provided, the receive operation will function as a data sink until an error occurs.

  • Sets up a socket for listening on the specified service port number. It will listen on all available IP addresses of the server, either in IPv4 or IPv6.

    Declaration

    Swift

    public func setupTipServer(onPort port: String, maxPendingConnectionRequest: Int32) -> Result<Int32>

    Parameters

    port

    A string containing the number of the port to listen on.

    maxPendingConnectionRequest

    The number of connections that can be kept pending before they are accepted. A connection request can be put into a queue before it is accepted or rejected. This argument specifies the size of the queue. If the queue is full further connection requests will be rejected.

    Return Value

    Either .success(socket: Int32) or .error(message: String).

  • Wait until the POSIX select call returns for the requested event(s). If no event occurs within the timeout period, the .timeout value is returned.

    Declaration

    Swift

    public func waitForSelect(socket: Int32, timeout: Date, forRead: Bool, forWrite: Bool) -> SelectResult

    Parameters

    socket

    The socket on which something must happen.

    timeout

    The time until the select call waits for an event

    forRead

    Wait for a read event.

    forWrite

    Wait for a write event.

    Return Value

    A SelectResult value.

  • Verifies if the given string is a valid IPv4 or IPv6 address by converting it to a network address and back again. If the result equals the input, it must be correct.

    Declaration

    Swift

    public func isValidIpAddress(_ address: String) -> Bool

    Parameters

    address

    A string containing the address specification.

    Return Value

    True if the string is a valid inet address, false otherwise

  • Returns the (ipAddress, portNumber) tuple for a given sockaddr if available.

    Declaration

    Swift

    public func sockaddrDescription(_ addr: UnsafePointer<sockaddr>) -> (ipAddress: String?, portNumber: String?)

    Parameters

    addr

    A pointer to a sockaddr structure.

    Return Value

    (nil, nil) on failure, (ipAddress, portNumber) on success.

  • Replacement for FD_ZERO macro.

    Declaration

    Swift

    public func fdZero(_ set: inout fd_set)

    Parameters

    set

    A pointer to a fd_set structure.

    Return Value

    The set that is opinted at is filled with all zero’s.

  • Replacement for FD_SET macro

    Note

    If you receive an EXC_BAD_INSTRUCTION at the mask statement, then most likely the socket was already closed.

    Declaration

    Swift

    public func fdSet(_ fd: Int32?, set: inout fd_set)

    Parameters

    fd

    A file descriptor that offsets the bit to be set to 1 in the fd_set pointed at by ‘set’.

    set

    A pointer to a fd_set structure.

    Return Value

    The given set is updated in place, with the bit at offset ‘fd’ set to 1.

  • Replacement for FD_CLR macro

    Declaration

    Swift

    public func fdClr(_ fd: Int32?, set: inout fd_set)

    Parameters

    fd

    A file descriptor that offsets the bit to be cleared in the fd_set pointed at by ‘set’.

    set

    A pointer to a fd_set structure.

    Return Value

    The given set is updated in place, with the bit at offset ‘fd’ cleared to 0.

  • Replacement for FD_ISSET macro

    Declaration

    Swift

    public func fdIsSet(_ fd: Int32?, set: inout fd_set) -> Bool

    Parameters

    fd

    A file descriptor that offsets the bit to be tested in the fd_set pointed at by ‘set’.

    set

    A pointer to a fd_set structure.

    Return Value

    ‘true’ if the bit at offset ‘fd’ is 1, ‘false’ otherwise.

  • Returns all IP addresses in the addrinfo structure as a String.

    Declaration

    Swift

    public func logAddrInfoIPAddresses(_ infoPtr: UnsafeMutablePointer<addrinfo>) -> String

    Parameters

    infoPtr

    A pointer to an addrinfo structure of which the IP addresses should be logged.

    Return Value

    A string with the IP Addresses of all entries in the infoPtr addrinfo structure chain.

  • Returns a string with all socket options.

    Declaration

    Swift

    public func logSocketOptions(_ socket: Int32) -> String

    Parameters

    socket

    The socket of which to log the options.

    Return Value

    A string with all socket options of the given socket.

  • Closes the given socket if not nil.

    This method is supplied to have a single place that closes all sockets. During debugging it is often good to create a logging entry for the calls on the unix sockets. This method prevents having to look through all code to find all occurances of the close call.

    Declaration

    Swift

    public func closeSocket(_ socket: Int32?) -> Bool?

    Return Value

    True if the port was closed, nil if it was closed already and false if an error occured (errno will contain an error reason).