1 ! Copyright (C) 2005, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel continuations destructors io io.encodings
4 io.encodings.private io.timeouts io.ports summary
5 accessors delegate delegate.protocols ;
8 TUPLE: duplex-stream in out ;
10 C: <duplex-stream> duplex-stream
12 CONSULT: input-stream-protocol duplex-stream in>> ;
14 CONSULT: output-stream-protocol duplex-stream out>> ;
16 M: duplex-stream set-timeout
17 [ in>> set-timeout ] [ out>> set-timeout ] 2bi ;
19 M: duplex-stream dispose
20 #! The output stream is closed first, in case both streams
21 #! are attached to the same file descriptor, the output
22 #! buffer needs to be flushed before we close the fd.
24 [ in>> &dispose drop ]
25 [ out>> &dispose drop ]
29 : <encoder-duplex> ( stream-in stream-out encoding -- duplex )
30 tuck [ re-decode ] [ re-encode ] 2bi* <duplex-stream> ;
32 : with-stream* ( stream quot -- )
33 [ [ in>> ] [ out>> ] bi ] dip with-streams* ; inline
35 : with-stream ( stream quot -- )
36 [ [ in>> ] [ out>> ] bi ] dip with-streams ; inline
38 ERROR: invalid-duplex-stream ;
40 M: duplex-stream underlying-handle
41 [ in>> underlying-handle ]
42 [ out>> underlying-handle ] bi
43 [ = [ invalid-duplex-stream ] when ] keep ;