Skip to content

Commit 985755e

Browse files
authored
Merge pull request #163 from vapor/is-closed-fix
Is closed fix
2 parents f4bd75b + 96515a5 commit 985755e

File tree

5 files changed

+24
-2
lines changed

5 files changed

+24
-2
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let package = Package(
1111
.package(url: "https://github.com/vapor/async.git", "1.0.0-beta.1"..<"1.0.0-beta.2"),
1212

1313
// Core extensions, type-aliases, and functions that facilitate common tasks.
14-
.package(url: "https://github.com/vapor/core.git", "3.0.0-beta.1"..<"3.0.0-beta.2"),
14+
.package(url: "https://github.com/vapor/core.git", "3.0.0-beta.2"..<"3.0.0-beta.3"),
1515
],
1616
targets: [
1717
.target(name: "TCP", dependencies: ["Async", "Bits", "COperatingSystem", "Debugging"]),

Sources/TCP/Socket/TCPSocket.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public final class TCPSocket {
1717
/// True if the socket should re-use addresses
1818
public let shouldReuseAddress: Bool
1919

20+
/// True if the socket has been closed.
21+
public var isClosed: Bool {
22+
return descriptor < 0
23+
}
24+
2025
/// Creates a TCP socket around an existing descriptor
2126
public init(
2227
established: Int32,
@@ -86,6 +91,9 @@ public final class TCPSocket {
8691
/// Read data from the socket into the supplied buffer.
8792
/// Returns the amount of bytes actually read.
8893
public func read(into buffer: MutableByteBuffer) throws -> TCPSocketStatus {
94+
guard !isClosed else {
95+
throw TCPError(identifier: "read", reason: "Socket is closed.")
96+
}
8997
let receivedBytes = COperatingSystem.read(descriptor, buffer.baseAddress!, buffer.count)
9098

9199
guard receivedBytes != -1 else {
@@ -121,6 +129,10 @@ public final class TCPSocket {
121129

122130
/// Writes all data from the pointer's position with the length specified to this socket.
123131
public func write(from buffer: ByteBuffer) throws -> TCPSocketStatus {
132+
guard !isClosed else {
133+
throw TCPError(identifier: "write", reason: "Socket is closed.")
134+
}
135+
124136
guard let pointer = buffer.baseAddress else {
125137
return .success(count: 0)
126138
}

Sources/TCP/Streams/TCPSocketSink.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ public final class TCPSocketSink: Async.InputStream {
9797
/// Writes the buffered data to the socket.
9898
private func writeData(ready: Promise<Void>) {
9999
DEBUG("TCPSocketSink.writeData(\(ready))")
100+
guard !socket.isClosed else {
101+
close()
102+
return
103+
}
104+
100105
do {
101106
guard let buffer = self.inputBuffer else {
102107
ERROR("Unexpected nil SocketSink inputBuffer during writeData")

Sources/TCP/Streams/TCPSocketSource.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ public final class TCPSocketSource: Async.OutputStream {
8888
/// as indicated by a read source.
8989
private func readData() {
9090
DEBUG("TCPSocketSource.readData()")
91+
guard !socket.isClosed else {
92+
close()
93+
return
94+
}
95+
9196
guard let downstream = self.downstream else {
9297
ERROR("Unexpected nil downstream on SocketSource during readData.")
9398
return

Sources/TCP/Utilties/TCPError.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Debugging
22
import COperatingSystem
33

44
/// Errors that can be thrown while working with TCP sockets.
5-
public struct TCPError: Traceable, Debuggable, Helpable, Swift.Error, Encodable {
5+
public struct TCPError: Debuggable {
66
public static let readableName = "TCP Error"
77
public let identifier: String
88
public var reason: String

0 commit comments

Comments
 (0)