swift-nio-extras icon indicating copy to clipboard operation
swift-nio-extras copied to clipboard

Import `ucrt` on Windows if available

Open Joannis opened this issue 5 months ago • 3 comments

This should enable NIOExtras to work on Windows, except for modules that depend on NIOSSL.

Joannis avatar Nov 03 '25 15:11 Joannis

Looks like there are format tweaks that need to happen.

Lukasa avatar Nov 05 '25 23:11 Lukasa

@Lukasa I tried swift format -r -i ., but the invocation results in many more changes than my PR touches.

Joannis avatar Nov 06 '25 14:11 Joannis

It would probably be enough to apply this diff:

diff --git a/Package.swift b/Package.swift
index 0707fe6..7d6d0dd 100644
--- a/Package.swift
+++ b/Package.swift
@@ -111,7 +111,7 @@ var targets: [PackageDescription.Target] = [
         name: "CNIOExtrasZlib",
         dependencies: [],
         cSettings: [
-            .define("_CRT_SECURE_NO_WARNINGS", .when(platforms: [.windows])),
+            .define("_CRT_SECURE_NO_WARNINGS", .when(platforms: [.windows]))
         ],
         linkerSettings: [
             .linkedLibrary("z", .when(platforms: [.linux, .macOS, .iOS, .tvOS, .watchOS, .visionOS])),
diff --git a/Sources/HTTPServerWithQuiescingDemo/main.swift b/Sources/HTTPServerWithQuiescingDemo/main.swift
index 41c1458..a84ba37 100644
--- a/Sources/HTTPServerWithQuiescingDemo/main.swift
+++ b/Sources/HTTPServerWithQuiescingDemo/main.swift
@@ -136,4 +136,4 @@ private func runServer() throws {
 try runServer()
 #else
 print("This demo is not supported on Windows.")
-#endif
\ No newline at end of file
+#endif
diff --git a/Sources/NIOExtras/WritePCAPHandler.swift b/Sources/NIOExtras/WritePCAPHandler.swift
index 2e02cac..23c9978 100644
--- a/Sources/NIOExtras/WritePCAPHandler.swift
+++ b/Sources/NIOExtras/WritePCAPHandler.swift
@@ -26,21 +26,21 @@ import Android
 import ucrt
 import WinSDK
 
-fileprivate func gettimeofday(_ tp: inout timeval, _ tzp: Never?) {
+private func gettimeofday(_ tp: inout timeval, _ tzp: Never?) {
     var file_time = FILETIME()
     var system_time = SYSTEMTIME()
     var time: UInt64 = 0
 
     // This magic number is the number of 100 nanosecond intervals since January 1, 1601 (UTC)
-    // until 00:00:00 January 1, 1970 
-    let epoch: UInt64 = 116444736000000000
+    // until 00:00:00 January 1, 1970
+    let epoch: UInt64 = 116_444_736_000_000_000
 
-    GetSystemTime(&system_time);
-    SystemTimeToFileTime(&system_time, &file_time);
-    time =  UInt64(file_time.dwLowDateTime)
+    GetSystemTime(&system_time)
+    SystemTimeToFileTime(&system_time, &file_time)
+    time = UInt64(file_time.dwLowDateTime)
     time += UInt64(file_time.dwHighDateTime) << 32
 
-    tp.tv_sec = Int32((time - epoch) / 10000000)
+    tp.tv_sec = Int32((time - epoch) / 10_000_000)
     tp.tv_usec = Int32(system_time.wMilliseconds * 1000)
 }
 #else
@@ -752,7 +752,14 @@ extension NIOWritePCAPHandler {
         ) throws -> SynchronizedFileSink {
             #if os(Windows)
             let fd = try path.withCString(encodedAs: UTF16.self) { pathPtr -> CInt in
-                let fd = _wsopen_s(nil, pathPtr, _O_WRONLY | (fileWritingMode == FileWritingMode.createNewPCAPFile ? (_O_TRUNC | _O_CREAT) : _O_APPEND), _SH_DENYNO, _S_IREAD | _S_IWRITE)
+                let fd = _wsopen_s(
+                    nil,
+                    pathPtr,
+                    _O_WRONLY
+                        | (fileWritingMode == FileWritingMode.createNewPCAPFile ? (_O_TRUNC | _O_CREAT) : _O_APPEND),
+                    _SH_DENYNO,
+                    _S_IREAD | _S_IWRITE
+                )
                 guard fd >= 0 else {
                     throw SynchronizedFileSink.Error(errorCode: Error.ErrorCode.cannotOpenFileError.rawValue)
                 }

Lukasa avatar Nov 07 '25 14:11 Lukasa