@@ -28,8 +28,8 @@ import Stream
28
28
29
29
final class TCPStream : StreamType {
30
30
let socket : TCPClientSocket
31
- let channel : IOChannel
32
- var receive : ( ( Void throws -> [ Int8 ] ) -> Void ) ?
31
+ var channel : IOChannel !
32
+ var piped : Bool = false
33
33
34
34
init ( socket: TCPClientSocket ) {
35
35
self . socket = socket
@@ -38,25 +38,25 @@ final class TCPStream: StreamType {
38
38
result. failure { error in
39
39
print ( error)
40
40
}
41
- socket. close ( )
41
+ if !self . piped {
42
+ socket. close ( )
43
+ }
42
44
}
43
45
44
46
channel. setLowWater ( 1 )
47
+ }
45
48
49
+ func receive( completion: ( Void throws -> [ Int8 ] ) -> Void ) {
46
50
channel. read { result in
47
51
result. success { done, data in
48
- self . receive ? ( { data } )
52
+ completion ( { data } )
49
53
}
50
54
result. failure { error in
51
- self . receive ? ( { throw error } )
55
+ completion ( { throw error } )
52
56
}
53
57
}
54
58
}
55
59
56
- func receive( receive: ( Void throws -> [ Int8 ] ) -> Void ) {
57
- self . receive = receive
58
- }
59
-
60
60
func send( data: [ Int8 ] , completion: ( Void throws -> Void ) -> Void ) {
61
61
channel. write ( data: data) { result in
62
62
result. success { done, _ in
@@ -73,4 +73,10 @@ final class TCPStream: StreamType {
73
73
func close( ) {
74
74
channel. close ( )
75
75
}
76
+
77
+ func pipe( ) -> StreamType {
78
+ piped = true
79
+ channel. close ( )
80
+ return TCPStream ( socket: socket)
81
+ }
76
82
}
0 commit comments