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.

This commit is contained in:
Bill Abt
2016-09-13 13:06:43 -04:00
parent 1da9d704a9
commit af73b59e7d
2 changed files with 10 additions and 0 deletions
+1
View File
@@ -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<CChar>, 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.
+9
View File
@@ -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
}