Skip to content

Commit 9e81d8a

Browse files
author
Bill Abt
committed
Changes in response to Issue #6. Socket.wait() now supports a timed wait based on a passed timer value, an immediate return (i.e. quick check) and an indefinite wait.
1 parent 4974f5c commit 9e81d8a

1 file changed

Lines changed: 21 additions & 6 deletions

File tree

Sources/Socket.swift

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public class Socket: SocketReader, SocketWriter {
7878
public static let SOCKET_ERR_BAD_SIGNATURE_PARAMETERS = -9976
7979
public static let SOCKET_ERR_INTERNAL = -9975
8080
public static let SOCKET_ERR_WRONG_PROTOCOL = -9974
81+
public static let SOCKET_ERR_NOT_ACTIVE = -9973
8182

8283
// MARK: Enums
8384

@@ -601,6 +602,14 @@ public class Socket: SocketReader, SocketWriter {
601602
///
602603
public private(set) var isListening: Bool = false
603604

605+
///
606+
/// True if the socket is listening or connected.
607+
///
608+
public var isActive: Bool {
609+
610+
return isListening || isConnected
611+
}
612+
604613
///
605614
/// Listening port (-1 if not listening)
606615
///
@@ -788,11 +797,12 @@ public class Socket: SocketReader, SocketWriter {
788797
///
789798
/// - Parameters:
790799
/// - sockets: An array of sockets to be monitored.
791-
/// - timeout: Timeout (in msec) before returning.
800+
/// - timeout: Timeout (in msec) before returning. A timeout value of 0 will return immediately.
801+
/// - waitForever: If true, this function will wait indefinitely regardless of timeout value. Defaults to false.
792802
///
793803
/// - Returns: An optional array of sockets which have data available or nil if a timeout expires.
794804
///
795-
public class func wait(for sockets: [Socket], timeout: UInt) throws -> [Socket]? {
805+
public class func wait(for sockets: [Socket], timeout: UInt, waitForever: Bool = false) throws -> [Socket]? {
796806

797807
// Validate we have sockets to look for and they are valid...
798808
for socket in sockets {
@@ -801,15 +811,15 @@ public class Socket: SocketReader, SocketWriter {
801811

802812
throw Error(code: Socket.SOCKET_ERR_BAD_DESCRIPTOR, reason: nil)
803813
}
804-
if !socket.isConnected {
814+
if !socket.isActive {
805815

806-
throw Error(code: Socket.SOCKET_ERR_NOT_CONNECTED, reason: nil)
816+
throw Error(code: Socket.SOCKET_ERR_NOT_ACTIVE, reason: nil)
807817
}
808818
}
809819

810820
// Setup the timeout...
811821
var timer = timeval()
812-
if timeout > 0 {
822+
if timeout > 0 && !waitForever {
813823

814824
// First get seconds...
815825
let secs = Int(Double(timeout / 1000))
@@ -843,7 +853,12 @@ public class Socket: SocketReader, SocketWriter {
843853
}
844854

845855
// Issue the select...
846-
let count = select(highSocketfd + 1, &readfds, nil, nil, &timer)
856+
var count: Int32 = 0
857+
if waitForever {
858+
count = select(highSocketfd + 1, &readfds, nil, nil, nil)
859+
} else {
860+
count = select(highSocketfd + 1, &readfds, nil, nil, &timer)
861+
}
847862

848863
// A count of less than zero indicates select failed...
849864
if count < 0 {

0 commit comments

Comments
 (0)