! Copyright (C) 2010 John Benediktsson ! See http://factorcode.org/license.txt for BSD license USING: alien.c-types bit-arrays byte-arrays classes.struct destructors io.sockets.private io.sockets.unix unix.ffi unix.time ; IN: udp : ( -- fd ) [ AF_INET SOCK_DGRAM socket-fd fd>> ] with-destructors ; : udp-send ( fd byte-array addr -- ) [ dup length 0 ] dip make-sockaddr/size sendto 0 < [ "send error" throw ] when ; : udp-recv ( fd n -- byte-array ) [ dup length 0 recv ] keep swap head ; ! Factor's bit-arrays are an array of bytes, OS X expects ! FD_SET to be an array of cells, so we have to account for ! byte order differences on big endian platforms : munge ( i -- i' ) little-endian? [ BIN: 11000 bitxor ] unless ; inline : create-fdset ( fd -- fdset ) [ t ] dip munge FD_SETSIZE 8 * [ set-nth ] keep ; : wait-for-read ( fd -- ) [ 1 + ] [ create-fdset ] bi f f timeval 1 >>sec select 0 < [ "select error" throw ] when ;