factor/extra/io/sockets/sockets.factor

59 lines
1.5 KiB
Factor
Raw Normal View History

2008-02-21 16:22:49 -05:00
! Copyright (C) 2007, 2008 Slava Pestov, Daniel Ehrenberg.
2007-09-20 18:09:08 -04:00
! See http://factorcode.org/license.txt for BSD license.
USING: generic kernel io.backend namespaces continuations
2008-04-11 17:08:40 -04:00
sequences arrays io.encodings io.nonblocking accessors ;
IN: io.sockets
2007-09-20 18:09:08 -04:00
TUPLE: local path ;
2008-03-31 14:51:34 -04:00
: <local> ( path -- addrspec )
normalize-path local construct-boa ;
2007-09-20 18:09:08 -04:00
TUPLE: inet4 host port ;
C: <inet4> inet4
TUPLE: inet6 host port ;
C: <inet6> inet6
TUPLE: inet host port ;
C: <inet> inet
2008-04-11 17:08:40 -04:00
HOOK: ((client)) io-backend ( addrspec -- client-in client-out )
2007-09-20 18:09:08 -04:00
2008-04-11 17:08:40 -04:00
GENERIC: (client) ( addrspec -- client-in client-out )
M: array (client) [ ((client)) 2array ] attempt-all first2 ;
M: object (client) ((client)) ;
2007-09-20 18:09:08 -04:00
2008-02-21 16:22:49 -05:00
: <client> ( addrspec encoding -- stream )
2008-04-11 17:08:40 -04:00
>r (client) r> <encoder-duplex> ;
HOOK: (server) io-backend ( addrspec -- handle )
: <server> ( addrspec encoding -- server )
>r [ (server) ] keep r> <server-port> ;
2007-09-20 18:09:08 -04:00
HOOK: (accept) io-backend ( server -- addrspec handle )
2007-09-20 18:09:08 -04:00
2008-04-11 17:08:40 -04:00
: accept ( server -- client addrspec )
[ (accept) dup <reader&writer> ] [ encoding>> ] bi
<encoder-duplex> swap ;
2007-09-20 18:09:08 -04:00
HOOK: <datagram> io-backend ( addrspec -- datagram )
HOOK: receive io-backend ( datagram -- packet addrspec )
HOOK: send io-backend ( packet addrspec datagram -- )
HOOK: resolve-host io-backend ( host serv passive? -- seq )
HOOK: host-name io-backend ( -- string )
2008-04-11 17:08:40 -04:00
M: inet (client)
[ host>> ] [ port>> ] bi f resolve-host
[ empty? [ "Host name lookup failed" throw ] when ]
[ (client) ]
bi ;