I need to use a struct to implement some network protocol.
I need to send the struct as a byte stream and when I receive a reply to convert it back to the struct.
Currently, I am using these encode and decode functions:
func encode( value: T) -> NSData {
return withUnsafePointer(to:value) { p in
NSData(bytes: p, length: MemoryLayout.size(ofValue:value))
}
}
func decode<T>(data: NSData) -> T {
let pointer = UnsafeMutablePointer<T>.allocate(capacity: MemoryLayout.size(ofValue:T.self))
data.getBytes(pointer)
return pointer.move()
}
The struct which I need to convert looks like this:
struct start_ctrl_conn {
var header:header;
var version:UInt16;
var reserved:UInt8;
var framing_cap:UInt32;
var bearer_cap:UInt32;
var max_channels:UInt16;
var firmware_rev:UInt16; */
var hostname = Array.init(repeating: UInt8(0), count: 64);
var vendor = Array.init(repeating: UInt8(0), count: 64);
};
struct header {
var length:UInt16
var pptp_type:UInt16
var magic:UInt32
var ctrl_type:UInt16
var reserved0:UInt16
}
This doesn't seem to work.
The memory layout size give 48 bytes no matter what, although there at least 156 bytes.
Is there a simple way to encode and decode to struct to byets an visa versa?,
what am I doing wrong?
Cheers