diff --git a/Sources/NIO/PendingDatagramWritesManager.swift b/Sources/NIO/PendingDatagramWritesManager.swift index 382f59fa0..c7d45f60b 100644 --- a/Sources/NIO/PendingDatagramWritesManager.swift +++ b/Sources/NIO/PendingDatagramWritesManager.swift @@ -17,6 +17,7 @@ private struct PendingDatagramWrite { var data: ByteBuffer var promise: Optional> let address: SocketAddress + var metadata: AddressedEnvelope.Metadata? /// A helper function that copies the underlying sockaddr structure into temporary storage, /// and then returns the length. @@ -407,7 +408,10 @@ final class PendingDatagramWritesManager: PendingWritesManager { /// - result: If the `Channel` is still writable after adding the write of `data`. func add(envelope: AddressedEnvelope, promise: EventLoopPromise?) -> Bool { assert(self.isOpen) - self.state.append(.init(data: envelope.data, promise: promise, address: envelope.remoteAddress)) + self.state.append(.init(data: envelope.data, + promise: promise, + address: envelope.remoteAddress, + metadata: envelope.metadata)) if self.state.bytes > waterMark.high && channelWritabilityFlag.compareAndExchange(expected: true, desired: false) { // Returns false to signal the Channel became non-writable and we need to notify the user @@ -428,12 +432,12 @@ final class PendingDatagramWritesManager: PendingWritesManager { /// - scalarWriteOperation: An operation that writes a single, contiguous array of bytes (usually `sendto`). /// - vectorWriteOperation: An operation that writes multiple contiguous arrays of bytes (usually `sendmmsg`). /// - returns: The `WriteResult` and whether the `Channel` is now writable. - func triggerAppropriateWriteOperations(scalarWriteOperation: (UnsafeRawBufferPointer, UnsafePointer, socklen_t) throws -> IOResult, + func triggerAppropriateWriteOperations(scalarWriteOperation: (UnsafeRawBufferPointer, UnsafePointer, socklen_t, AddressedEnvelope.Metadata?) throws -> IOResult, vectorWriteOperation: (UnsafeMutableBufferPointer) throws -> IOResult) throws -> OverallWriteResult { return try self.triggerWriteOperations { writeMechanism in switch writeMechanism { case .scalarBufferWrite: - return try triggerScalarBufferWrite(scalarWriteOperation: { try scalarWriteOperation($0, $1, $2) }) + return try triggerScalarBufferWrite(scalarWriteOperation: { try scalarWriteOperation($0, $1, $2, $3) }) case .vectorBufferWrite: do { return try triggerVectorBufferWrite(vectorWriteOperation: { try vectorWriteOperation($0) }) @@ -444,7 +448,7 @@ final class PendingDatagramWritesManager: PendingWritesManager { throw error } - return try triggerScalarBufferWrite(scalarWriteOperation: { try scalarWriteOperation($0, $1, $2) }) + return try triggerScalarBufferWrite(scalarWriteOperation: { try scalarWriteOperation($0, $1, $2, $3) }) } case .scalarFileWrite: preconditionFailure("PendingDatagramWritesManager was handed a file write") @@ -498,13 +502,15 @@ final class PendingDatagramWritesManager: PendingWritesManager { /// /// - parameters: /// - scalarWriteOperation: An operation that writes a single, contiguous array of bytes (usually `sendto`). - private func triggerScalarBufferWrite(scalarWriteOperation: (UnsafeRawBufferPointer, UnsafePointer, socklen_t) throws -> IOResult) rethrows -> OneWriteOperationResult { + private func triggerScalarBufferWrite(scalarWriteOperation: (UnsafeRawBufferPointer, UnsafePointer, socklen_t, AddressedEnvelope.Metadata?) throws -> IOResult) rethrows -> OneWriteOperationResult { assert(self.state.isFlushPending && self.isOpen && !self.state.isEmpty, "illegal state for scalar datagram write operation: flushPending: \(self.state.isFlushPending), isOpen: \(self.isOpen), empty: \(self.state.isEmpty)") let pending = self.state.nextWrite! do { let writeResult = try pending.address.withSockAddr { (addrPtr, addrSize) in - try pending.data.withUnsafeReadableBytes { try scalarWriteOperation($0, addrPtr, socklen_t(addrSize)) } + try pending.data.withUnsafeReadableBytes { + try scalarWriteOperation($0, addrPtr, socklen_t(addrSize), pending.metadata) + } } return self.didWrite(writeResult, messages: nil) } catch { diff --git a/Sources/NIO/SocketChannel.swift b/Sources/NIO/SocketChannel.swift index 13b3c6ca9..1dca7f10b 100644 --- a/Sources/NIO/SocketChannel.swift +++ b/Sources/NIO/SocketChannel.swift @@ -617,7 +617,7 @@ final class DatagramChannel: BaseSocketChannel { override func writeToSocket() throws -> OverallWriteResult { let result = try self.pendingWrites.triggerAppropriateWriteOperations( - scalarWriteOperation: { (ptr, destinationPtr, destinationSize) in + scalarWriteOperation: { (ptr, destinationPtr, destinationSize, metadata) in guard ptr.count > 0 else { // No need to call write if the buffer is empty. return .processed(0) diff --git a/Tests/NIOTests/PendingDatagramWritesManagerTests.swift b/Tests/NIOTests/PendingDatagramWritesManagerTests.swift index 4d6ea7164..ad65ba648 100644 --- a/Tests/NIOTests/PendingDatagramWritesManagerTests.swift +++ b/Tests/NIOTests/PendingDatagramWritesManagerTests.swift @@ -111,7 +111,7 @@ class PendingDatagramWritesManagerTests: XCTestCase { var result: OverallWriteResult? = nil do { - let r = try pwm.triggerAppropriateWriteOperations(scalarWriteOperation: { (buf, addr, len) in + let r = try pwm.triggerAppropriateWriteOperations(scalarWriteOperation: { (buf, addr, len, metadata) in defer { singleState += 1 everythingState += 1