Skip to content

Latest commit

 

History

History
60 lines (42 loc) · 1 KB

File metadata and controls

60 lines (42 loc) · 1 KB

API

Overview of the API

Usage

Connect

func toBytes() -> [UInt8] {
    // convert something to bytes ...
}

let socket = UDPConnect()
socket.connect(host: "1.2.3.4", port: 9000)

let msg: [UInt8] = toBytes()
socket.send(msg)

let msg2: String = "Hello" // unicode only, no emoji
socket.send(msg2)
let conn: NWConnection = getConnection()

let socket = UDPConnect(connection: conn)

Bind

let ip = getIPAddress()
let server = UDPBind()
server.bind(host: ip, port: 9500)

Converting to a Byte Array

struct Bob {
    let a: Double
    let b: Int16
    
    // convert this struct into an array of bytes for transmission
    // across the network
    func pack() -> [UInt8] {
        var buffer: [UInt8] = []
        buffer.append(0xfe) // header byte
        buffer.append(contentsOf: toByteArray(self.a))
        buffer.append(contentsOf: toByteArray(self.b))
        
        print("buffer size: \(buffer.count)")
        return buffer
    }
}