Skip to content

Commit b0aa0d7

Browse files
Merge pull request #174 from kishikawakatsumi/parameterpacks
Refactoring the processing of compound requests using Variadic Generics and Parameter Packs
2 parents 3061fe4 + 25ab3ac commit b0aa0d7

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ on:
66
branches: [main]
77
workflow_dispatch:
88

9+
env:
10+
DEVELOPER_DIR: /Applications/Xcode_16.1.app/Contents/Developer
11+
912
jobs:
1013
test:
1114
runs-on: macos-14

Examples/FileBrowser/FileBrowser.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/SMBClient/Session.swift

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,60 @@ public class Session {
701701
return response
702702
}
703703

704+
#if compiler(>=5.9)
705+
private func send<each Request: Message.Request>(_ messages: repeat each Request) async throws -> (repeat (each Request).Response) {
706+
var count = 0
707+
for _ in repeat each messages {
708+
count += 1
709+
}
710+
711+
var packet = Data()
712+
var index = 0
713+
for message in repeat each messages {
714+
let data = message.encoded()
715+
let alignment = Data(count: 8 - data.count % 8)
716+
if index < count - 1 {
717+
let body = data + alignment
718+
var header = Header(data: body[..<64])
719+
let payload = data[64...]
720+
721+
header.nextCommand = UInt32(body.count)
722+
723+
packet += sign(header.encoded() + payload + alignment)
724+
} else {
725+
packet += sign(data + alignment)
726+
}
727+
728+
index += 1
729+
}
730+
731+
let responseData = try await connection.send(packet)
732+
let reader = ByteReader(responseData)
733+
734+
var responses = [Data]()
735+
736+
var header: Header
737+
var offset = 0
738+
739+
repeat {
740+
responses.append(Data(responseData[offset...]))
741+
742+
header = reader.read()
743+
744+
offset += Int(header.nextCommand)
745+
reader.seek(to: offset)
746+
} while header.nextCommand != 0
747+
748+
var iterator = 0
749+
func respond<R: Message.Request>(requestType: R.Type) -> R.Response {
750+
let response = R.Response(data: responses[iterator])
751+
iterator += 1
752+
return response
753+
}
754+
755+
return (repeat respond(requestType: (each Request).self))
756+
}
757+
#else
704758
private func send<R1: Message.Request, R2: Message.Request>(_ m1: R1, _ m2: R2) async throws -> (R1.Response, R2.Response) {
705759
let data = try await send(m1.encoded(), m2.encoded())
706760
let r1 = R1.Response(data: data)
@@ -734,6 +788,7 @@ public class Session {
734788
}
735789
)
736790
}
791+
#endif
737792

738793
private func sign(_ packet: Data) -> Data {
739794
if let signingKey, signingRequired, !isAnonymous {

0 commit comments

Comments
 (0)