我有一个struct对象。还有一种方法,其输入是有效载荷。现在我正在创建一个名为packet
的mutableData,它的可变字节指的是ICMPHeader结构。
struct ICMPHeader {
var type:UInt8
var code:UInt8
var checksum:UInt16
var identifier:UInt16
var sequenceNumber:UInt16
};
func createPacket(payload:NSData) -> NSData(){
var packet:NSMutableData?
var icmpPtr:ICMPHeader = ICMPHeader(type: 0, code: 0, checksum: 0, identifier: 0, sequenceNumber: 0)
packet = NSMutableData(length: Int(MemoryLayout<ICMPHeader>.size + payload.length))
if packet != nil {
icmpPtr = packet!.mutableBytes.assumingMemoryBound(to: ICMPHeader.self).pointee
icmpPtr.type = type
icmpPtr.code = 0
icmpPtr.checksum = 0
icmpPtr.identifier = CFSwapInt16BigToHost(identifier)
icmpPtr.sequenceNumber = CFSwapInt16HostToBig(identifier)
memcpy(&icmpPtr + 1, payload.bytes, payload.length)
if (requiresChecksum) {
icmpPtr.checksum = in_cksum(packet!.bytes, bufferLen: packet!.length);
}
}
return packet
}
可变字节成功绑定到struct,值也在struct ICMPHeader
中更新。
问题是更改struct中的值不会改变可变数据packet
的值。
如果,我在创建struct之后尝试重新创建数据包,那么它就会崩溃。
package = NSMutableData(bytes: unsafeBitCast(icmpPtr, to: UnsafeMutableRawPointer.self), length: Int(MemoryLayout<ICMPHeader>.size + payload.length))
答案 0 :(得分:0)
我在XCode 8.1 Swift发行说明中找到了答案。
在struct object icmpPtr
中进行更改后,我将其更改回缓冲区指针。
var byteBuffer = [UInt8]()
withUnsafeBytes(of: &icmpPtr) {
(bytes: UnsafeRawBufferPointer) in byteBuffer += bytes
}
package.replaceBytes(in: NSMakeRange(0, byteBuffer.count), withBytes: byteBuffer)
因此,我没有创建新的Data对象,而是更换了字节,它就像魅力一样。
根据文件:
XCode 8.1发行说明: https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Introduction.html Swift部分:
一个新的withUnsafeBytes(of :)函数公开了一个值在内存中 表示为UnsafeRawBufferPointer。这个例子复制了一个 异构结构为一个齐次的字节数组:
struct Header { var x: Int var y: Float } var header = Header(x: 0, y: 0.0) var byteBuffer = [UInt8]() withUnsafeBytes(of: &header) { (bytes: UnsafeRawBufferPointer) in byteBuffer += bytes }
一个新的Array.withUnsafeBytes方法公开了一个数组的底层 缓冲区作为UnsafeRawBufferPointer。此示例复制一个数组 整数成一个字节数组:
let intArray = [1, 2, 3] var byteBuffer = [UInt8]() intArray.withUnsafeBytes { (bytes: UnsafeRawBufferPointer) in byteBuffer += bytes }
所以最后的实现是
struct ICMPHeader {
var type:UInt8
var code:UInt8
var checksum:UInt16
var identifier:UInt16
var sequenceNumber:UInt16
};
func createPacket(payload:NSData) -> NSData(){
var packet:NSMutableData?
var icmpPtr:ICMPHeader = ICMPHeader(type: 0, code: 0, checksum: 0, identifier: 0, sequenceNumber: 0)
packet = NSMutableData(length: Int(MemoryLayout<ICMPHeader>.size + payload.length))
if packet != nil {
icmpPtr = packet!.mutableBytes.assumingMemoryBound(to: ICMPHeader.self).pointee
icmpPtr.type = type
icmpPtr.code = 0
icmpPtr.checksum = 0
icmpPtr.identifier = CFSwapInt16BigToHost(identifier)
icmpPtr.sequenceNumber = CFSwapInt16HostToBig(identifier)
memcpy(&icmpPtr + 1, payload.bytes, payload.length)
if (requiresChecksum) {
icmpPtr.checksum = in_cksum(packet!.bytes, bufferLen: packet!.length);
}
var byteBuffer = [UInt8]()
withUnsafeBytes(of: &icmpPtr) {
(bytes: UnsafeRawBufferPointer) in byteBuffer += bytes
}
packet.replaceBytes(in: NSMakeRange(0, byteBuffer.count), withBytes: byteBuffer)
}
return packet
}