Skip to content

Commit dabecb4

Browse files
committed
add pipe to stream
1 parent 1808d15 commit dabecb4

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

Aeon/HTTPServer/TCPStream.swift

+15-9
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import Stream
2828

2929
final class TCPStream: StreamType {
3030
let socket: TCPClientSocket
31-
let channel: IOChannel
32-
var receive: ((Void throws -> [Int8]) -> Void)?
31+
var channel: IOChannel!
32+
var piped: Bool = false
3333

3434
init(socket: TCPClientSocket) {
3535
self.socket = socket
@@ -38,25 +38,25 @@ final class TCPStream: StreamType {
3838
result.failure { error in
3939
print(error)
4040
}
41-
socket.close()
41+
if !self.piped {
42+
socket.close()
43+
}
4244
}
4345

4446
channel.setLowWater(1)
47+
}
4548

49+
func receive(completion: (Void throws -> [Int8]) -> Void) {
4650
channel.read { result in
4751
result.success { done, data in
48-
self.receive?({ data })
52+
completion({ data })
4953
}
5054
result.failure { error in
51-
self.receive?({ throw error })
55+
completion({ throw error })
5256
}
5357
}
5458
}
5559

56-
func receive(receive: (Void throws -> [Int8]) -> Void) {
57-
self.receive = receive
58-
}
59-
6060
func send(data: [Int8], completion: (Void throws -> Void) -> Void) {
6161
channel.write(data: data) { result in
6262
result.success { done, _ in
@@ -73,4 +73,10 @@ final class TCPStream: StreamType {
7373
func close() {
7474
channel.close()
7575
}
76+
77+
func pipe() -> StreamType {
78+
piped = true
79+
channel.close()
80+
return TCPStream(socket: socket)
81+
}
7682
}

Aeon/HTTPServerType/HTTPServerType.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ extension HTTPServerType {
5757
self.serializer.serializeResponse(stream, response: response) { serializeResult in
5858
do {
5959
try serializeResult()
60-
completion({ stream })
60+
completion({ stream.pipe() })
6161
} catch {
6262
completion({ throw error })
6363
failure(error)

HTTPParser

0 commit comments

Comments
 (0)