I’m receiving Data packets through UDP from a game, and need to convert these packets into a struct.
This is an example of what the struct is made up of:
// -- 1289 bytes --
struct Packet {
let property1: UInt8
let property2: Float
[...]
let property3: [SecondPacket] // contains 20 elements - 20* MemoryLayout<SecondPacket>.size = 900 bytes
[...]
}
// -- 45 bytes --
struct SecondPacket {
let property1: UInt8
let property2: Float
[...]
}
So each packet I receive is 1289 bytes, and also the size of the struct Packet is 1289 bytes, which makes sense.
Now in order to convert the data into Packet, I use this function:
extension Data {
func to<T>(type: T.Type) -> T {
return self.withUnsafeBytes { $0.pointee }
}
}
In order to get the array inside Packet I need to do something different. So I tried dividing the packets using
Data.subdata(in:)
this way I get only the data that should be contained in that array.
In order to fill the array I use this other function:
func convert<T>(count: Int, data: Data) -> [T] {
return data.withUnsafeBytes { (pointer: UnsafePointer<T>) -> [T] in
let buffer = UnsafeBufferPointer<T>(start: pointer, count: count)
return [T](buffer)
}
}
In theory this approach should work, but when I run the app and receive the first packet Xcode crashes at this line with this error:
func convert<T>(count: Int, data: Data) -> [T] {
return data.withUnsafeBytes { (pointer: UnsafePointer<T>) -> [T] in
let buffer = UnsafeBufferPointer<T>(start: pointer, count: count)
return [T](buffer) // Thread #: Fatal error: UnsafeMutablePointer.initialize overlapping range
}
}
Any clue why that happens?
Also do you believe this is the right approach of doing it?
Thanks!