Commit 4fdfc8b8 authored by Thomas Mellenthin's avatar Thomas Mellenthin

fix crc32 calculation

parent ee9f76c8
......@@ -51,6 +51,7 @@ class DataTransferManager: NSObject {
var lastPackageSentAt: TimeInterval?
var dataToSend: Data?
var currentChunkToSend: Data?
var currentChunkToSendCrc32: UInt32 = 0
var retryCount: Int = 0
init(_ delegateToUse: DataTransferManagerDelegate) {
......@@ -78,7 +79,7 @@ class DataTransferManager: NSObject {
case .CHUNK_ACK?:
self.transferState = .CHUNK_SENT
// calculate CRC and send new chunk
let range: Range<Data.Index> = 0..<1
let range: Range<Data.Index> = 1..<5
let crc = package.subdata(in: range)
updateProgress()
if checkChunkWithResponse(crc) {
......@@ -156,6 +157,8 @@ class DataTransferManager: NSObject {
var int = UInt32(bigEndian: UInt32(offset))
payloadToSend?.append(Data(bytes: &int, count: MemoryLayout.size(ofValue: UInt32())))
payloadToSend?.append(payload!)
// pre-calculate the expected CRC sum for that packet (must include header and offset)
currentChunkToSendCrc32 = CRC32.checksum(bytes: payloadToSend!.bytes)
lastPackageSentAt = NSDate.init().timeIntervalSinceNow
case .CHUNK_ACK:
......@@ -181,17 +184,8 @@ class DataTransferManager: NSObject {
}
func checkChunkWithResponse(_ response: Data) -> Bool {
return true
//FIXME: This CRC32 implementation doesn't work. Disabled for now
let responseInUInt32 = response.withUnsafeBytes { $0.load(as: UInt8.self) }
let chunkInUInt8 = [UInt8](self.currentChunkToSend!)
let checksum = CRC32.checksum(bytes: chunkInUInt8)
if responseInUInt32 == checksum {
return true
}
return false
// compares received crc32 with the previously calculated checksum
return response.withUnsafeBytes { $0.load(as: UInt32.self) } == currentChunkToSendCrc32
}
func updateProgress() {
......
......@@ -21,3 +21,9 @@ extension isDataInt {
extension UInt16: isDataInt { }
extension UInt64: isDataInt { }
extension Data {
var bytes: [UInt8] {
return [UInt8](self)
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment