From af73b59e7d896efb8dedf0a02cb891350079bf46 Mon Sep 17 00:00:00 2001 From: Bill Abt Date: Tue, 13 Sep 2016 13:06:43 -0400 Subject: [PATCH] Added new property, remoteConnectionClosed, to enable differentiation between a socket that is no longer connected to its' remote partner and one that would block. If this property returns true, the remote connection has been closed and the socket should be closed. --- README.md | 1 + Sources/Socket.swift | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/README.md b/README.md index 710fc9a..938a027 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,7 @@ In addition to the `create(connectedUsing:)` factory method described above, **B - `read(into data: NSMutableData)` - This function reads all the data available on a socket and returns it in the `NSMutableData` object that was passed. - `readString()` - This function reads all the data available on a socket and returns it as an `String`. A `nil` is returned if no data is available for reading. - `read(into buffer: UnsafeMutablePointer, bufSize: Int)` - This function allows you to read data into a buffer of a specified size by providing an *unsafe* pointer to that buffer and an integer the denotes the size of that buffer. This API (in addition to other types of exceptions) will throw a `Socket.SOCKET_ERR_RECV_BUFFER_TOO_SMALL` if the buffer provided is too small. You will need to call again with proper buffer size (see `Error.bufferSizeNeeded`in *Socket.swift* for more information). +- **Note:** All of the read APIs above except `readString()` can return zero (0). This can indicate that the remote connection was closed or it could indicate that the socket would block (assuming you've turned off blocking). To differentiate between the two, the property `remoteConnectionClosed` can be checked. If `true`, the socket remote partner has closed the connection and this `Socket` instance should be closed. ### Writing data to a Socket. diff --git a/Sources/Socket.swift b/Sources/Socket.swift index f733485..f7a6633 100644 --- a/Sources/Socket.swift +++ b/Sources/Socket.swift @@ -605,6 +605,12 @@ public class Socket: SocketReader, SocketWriter { /// public internal(set) var isListening: Bool = false + /// + /// True if this socket's remote connection has closed. (Readonly) + /// **Note:** This is only valid after a Socket is connected. + /// + public internal(set) var remoteConnectionClosed: Bool = false + /// /// True if the socket is listening or connected. /// @@ -1768,6 +1774,7 @@ public class Socket: SocketReader, SocketWriter { // Check for disconnect... if count == 0 { + self.remoteConnectionClosed = true return count } @@ -1844,6 +1851,7 @@ public class Socket: SocketReader, SocketWriter { // Check for disconnect... if count == 0 { + self.remoteConnectionClosed = true return count } @@ -1888,6 +1896,7 @@ public class Socket: SocketReader, SocketWriter { // Check for disconnect... if count == 0 { + self.remoteConnectionClosed = true return count }