Skip to content

Commit 513503c

Browse files
xiazhverawaahm7
andauthored
Request-Response Client Operations (#332)
Co-authored-by: Waqar Ahmed Khan <[email protected]>
1 parent 9bdecb5 commit 513503c

File tree

6 files changed

+781
-160
lines changed

6 files changed

+781
-160
lines changed

Source/AwsCommonRuntimeKit/crt/CStruct.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ extension Array where Element: CStruct {
4747
// `aws_array_list_push_back` will do a memory copy of $0 into array_list, but it would
4848
// not do a deep copy there.
4949
guard aws_array_list_push_back(array_list, $0) == AWS_OP_SUCCESS else {
50-
fatalError("Unable to add user property")
50+
fatalError("Unable to add array element")
5151
}
5252
}
5353
}

Source/AwsCommonRuntimeKit/crt/Utilities.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,4 +481,19 @@ extension Array where Element == String {
481481
return body(cursorsPtr.baseAddress!, len)
482482
}
483483
}
484+
485+
func withMutableByteCursorArray<R>(_ body: (UnsafeMutablePointer<aws_byte_cursor>, Int) -> R) -> R
486+
{
487+
let len = self.count
488+
let cStrings = self.map { strdup($0) }
489+
var cursors = cStrings.map { aws_byte_cursor_from_c_str($0) }
490+
491+
defer {
492+
cStrings.forEach { free($0) }
493+
}
494+
495+
return cursors.withUnsafeMutableBufferPointer { cursorsPtr in
496+
return body(cursorsPtr.baseAddress!, len)
497+
}
498+
}
484499
}

Source/AwsCommonRuntimeKit/mqtt/Mqtt5Packets.swift

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ func convertOptionalUserProperties(
6666
return userProperties
6767
}
6868

69+
extension UserProperty: Equatable {
70+
static public func == (lhs: UserProperty, rhs: UserProperty) -> Bool {
71+
return lhs.name == rhs.name && lhs.value == rhs.value
72+
}
73+
}
74+
6975
// We can't mutate this class after initialization. Swift can not verify the sendability due to the class is non-final,
7076
// so mark it unchecked Sendable
7177
/// Data model of an `MQTT5 PUBLISH <https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901100>`_ packet
@@ -494,7 +500,6 @@ public class UnsubscribePacket: CStruct, @unchecked Sendable {
494500
) {
495501
self.topicFilters = topicFilters
496502
self.userProperties = userProperties
497-
rawTopicFilters = convertTopicFilters(self.topicFilters)
498503
}
499504

500505
// Allow an UnsubscribePacket to be created directly using a single topic filter
@@ -508,40 +513,15 @@ public class UnsubscribePacket: CStruct, @unchecked Sendable {
508513
typealias RawType = aws_mqtt5_packet_unsubscribe_view
509514
func withCStruct<Result>(_ body: (RawType) -> Result) -> Result {
510515
var raw_unsubscribe_view = aws_mqtt5_packet_unsubscribe_view()
511-
raw_unsubscribe_view.topic_filters = UnsafePointer(rawTopicFilters)
512-
raw_unsubscribe_view.topic_filter_count = topicFilters.count
513-
return userProperties.withAWSArrayList { userPropertyPointer in
514-
raw_unsubscribe_view.user_property_count = userProperties.count
515-
raw_unsubscribe_view.user_properties =
516-
UnsafePointer<aws_mqtt5_user_property>(userPropertyPointer)
517-
return body(raw_unsubscribe_view)
518-
}
519-
}
520-
521-
func convertTopicFilters(_ topicFilters: [String]) -> UnsafeMutablePointer<aws_byte_cursor>? {
522-
let cArray = UnsafeMutablePointer<aws_byte_cursor>.allocate(capacity: topicFilters.count)
523-
524-
for (index, string) in topicFilters.enumerated() {
525-
let data = Data(string.utf8)
526-
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: data.count)
527-
data.copyBytes(to: buffer, count: data.count)
528-
529-
cArray[index] = aws_byte_cursor(len: data.count, ptr: buffer)
530-
}
531-
532-
return cArray
533-
}
534-
535-
/// storage of topic filter Strings converted into native c aws_byte_cursor pointer
536-
private var rawTopicFilters: UnsafeMutablePointer<aws_byte_cursor>?
537-
538-
deinit {
539-
/// Clean up memory of converted topic filter Strings
540-
if let filters = rawTopicFilters {
541-
for i in 0..<topicFilters.count {
542-
filters[i].ptr.deallocate()
516+
return self.topicFilters.withByteCursorArray { byteCusorArray, len in
517+
raw_unsubscribe_view.topic_filters = byteCusorArray
518+
raw_unsubscribe_view.topic_filter_count = len
519+
return userProperties.withAWSArrayList { userPropertyPointer in
520+
raw_unsubscribe_view.user_property_count = userProperties.count
521+
raw_unsubscribe_view.user_properties =
522+
UnsafePointer<aws_mqtt5_user_property>(userPropertyPointer)
523+
return body(raw_unsubscribe_view)
543524
}
544-
filters.deallocate()
545525
}
546526
}
547527
}

0 commit comments

Comments
 (0)