Skip to content

Commit c94ca28

Browse files
author
mustii
committed
Fixes zlib function + write function
1 parent ed13479 commit c94ca28

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

Sources/GRPC/Compression/Zlib.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ enum Zlib {
5757
// by deflateBound() if flush options other than Z_FINISH or Z_NO_FLUSH are used.
5858
let upperBound = CGRPCZlib_deflateBound(&self.stream.zstream, UInt(input.readableBytes))
5959

60-
// Note: readWithUnsafeMutableReadableBytes leads to an assertion whenever used
61-
// while the writer is bigger than the reader
62-
return try input.withUnsafeMutableReadableBytes { inputPointer in
60+
return try input.readWithUnsafeMutableReadableBytes { inputPointer -> (Int, Int) in
6361

6462
self.stream.nextInputBuffer = CGRPCZlib_castVoidToBytefPointer(inputPointer.baseAddress!)
6563
self.stream.availableInputBytes = inputPointer.count
@@ -69,12 +67,15 @@ enum Zlib {
6967
self.stream.availableInputBytes = 0
7068
}
7169

72-
return try output.writeWithUnsafeMutableBytes(minimumWritableBytes: Int(upperBound)) { outputPointer in
70+
let writtenBytes = try output.writeWithUnsafeMutableBytes(minimumWritableBytes: Int(upperBound)) { outputPointer in
7371
try self.stream.deflate(
7472
outputBuffer: CGRPCZlib_castVoidToBytefPointer(outputPointer.baseAddress!),
7573
outputBufferSize: outputPointer.count
7674
)
7775
}
76+
77+
let bytesRead = inputPointer.count - self.stream.availableInputBytes
78+
return (bytesRead, writtenBytes)
7879
}
7980
}
8081

Sources/GRPC/LengthPrefixedMessageWriter.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ internal struct LengthPrefixedMessageWriter {
5050
/// - Precondition: `compression.supported` is `true`.
5151
/// - Note: See `LengthPrefixedMessageReader` for more details on the format.
5252
func write(_ payload: GRPCPayload, into buffer: inout ByteBuffer, disableCompression: Bool = false) throws {
53-
buffer.reserveCapacity(MemoryLayout.size(ofValue: payload) + LengthPrefixedMessageWriter.metadataLength)
54-
53+
buffer.reserveCapacity(buffer.writerIndex + LengthPrefixedMessageWriter.metadataLength)
5554
if !disableCompression, let compressor = self.compressor {
5655
// Set the compression byte.
5756
buffer.writeInteger(UInt8(1))

0 commit comments

Comments
 (0)