From d28a7573de2fc6e336efcddd5961a53936c614bb Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Fri, 19 May 2017 20:34:12 +0200 Subject: [PATCH] Make swift-nio compile on linux as well --- Sources/NIO/Channel.swift | 4 ++-- Sources/NIO/ChannelOption.swift | 15 ++++++++++----- Sources/Sockets/Selector.swift | 2 +- Sources/Sockets/ServerSocket.swift | 8 ++++++-- 4 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Sources/NIO/Channel.swift b/Sources/NIO/Channel.swift index c2269e435..b3a1aa427 100644 --- a/Sources/NIO/Channel.swift +++ b/Sources/NIO/Channel.swift @@ -116,8 +116,8 @@ public class Channel : ChannelOutboundInvoker { public func setOption(option: T, value: T.OptionType) throws { if option is SocketOption { - let (level, name) = option.value as! (Int32, Int32) - try socket.setOption(level: level, name: name, value: value) + let (level, name) = option.value as! (Int, Int32) + try socket.setOption(level: Int32(level), name: name, value: value) } else if option is AllocatorOption { allocator = value as! ByteBufferAllocator } else if option is RecvAllocatorOption { diff --git a/Sources/NIO/ChannelOption.swift b/Sources/NIO/ChannelOption.swift index 584e15293..68ef921a8 100644 --- a/Sources/NIO/ChannelOption.swift +++ b/Sources/NIO/ChannelOption.swift @@ -35,16 +35,17 @@ extension ChannelOption where AssociatedValueType == () { } public enum SocketOption: ChannelOption { - public typealias AssociatedValueType = (Int32, Int32) + public typealias AssociatedValueType = (Int, Int32) + public typealias OptionType = (Int) case const(AssociatedValueType) - public init(level: Int32, name: Int32) { + public init(level: Int, name: Int32) { self = .const((level, name)) } - - public var value: (Int32, Int32) { + + public var value: (Int, Int32) { switch self { case .const(let level, let name): return (level, name) @@ -81,7 +82,11 @@ public enum MaxMessagesPerReadOption: ChannelOption { } public struct ChannelOptions { - public static let Socket = { (level: Int32, name: Int32) -> SocketOption in .const((level, name)) } +#if os(Linux) + public static let Socket = { (level: Int, name: Int32) -> SocketOption in .const((level, name)) } +#else + public static let Socket = { (level: Int32, name: Int32) -> SocketOption in .const((Int(level), name)) } +#endif public static let Allocator = AllocatorOption.const(()) public static let RecvAllocator = RecvAllocatorOption.const(()) public static let AutoRead = AutoReadOption.const(()) diff --git a/Sources/Sockets/Selector.swift b/Sources/Sockets/Selector.swift index a57305f1c..738f0390d 100644 --- a/Sources/Sockets/Selector.swift +++ b/Sources/Sockets/Selector.swift @@ -200,7 +200,7 @@ public class Selector { public func close() throws { let _ = try wrapSyscall({ $0 >= 0 }, function: "close") { #if os(Linux) - return Glibc.close(self.fd) + return Int(Glibc.close(self.fd)) #else return Int(Darwin.close(self.fd)) #endif diff --git a/Sources/Sockets/ServerSocket.swift b/Sources/Sockets/ServerSocket.swift index 9d078a7b8..a1a39d094 100644 --- a/Sources/Sockets/ServerSocket.swift +++ b/Sources/Sockets/ServerSocket.swift @@ -72,8 +72,12 @@ public class ServerSocket: BaseSocket { #if os(Linux) /* no SO_NOSIGPIPE on Linux :( */ - let old_sighandler = signal(SIGPIPE, SIG_IGN); - if old_sighandler == SIG_ERR { + let old_sighandler: sighandler_t = Glibc.signal(SIGPIPE, SIG_IGN) + + let old_sighandler_ptr = unsafeBitCast(old_sighandler, to: UnsafeRawPointer.self) + let sig_err_ptr = unsafeBitCast(SIG_ERR, to: UnsafeRawPointer.self) + + if old_sighandler_ptr == sig_err_ptr { return nil } #else