SwiftDSSocket

public class SwiftDSSocket: NSObject

SwiftDSSocket class definition

  • store user data that can be used for context

    Declaration

    Swift

    public var userData: Any?
  • debug option (might be deprecated in future)

    Declaration

    Swift

    public static var debugMode = false
  • this defines all kinds of error in SwiftDSSocket

    See more

    Declaration

    Swift

    public class SocketError: NSObject, Error
  • Socket Type

    • tcp: for TCP stream
    • kernel: for kernel TCP stream
    See more

    Declaration

    Swift

    public enum SocketType
  • create a new SwiftDSSocket instance by specifying delegate, delegate queue and socket type

    Declaration

    Swift

    public init(delegate: SwiftDSSocketDelegate?, delegateQueue: DispatchQueue?, type: SocketType)

    Parameters

    delegate

    specify delegate target (normally self)

    delegateQueue

    specify the GCD dispatch queue for delegate methods

    type

    socket type: tcp or kernel

  • let SwiftDSSocket connect to IPv4 in priority

    Declaration

    Swift

    public func preferIPv4()
  • listen on a specified port for both IPv4/IPv6

    Throws

    throws a SocketError

    Declaration

    Swift

    public func accept(onPort port: UInt16) throws

    Parameters

    port

    port number in UInt16

  • connect to a host using address:port

    Throws

    throws a SocketError

    Declaration

    Swift

    public func connect(toHost host: String, port: UInt16) throws

    Parameters

    host

    host address could be IP(v4/v6) or domain name in String

    port

    port number in UInt16

  • Undocumented

    Declaration

    Swift

    public class SwiftDSSocket: NSObject
  • write data to socket

    Declaration

    Swift

    public func write(data: Data, tag: Int)

    Parameters

    data

    data ready to send to socket in Data type

    tag

    a tag to mark this write operation

  • read data from socket without given data length

    Declaration

    Swift

    public func readData(tag: Int)

    Parameters

    tag

    a tag to mark this read operation

  • read specified length of data from socket the callback will be invoked right after read specified amount of data

    Declaration

    Swift

    public func readData(toLength: UInt, tag: Int)

    Parameters

    toLength

    length of data to read

    tag

    a tag to mark this read operation

  • read specified length of data into buffer that starts from offset

    users should make sure that buffer will not overflow

    Declaration

    Swift

    public func readData(toLength: UInt, buffer: UnsafeMutablePointer<UInt8>, offset: Int, tag: Int)

    Parameters

    toLength

    length of data to read

    buffer

    buffer given by user

    offset

    offset of buffer

    tag

    a tag to mark this read operation

  • disconnect socket after all read opreations queued up and prevents new read operations

    Declaration

    Swift

    public func disconnectAfterReading()
  • disconnect socket after all write opreations queued up and prevents new write operations

    Declaration

    Swift

    public func disconnectAfterWriting()
  • disconnect socket after all opreations queued up and prevents new operations

    Declaration

    Swift

    public func disconnectAfterReadingAndWriting()
  • simply disconnect socket and discards all opreations queued up

    Declaration

    Swift

    public func disconnect()