factor/extra/io/windows/ce/sockets/sockets.factor

114 lines
3.3 KiB
Factor
Raw Normal View History

2007-11-06 20:44:45 -05:00
USING: alien alien.c-types combinators io io.backend io.buffers
2008-05-13 19:24:46 -04:00
io.ports io.sockets io.windows kernel libc
2007-11-06 20:44:45 -05:00
math namespaces prettyprint qualified sequences strings threads
2007-11-07 14:01:45 -05:00
threads.private windows windows.kernel32 io.windows.ce.backend
2008-04-02 21:09:56 -04:00
byte-arrays system ;
2007-11-06 20:44:45 -05:00
QUALIFIED: windows.winsock
IN: io.windows.ce
2008-04-02 21:09:56 -04:00
M: wince WSASocket-flags ( -- DWORD ) 0 ;
2007-11-06 20:44:45 -05:00
M: win32-socket wince-read ( port port-handle -- )
2007-11-07 14:01:45 -05:00
win32-file-handle over buffer-end pick buffer-capacity 0
windows.winsock:recv
dup windows.winsock:SOCKET_ERROR = [
2007-11-06 20:44:45 -05:00
drop port-errored
] [
2007-11-11 16:09:24 -05:00
dup zero?
[ drop t swap set-port-eof? ] [ swap n>buffer ] if
2007-11-06 20:44:45 -05:00
] if ;
M: win32-socket wince-write ( port port-handle -- )
2007-11-07 14:01:45 -05:00
win32-file-handle over buffer@ pick buffer-length 0
windows.winsock:send
dup windows.winsock:SOCKET_ERROR =
2007-11-09 03:01:45 -05:00
[ drop port-errored ] [ swap buffer-consume ] if ;
2007-11-06 20:44:45 -05:00
: do-connect ( addrspec -- socket )
[ tcp-socket dup ] keep
make-sockaddr/size
2007-11-11 16:09:24 -05:00
f f f f
windows.winsock:WSAConnect
windows.winsock:winsock-error!=0/f ;
2007-11-06 20:44:45 -05:00
2008-04-02 21:09:56 -04:00
M: wince (client) ( addrspec -- reader writer )
do-connect <win32-socket> dup <ports> ;
2007-11-06 20:44:45 -05:00
2008-04-02 21:09:56 -04:00
M: wince (server) ( addrspec -- handle )
windows.winsock:SOCK_STREAM server-fd
dup listen-on-socket
<win32-socket> ;
2007-11-06 20:44:45 -05:00
2008-04-02 21:09:56 -04:00
M: wince (accept) ( server -- client )
2007-11-06 20:44:45 -05:00
[
2008-01-31 13:27:37 -05:00
[
dup port-handle win32-file-handle
swap server-port-addr sockaddr-type heap-size
dup <byte-array> [
swap <int> f 0
windows.winsock:WSAAccept
dup windows.winsock:INVALID_SOCKET =
[ windows.winsock:winsock-error ] when
] keep
] keep server-port-addr parse-sockaddr swap
<win32-socket> <ports>
2008-02-09 22:34:42 -05:00
] with-timeout ;
2007-11-06 20:44:45 -05:00
2008-04-02 21:09:56 -04:00
M: wince <datagram> ( addrspec -- datagram )
2007-11-06 20:44:45 -05:00
[
2008-01-21 15:33:43 -05:00
windows.winsock:SOCK_DGRAM server-fd <win32-socket>
2007-11-06 20:44:45 -05:00
] keep <datagram-port> ;
2007-11-11 16:09:24 -05:00
: packet-size 65536 ; inline
: receive-buffer ( -- buf )
\ receive-buffer get-global expired? [
packet-size malloc \ receive-buffer set-global
] when
\ receive-buffer get-global ;
: make-WSABUF ( len buf -- ptr )
"WSABUF" <c-object>
[ windows.winsock:set-WSABUF-buf ] keep
[ windows.winsock:set-WSABUF-len ] keep ;
: receive-WSABUF ( -- buf )
packet-size receive-buffer make-WSABUF ;
: packet-data ( len -- byte-array )
receive-buffer swap memory>byte-array ;
2007-11-11 16:09:24 -05:00
packet-size <byte-array> receive-buffer set-global
2008-04-02 21:09:56 -04:00
M: wince receive ( datagram -- packet addrspec )
2007-11-06 20:44:45 -05:00
dup check-datagram-port
[
2007-11-07 14:01:45 -05:00
port-handle win32-file-handle
2007-11-11 16:09:24 -05:00
receive-WSABUF
1
0 <uint> [
0 <uint>
64 "char" <c-array> [
64 <int>
f
f
windows.winsock:WSARecvFrom
windows.winsock:winsock-error!=0/f
] keep
] keep *uint packet-data swap
] keep datagram-port-addr parse-sockaddr ;
: send-WSABUF ( byte-array -- ptr )
dup length packet-size > [ "UDP packet too long" throw ] when
dup length receive-buffer rot pick memcpy
receive-buffer make-WSABUF ;
2007-11-06 20:44:45 -05:00
2008-04-02 21:09:56 -04:00
M: wince send ( packet addrspec datagram -- )
2007-11-06 20:44:45 -05:00
3dup check-datagram-send
2007-11-07 14:01:45 -05:00
port-handle win32-file-handle
2007-11-11 16:09:24 -05:00
rot send-WSABUF
2007-11-06 20:44:45 -05:00
rot make-sockaddr/size
>r >r 1 0 <uint> 0 r> r> f f
2007-11-11 16:09:24 -05:00
windows.winsock:WSASendTo
windows.winsock:winsock-error!=0/f ;