diff --git a/Sources/Selectable.swift b/Sources/Selectable.swift index c0e085b53..b0f31a14f 100644 --- a/Sources/Selectable.swift +++ b/Sources/Selectable.swift @@ -15,8 +15,9 @@ import Foundation public protocol Selectable { - - func descriptor() -> Int32; - + var descriptor: Int32 { get } + + var open: Bool { get } + func close() throws; } diff --git a/Sources/Selector.swift b/Sources/Selector.swift index 42a88d88a..f001389db 100644 --- a/Sources/Selector.swift +++ b/Sources/Selector.swift @@ -55,12 +55,12 @@ public class Selector { case InterestedEvent.All: ev.events = EPOLLIN.rawValue | EPOLLOUT.rawValue } - ev.data.fd = selectable.descriptor() - let res = CEpoll.epoll_ctl(self.fd, EPOLL_CTL_ADD, selectable.descriptor(), &ev) + ev.data.fd = selectable.descriptor + let res = CEpoll.epoll_ctl(self.fd, EPOLL_CTL_ADD, selectable.descriptor, &ev) guard res == 0 else { throw IOError(errno: errno, reason: "epoll_ctl(...) failed") } - registrations[Int(selectable.descriptor())] = Registration(selectable: selectable, attachment: attachment) + registrations[Int(selectable.descriptor)] = Registration(selectable: selectable, attachment: attachment) #endif } @@ -77,8 +77,8 @@ public class Selector { ev.events = EPOLLIN.rawValue | EPOLLOUT.rawValue } - ev.data.fd = selectable.descriptor() - let res = CEpoll.epoll_ctl(self.fd, EPOLL_CTL_MOD, selectable.descriptor(), &ev) + ev.data.fd = selectable.descriptor + let res = CEpoll.epoll_ctl(self.fd, EPOLL_CTL_MOD, selectable.descriptor, &ev) guard res == 0 else { throw IOError(errno: errno, reason: "epoll_ctl(...) failed") } @@ -88,11 +88,11 @@ public class Selector { public func deregister(selectable: Selectable) throws { #if os(Linux) var ev = epoll_event() - let res = CEpoll.epoll_ctl(self.fd, EPOLL_CTL_DEL, selectable.descriptor(), &ev) + let res = CEpoll.epoll_ctl(self.fd, EPOLL_CTL_DEL, selectable.descriptor, &ev) guard res == 0 else { throw IOError(errno: errno, reason: "epoll_ctl(...) failed") } - registrations.removeValue(forKey: Int(selectable.descriptor())) + registrations.removeValue(forKey: Int(selectable.descriptor)) #endif } diff --git a/Sources/ServerSocket.swift b/Sources/ServerSocket.swift index f46149a39..f09329bda 100644 --- a/Sources/ServerSocket.swift +++ b/Sources/ServerSocket.swift @@ -23,8 +23,19 @@ import Foundation public class ServerSocket: Selectable { - private let fd: Int32; - public internal(set) var open: Bool; + public let descriptor: Int32; + public private(set) var open: Bool; + + public var localAddress: SocketAddress? { + get { + return nil + } + } + public var remoteAddress: SocketAddress? { + get { + return nil + } + } public class func bootstrap(host: String, port: Int32) throws -> ServerSocket { let socket = try ServerSocket(); @@ -35,26 +46,18 @@ public class ServerSocket: Selectable { init() throws { #if os(Linux) - self.fd = Glibc.socket(AF_INET, Int32(SOCK_STREAM.rawValue), 0) + self.descriptor = Glibc.socket(AF_INET, Int32(SOCK_STREAM.rawValue), 0) #else - self.fd = Darwin.socket(AF_INET, Int32(SOCK_STREAM), 0) + self.descriptor = Darwin.socket(AF_INET, Int32(SOCK_STREAM), 0) #endif - guard self.fd >= 0 else { + guard self.descriptor >= 0 else { throw IOError(errno: errno, reason: "socket(...) failed") } self.open = true } - public func localAddress() -> SocketAddress? { - return nil - } - - public func remoteAddress() -> SocketAddress? { - return nil; - } - public func setNonBlocking() throws { - let res = fcntl(self.fd, F_SETFL, O_NONBLOCK) + let res = fcntl(self.descriptor, F_SETFL, O_NONBLOCK) guard res >= 0 else { throw IOError(errno: errno, reason: "fcntl(...) failed") @@ -62,20 +65,17 @@ public class ServerSocket: Selectable { } - public func descriptor() -> Int32 { - return fd - } - public func close() throws { #if os(Linux) - let res = Glibc.close(self.fd) + let res = Glibc.close(self.descriptor) #else - let res = Darwin.close(self.fd) + let res = Darwin.close(self.descriptor) #endif guard res >= 0 else { throw IOError(errno: errno, reason: "close(...) failed") } + self.open = false } public func bind(address: SocketAddress) throws { @@ -83,11 +83,11 @@ public class ServerSocket: Selectable { #if os(Linux) let res = withUnsafePointer(to: &addr) { - Glibc.bind(self.fd, UnsafePointer($0), socklen_t(address.size)); + Glibc.bind(self.descriptor, UnsafePointer($0), socklen_t(address.size)); } #else let res = withUnsafePointer(to: &addr) { - Darwin.bind(self.fd, UnsafePointer($0), socklen_t(address.size)); + Darwin.bind(self.descriptor, UnsafePointer($0), socklen_t(address.size)); } #endif @@ -98,9 +98,9 @@ public class ServerSocket: Selectable { public func listen(backlog: Int32) throws { #if os(Linux) - let res = Glibc.listen(self.fd, backlog) + let res = Glibc.listen(self.descriptor, backlog) #else - let res = Darwin.listen(self.fd, backlog) + let res = Darwin.listen(self.descriptor, backlog) #endif guard res >= 0 else { throw IOError(errno: errno, reason: "listen(...) failed") @@ -113,7 +113,7 @@ public class ServerSocket: Selectable { #if os(Linux) let fd = withUnsafeMutablePointer(to: &acceptAddr) { - Glibc.accept(self.fd, UnsafeMutableRawPointer($0).assumingMemoryBound(to: sockaddr.self), &addrSize) + Glibc.accept(self.descriptor, UnsafeMutableRawPointer($0).assumingMemoryBound(to: sockaddr.self), &addrSize) } #else let fd = withUnsafeMutablePointer(to: &acceptAddr) { @@ -128,6 +128,6 @@ public class ServerSocket: Selectable { } return nil } - return Socket(fd: fd) + return Socket(descriptor: fd) } } diff --git a/Sources/Socket.swift b/Sources/Socket.swift index 2e3315f28..d25827edb 100644 --- a/Sources/Socket.swift +++ b/Sources/Socket.swift @@ -23,40 +23,39 @@ import Darwin public class Socket : Selectable { - private let fd: Int32; - public internal(set) var open: Bool; + public let descriptor: Int32 + public private(set) var open: Bool; + + public var localAddress: SocketAddress? { + get { + return nil + } + } + public var remoteAddress: SocketAddress? { + get { + return nil + } + } init() throws { #if os(Linux) - self.fd = Glibc.socket(AF_INET, Int32(SOCK_STREAM.rawValue), 0) + self.descriptor = Glibc.socket(AF_INET, Int32(SOCK_STREAM.rawValue), 0) #else - self.fd = Darwin.socket(AF_INET, Int32(SOCK_STREAM), 0) + self.descriptor = Darwin.socket(AF_INET, Int32(SOCK_STREAM), 0) #endif - if self.fd < 0 { + if self.descriptor < 0 { throw IOError(errno: errno, reason: "socket(...) failed") } self.open = true } - init(fd : Int32) { - self.fd = fd + init(descriptor : Int32) { + self.descriptor = descriptor self.open = true } - public func localAddress() -> SocketAddress? { - return nil - } - - public func remoteAddress() -> SocketAddress? { - return nil; - } - - public func descriptor() -> Int32 { - return fd - } - public func setNonBlocking() throws { - let res = fcntl(self.fd, F_SETFL, O_NONBLOCK) + let res = fcntl(self.descriptor, F_SETFL, O_NONBLOCK) guard res >= 0 else { throw IOError(errno: errno, reason: "fcntl(...) failed") @@ -66,21 +65,22 @@ public class Socket : Selectable { public func close() throws { #if os(Linux) - let res = Glibc.close(self.fd) + let res = Glibc.close(self.descriptor) #else - let res = Darwin.close(self.fd) + let res = Darwin.close(self.descriptor) #endif guard res >= 0 else { throw IOError(errno: errno, reason: "shutdown(...) failed") } + self.open = false } public func write(data: Data, offset: Int, len: Int) throws -> Int { let res = data.withUnsafeBytes() { [unowned self] (buffer: UnsafePointer) -> Int in #if os(Linux) - return Glibc.write(self.fd, buffer.advanced(by: offset), len) + return Glibc.write(self.descriptor, buffer.advanced(by: offset), len) #else - return Darwin.Glibc.write(self.fd, buffer.advanced(by: offset), len) + return Darwin.Glibc.write(self.descriptor, buffer.advanced(by: offset), len) #endif } @@ -97,9 +97,9 @@ public class Socket : Selectable { public func read(data: inout Data, offset: Int, len: Int) throws -> Int { let res = data.withUnsafeMutableBytes() { [unowned self] (buffer: UnsafeMutablePointer) -> Int in #if os(Linux) - return Glibc.read(self.fd, buffer.advanced(by: offset), len) + return Glibc.read(self.descriptor, buffer.advanced(by: offset), len) #else - return Darwin.read(self.fd, buffer.advanced(by: offset), len) + return Darwin.read(self.descriptor, buffer.advanced(by: offset), len) #endif }