Skip to content

Commit f1971de

Browse files
committed
fix(example): update file descriptor closing logic and improve sendAll error handling
1 parent 01b643e commit f1971de

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ jobs:
220220
~/.cargo/registry
221221
~/.cargo/git
222222
target
223-
key: ${{ runner.os }}-cargo-swift-${{ hashFiles('**/Cargo.lock') }}
223+
key: ${{ runner.os }}-cargo-swift-${{ hashFiles('**/Cargo.toml','**/Cargo.lock') }}
224224
restore-keys: |
225225
${{ runner.os }}-cargo-swift-
226226

swift/Examples/QuicClientExample/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,5 +212,5 @@ while nowMs(since: engineStartMs) < runDurationMs {
212212
print("Run time elapsed, disconnecting...")
213213
engine.disconnect()
214214
sendOutgoing(engine, fd: fd, to: &brokerAddr)
215-
Darwin.close(fd)
215+
close(fd)
216216
print("Done.")

swift/Examples/TcpClientExample/main.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,18 @@ private func sendAll(_ fd: Int32, data: Data) {
6565
var sent = 0
6666
while sent < data.count {
6767
let n = send(fd, ptr.baseAddress!.advanced(by: sent), data.count - sent, 0)
68-
if n <= 0 { return }
69-
sent += n
68+
if n > 0 {
69+
sent += n
70+
} else if n < 0 {
71+
if errno == EINTR {
72+
continue // signal interrupted — retry immediately
73+
} else if errno == EAGAIN || errno == EWOULDBLOCK {
74+
_ = waitWritable(fd, timeoutMs: 1000) // wait for buffer space
75+
continue
76+
} else {
77+
return // unrecoverable error
78+
}
79+
}
7080
}
7181
}
7282
}
@@ -196,5 +206,5 @@ while nowMs(since: engineStartMs) < runDurationMs {
196206
print("Run time elapsed, disconnecting...")
197207
engine.disconnect()
198208
sendAll(fd, data: engine.takeOutgoing())
199-
Darwin.close(fd)
209+
close(fd)
200210
print("Done.")

0 commit comments

Comments
 (0)