@@ -91,9 +91,6 @@ public final class TCPSocket {
9191 /// Read data from the socket into the supplied buffer.
9292 /// Returns the amount of bytes actually read.
9393 public func read( into buffer: MutableByteBuffer ) throws -> TCPSocketStatus {
94- guard !isClosed else {
95- throw TCPError ( identifier: " read " , reason: " Socket is closed. " )
96- }
9794 let receivedBytes = COperatingSystem . read ( descriptor, buffer. baseAddress!, buffer. count)
9895
9996 guard receivedBytes != - 1 else {
@@ -110,6 +107,9 @@ public final class TCPSocket {
110107 case EAGAIN, EWOULDBLOCK:
111108 // no data yet
112109 return . wouldBlock
110+ case EBADF:
111+ assert ( isClosed, " EBADF when socket not closed " )
112+ throw TCPError ( identifier: " read " , reason: " Socket is closed. " )
113113 default :
114114 throw TCPError . posix ( errno, identifier: " read " )
115115 }
@@ -129,10 +129,6 @@ public final class TCPSocket {
129129
130130 /// Writes all data from the pointer's position with the length specified to this socket.
131131 public func write( from buffer: ByteBuffer ) throws -> TCPSocketStatus {
132- guard !isClosed else {
133- throw TCPError ( identifier: " write " , reason: " Socket is closed. " )
134- }
135-
136132 guard let pointer = buffer. baseAddress else {
137133 return . success( count: 0 )
138134 }
@@ -148,10 +144,8 @@ public final class TCPSocket {
148144 self . close ( )
149145 return . success( count: 0 )
150146 case EBADF:
151- // closed by peer, need to close this side.
152- // Since this is not an error, no need to throw unless the close
153- // itself throws an error.
154- return . success( count: 0 )
147+ assert ( isClosed, " EBADF when socket not closed " )
148+ throw TCPError ( identifier: " write " , reason: " Socket is closed. " )
155149 case EAGAIN, EWOULDBLOCK:
156150 return . wouldBlock
157151 default :
0 commit comments