Improved documentation. Wrapper socket fd functions in FD class and changed names to look more familiar with their 'C' counterparts. i.e. FD_SET in C becomes FD.SET in Swift.
This commit is contained in:
@@ -28,6 +28,9 @@
|
||||
|
||||
// MARK: Socket
|
||||
|
||||
///
|
||||
/// Low level BSD sockets wrapper.
|
||||
///
|
||||
public class Socket: SocketReader, SocketWriter {
|
||||
|
||||
// MARK: Constants
|
||||
@@ -1836,12 +1839,12 @@ public class Socket: SocketReader, SocketWriter {
|
||||
|
||||
// Create a read and write file descriptor set for this socket...
|
||||
var readfds = fd_set()
|
||||
fdZero(set: &readfds)
|
||||
fdSet(fd: self.socketfd, set: &readfds)
|
||||
FD.ZERO(set: &readfds)
|
||||
FD.SET(fd: self.socketfd, set: &readfds)
|
||||
|
||||
var writefds = fd_set()
|
||||
fdZero(set: &writefds)
|
||||
fdSet(fd: self.socketfd, set: &writefds)
|
||||
FD.ZERO(set: &writefds)
|
||||
FD.SET(fd: self.socketfd, set: &writefds)
|
||||
|
||||
// Create a timeout of zero (i.e. don't wait)...
|
||||
var timeout = timeval()
|
||||
@@ -1856,7 +1859,7 @@ public class Socket: SocketReader, SocketWriter {
|
||||
}
|
||||
|
||||
// Return a tuple containing whether or not this socket is readable and/or writable...
|
||||
return (fdIsSet(fd: self.socketfd, set: &readfds), fdIsSet(fd: self.socketfd, set: &writefds))
|
||||
return (FD.ISSET(fd: self.socketfd, set: &readfds), FD.ISSET(fd: self.socketfd, set: &writefds))
|
||||
}
|
||||
|
||||
///
|
||||
|
||||
@@ -22,6 +22,9 @@ import Foundation
|
||||
|
||||
// MARK: Reader
|
||||
|
||||
///
|
||||
/// Socket reader protocol
|
||||
///
|
||||
public protocol SocketReader {
|
||||
|
||||
///
|
||||
@@ -32,7 +35,7 @@ public protocol SocketReader {
|
||||
func readString() throws -> String?
|
||||
|
||||
///
|
||||
/// Reads all available data
|
||||
/// Reads all available data into an NSMutableData object.
|
||||
///
|
||||
/// - Parameter data: NSMutableData object to contain read data.
|
||||
///
|
||||
@@ -43,10 +46,13 @@ public protocol SocketReader {
|
||||
|
||||
// MARK: Writer
|
||||
|
||||
///
|
||||
/// Socket writer protocol
|
||||
///
|
||||
public protocol SocketWriter {
|
||||
|
||||
///
|
||||
/// Writes data
|
||||
/// Writes data from NSData object.
|
||||
///
|
||||
/// - Parameter data: NSData object containing the data to be written.
|
||||
///
|
||||
|
||||
@@ -114,16 +114,18 @@ public extension sockaddr_in6 {
|
||||
|
||||
#if os(Linux)
|
||||
|
||||
public struct FD {
|
||||
|
||||
/// Replacement for FD_ZERO macro
|
||||
|
||||
func fdZero(set: inout fd_set) {
|
||||
public static func ZERO(set: inout fd_set) {
|
||||
set.__fds_bits = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
|
||||
}
|
||||
|
||||
|
||||
/// Replacement for FD_SET macro
|
||||
|
||||
func fdSet(fd: Int32, set: inout fd_set) {
|
||||
public static func SET(fd: Int32, set: inout fd_set) {
|
||||
let intOffset = Int(fd / 16)
|
||||
let bitOffset: Int = Int(fd % 16)
|
||||
let mask: Int = 1 << bitOffset
|
||||
@@ -151,7 +153,7 @@ public extension sockaddr_in6 {
|
||||
|
||||
/// Replacement for FD_CLR macro
|
||||
|
||||
func fdClr(fd: Int32, set: inout fd_set) {
|
||||
public static func CLR(fd: Int32, set: inout fd_set) {
|
||||
let intOffset = Int(fd / 16)
|
||||
let bitOffset: Int = Int(fd % 16)
|
||||
let mask: Int = ~(1 << bitOffset)
|
||||
@@ -179,7 +181,7 @@ public extension sockaddr_in6 {
|
||||
|
||||
/// Replacement for FD_ISSET macro
|
||||
|
||||
func fdIsSet(fd: Int32, set: inout fd_set) -> Bool {
|
||||
public static func ISSET(fd: Int32, set: inout fd_set) -> Bool {
|
||||
let intOffset = Int(fd / 16)
|
||||
let bitOffset = Int(fd % 16)
|
||||
let mask: Int = 1 << bitOffset
|
||||
@@ -204,19 +206,22 @@ public extension sockaddr_in6 {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
public struct FD {
|
||||
|
||||
/// Replacement for FD_ZERO macro
|
||||
|
||||
func fdZero(set: inout fd_set) {
|
||||
public static func ZERO(set: inout fd_set) {
|
||||
set.fds_bits = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
|
||||
}
|
||||
|
||||
|
||||
/// Replacement for FD_SET macro
|
||||
|
||||
func fdSet(fd: Int32, set: inout fd_set) {
|
||||
public static func SET(fd: Int32, set: inout fd_set) {
|
||||
let intOffset = Int(fd / 32)
|
||||
let bitOffset = fd % 32
|
||||
let mask = 1 << bitOffset
|
||||
@@ -260,7 +265,7 @@ public extension sockaddr_in6 {
|
||||
|
||||
/// Replacement for FD_CLR macro
|
||||
|
||||
func fdClr(fd: Int32, set: inout fd_set) {
|
||||
public static func CLR(fd: Int32, set: inout fd_set) {
|
||||
let intOffset = Int(fd / 32)
|
||||
let bitOffset = fd % 32
|
||||
let mask = ~(1 << bitOffset)
|
||||
@@ -304,7 +309,7 @@ public extension sockaddr_in6 {
|
||||
|
||||
/// Replacement for FD_ISSET macro
|
||||
|
||||
func fdIsSet(fd: Int32, set: inout fd_set) -> Bool {
|
||||
public static func ISSET(fd: Int32, set: inout fd_set) -> Bool {
|
||||
let intOffset = Int(fd / 32)
|
||||
let bitOffset = fd % 32
|
||||
let mask = 1 << bitOffset
|
||||
@@ -345,5 +350,6 @@ public extension sockaddr_in6 {
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user