Skip to content

Conversation

@compnerd
Copy link
Member

The DispatchIO constructor maps to dispatch_io_create. The value on
Windows for the fd (which is actually a HANDLE) is being truncated
since dispatch_fd_t is actually Int not Int32 on Windows. Use the
dispatch_fd_t to ensure that the right size is always passed along.

The `DispatchIO` constructor maps to `dispatch_io_create`.  The value on
Windows for the `fd` (which is actually a `HANDLE`) is being truncated
since `dispatch_fd_t` is actually `Int` not `Int32` on Windows.  Use the
`dispatch_fd_t` to ensure that the right size is always passed along.
@compnerd
Copy link
Member Author

@swift-ci please test

@compnerd
Copy link
Member Author

With this change, it is now possible to write an echo server for Windows:

import Dispatch
import ucrt
import WinSDK

let rx: DispatchQueue = DispatchQueue(label: "rx", qos: .userInitiated)

let hIn: HANDLE = GetStdHandle(STD_INPUT_HANDLE)
if hIn == INVALID_HANDLE_VALUE { exit(EXIT_FAILURE) }

let io: DispatchIO = DispatchIO(type: .stream, fileDescriptor: Int(bitPattern: hIn), queue: rx) { error in
  guard error == 0 else { exit(EXIT_FAILURE) }
}
io.setLimit(lowWater: 1)
io.setLimit(highWater: Int.max)

while true {
  io.read(offset: 0, length: Int.max, queue: rx) { done, data, errorCode in
    guard errorCode == 0 else {
      print("IO Error \(errorCode)");
      exit(EXIT_FAILURE)
    }

    guard let data = data else { return }
    data.withUnsafeBytes { (ptr: UnsafePointer<CChar>) -> () in
      print(">> \(String(cString: ptr))")
    }
  }
}

@drexin
Copy link
Contributor

drexin commented Apr 13, 2020

/cc @MadCoder Would you mind taking a look if you have a moment?

@gwynne
Copy link

gwynne commented Jun 4, 2020

Looks nice and straightforward to me.

@snitebobbie
Copy link

Pull request

@MadCoder MadCoder merged commit feb4421 into swiftlang:master Jun 8, 2020
rokhinip pushed a commit that referenced this pull request Nov 5, 2021
IO: match the signature for `DispatchIO` with `dispatch_io_create`
Signed-off-by: Rokhini Prabhu <rokhinip@apple.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants