[Windows] Use recv instead of read (#3357)

This commit is contained in:
Fabian Fett authored and GitHub committed 2025-08-26 10:30:46 +00:00
1 parent 0878179234
commit aff71b37f5
3 files changed
+20 -3

No files matched your search

+1 -1
View File
@@ -720,7 +720,7 @@ extension NIOBSDSocket {
extension IOVector {
// An initializer thats maps `ByteBuffer`` derived pointers to WSABUF. This allows us to use the
// same initializer, that we use for iovecs on Windows.
init(iov_base: UnsafeMutableRawPointer, iov_len: UInt32) {
init(iov_base: UnsafeMutableRawPointer!, iov_len: UInt32) {
self = WSABUF(
len: iov_len,
buf: iov_base.assumingMemoryBound(to: Int8.self)
+6 -2
View File
@@ -224,8 +224,12 @@ class Socket: BaseSocket, SocketProtocol {
/// - Returns: The `IOResult` which indicates how much data could be read and if the operation returned before all could be read (because the socket is in non-blocking mode).
/// - Throws: An `IOError` if the operation failed.
func read(pointer: UnsafeMutableRawBufferPointer) throws -> IOResult<Int> {
try withUnsafeHandle {
try Posix.read(descriptor: $0, pointer: pointer.baseAddress!, size: pointer.count)
try withUnsafeHandle { (handle) -> IOResult<Int> in
#if os(Windows)
try Windows.recv(socket: handle, pointer: pointer.baseAddress!, size: Int32(pointer.count), flags: 0)
#else
try Posix.read(descriptor: handle, pointer: pointer.baseAddress!, size: pointer.count)
#endif
}
}
+13
View File
@@ -44,6 +44,19 @@ extension NIOCore.Windows {
return nil
}
}
static func recv(
socket: SOCKET,
pointer: UnsafeMutableRawPointer,
size: Int32,
flags: Int32
) throws -> IOResult<Int> {
let result = WinSDK.recv(socket, pointer.assumingMemoryBound(to: CChar.self), size, flags)
if result == WinSDK.SOCKET_ERROR {
throw IOError(winsock: WSAGetLastError(), reason: "accept")
}
return .processed(Int(result))
}
}
#endif