diff --git a/Sources/NIOPosix/BSDSocketAPIWindows.swift b/Sources/NIOPosix/BSDSocketAPIWindows.swift index 2d8af7d3c..b51aaca02 100644 --- a/Sources/NIOPosix/BSDSocketAPIWindows.swift +++ b/Sources/NIOPosix/BSDSocketAPIWindows.swift @@ -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) diff --git a/Sources/NIOPosix/Socket.swift b/Sources/NIOPosix/Socket.swift index 0b7d06a78..723f0aa2a 100644 --- a/Sources/NIOPosix/Socket.swift +++ b/Sources/NIOPosix/Socket.swift @@ -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 { - try withUnsafeHandle { - try Posix.read(descriptor: $0, pointer: pointer.baseAddress!, size: pointer.count) + try withUnsafeHandle { (handle) -> IOResult 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 } } diff --git a/Sources/NIOPosix/Windows.swift b/Sources/NIOPosix/Windows.swift index 8b43c1d08..b2733c544 100644 --- a/Sources/NIOPosix/Windows.swift +++ b/Sources/NIOPosix/Windows.swift @@ -44,6 +44,19 @@ extension NIOCore.Windows { return nil } } + + static func recv( + socket: SOCKET, + pointer: UnsafeMutableRawPointer, + size: Int32, + flags: Int32 + ) throws -> IOResult { + 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