2008-04-13 01:21:48 -04:00
|
|
|
! Copyright (C) 2005, 2008 Slava Pestov.
|
2007-09-20 18:09:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-05-15 00:23:12 -04:00
|
|
|
USING: kernel continuations destructors io io.encodings
|
2008-12-08 20:45:48 -05:00
|
|
|
io.encodings.private io.timeouts io.ports summary
|
|
|
|
accessors delegate delegate.protocols ;
|
2007-09-20 18:09:08 -04:00
|
|
|
IN: io.streams.duplex
|
|
|
|
|
2008-05-15 00:23:12 -04:00
|
|
|
TUPLE: duplex-stream in out ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-05-15 00:23:12 -04:00
|
|
|
C: <duplex-stream> duplex-stream
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-05-15 00:23:12 -04:00
|
|
|
CONSULT: input-stream-protocol duplex-stream in>> ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-05-15 00:23:12 -04:00
|
|
|
CONSULT: output-stream-protocol duplex-stream out>> ;
|
2008-05-05 03:19:25 -04:00
|
|
|
|
|
|
|
M: duplex-stream set-timeout
|
2008-05-15 00:23:12 -04:00
|
|
|
[ in>> set-timeout ] [ out>> set-timeout ] 2bi ;
|
2008-05-05 03:19:25 -04:00
|
|
|
|
2008-01-31 01:52:06 -05:00
|
|
|
M: duplex-stream dispose
|
2007-09-20 18:09:08 -04:00
|
|
|
#! The output stream is closed first, in case both streams
|
|
|
|
#! are attached to the same file descriptor, the output
|
|
|
|
#! buffer needs to be flushed before we close the fd.
|
2008-05-15 00:23:12 -04:00
|
|
|
[
|
|
|
|
[ in>> &dispose drop ]
|
2008-05-20 08:55:42 -04:00
|
|
|
[ out>> &dispose drop ]
|
2008-05-15 00:23:12 -04:00
|
|
|
bi
|
|
|
|
] with-destructors ;
|
2008-05-05 03:19:25 -04:00
|
|
|
|
|
|
|
: <encoder-duplex> ( stream-in stream-out encoding -- duplex )
|
2008-12-02 04:10:13 -05:00
|
|
|
tuck [ re-decode ] [ re-encode ] 2bi* <duplex-stream> ;
|
2008-05-05 03:19:25 -04:00
|
|
|
|
|
|
|
: with-stream* ( stream quot -- )
|
2008-11-30 14:46:39 -05:00
|
|
|
[ [ in>> ] [ out>> ] bi ] dip with-streams* ; inline
|
2008-05-05 03:19:25 -04:00
|
|
|
|
|
|
|
: with-stream ( stream quot -- )
|
2008-11-30 14:46:39 -05:00
|
|
|
[ [ in>> ] [ out>> ] bi ] dip with-streams ; inline
|
|
|
|
|
|
|
|
ERROR: invalid-duplex-stream ;
|
|
|
|
|
|
|
|
M: duplex-stream underlying-handle
|
|
|
|
[ in>> underlying-handle ]
|
|
|
|
[ out>> underlying-handle ] bi
|
|
|
|
[ = [ invalid-duplex-stream ] when ] keep ;
|
|
|
|
|