Skip to content

Navigation Menu

Sign in
Sign up

Fix silent failure receiving files: read large frames in bounded chunks #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
aryzae wants to merge 2 commits into grishka:master
base: master
Choose a base branch
Loading
from aryzae:fix/chunked-frame-reads
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions NearbyShare/NearbyConnection.swift
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import BigInt

class NearbyConnection{
internal static let SANE_FRAME_LENGTH=5*1024*1024
private static let MAX_RECEIVE_LENGTH=16*1024
private static let dispatchQueue=DispatchQueue(label: "me.grishka.NearDrop.queue", qos: .utility) // FIFO (non-concurrent) queue to avoid those exciting concurrency bugs

internal let connection:NWConnection
Expand Down Expand Up @@ -125,20 +126,31 @@ class NearbyConnection{
}

private func receiveFrameAsync(length:UInt32){
connection.receive(minimumIncompleteLength: Int(length), maximumLength: Int(length)) { content, contentContext, isComplete, error in
receiveFrameChunkAsync(remaining: Int(length), accumulated: NSMutableData(capacity: Int(length)) ?? NSMutableData())
}

// File payloads arrive in 512 KB frames, and asking for one of those in a single receive
// hands back damaged bytes, so reassemble the frame from bounded reads instead.
private func receiveFrameChunkAsync(remaining:Int, accumulated:NSMutableData){
if remaining<=0{
processReceivedFrame(frameData: accumulated as Data)
receiveFrameAsync()
return
}
connection.receive(minimumIncompleteLength: 1, maximumLength: min(remaining, NearbyConnection.MAX_RECEIVE_LENGTH)) { content, contentContext, isComplete, error in
if self.connectionClosed{
return
}
if isComplete{
self.handleConnectionClosure()
return
}
guard let content=content else {
guard let content=content, !content.isEmpty else {
self.protocolError()
return
}
self.processReceivedFrame(frameData: content)
self.receiveFrameAsync()
accumulated.append(content)
self.receiveFrameChunkAsync(remaining: remaining-content.count, accumulated: accumulated)
}
}

Expand Down

AltStyle によって変換されたページ (->オリジナル) /