factor/library/win32/win32-server.factor

114 lines
4.0 KiB
Factor
Raw Normal View History

2004-12-27 06:56:05 -05:00
! $Id$
!
2005-02-18 03:48:56 -05:00
! Copyright (C) 2004, 2005 Mackenzie Straight.
2004-12-27 06:56:05 -05:00
!
! Redistribution and use in source and binary forms, with or without
! modification, are permitted provided that the following conditions are met:
!
! 1. Redistributions of source code must retain the above copyright notice,
! this list of conditions and the following disclaimer.
!
! 2. Redistributions in binary form must reproduce the above copyright notice,
! this list of conditions and the following disclaimer in the documentation
! and/or other materials provided with the distribution.
!
! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
IN: win32-stream
2005-02-18 03:48:56 -05:00
USING: alien errors generic kernel kernel-internals lists math namespaces
prettyprint sequences io strings threads win32-api
2005-04-03 17:38:57 -04:00
win32-io-internals io-internals ;
2004-12-27 06:56:05 -05:00
TUPLE: win32-server this ;
2005-05-01 18:56:31 -04:00
TUPLE: win32-client-stream host port ;
2004-12-27 06:56:05 -05:00
SYMBOL: winsock
SYMBOL: socket
: maybe-init-winsock ( -- )
winsock get [
HEX: 0202 <wsadata> WSAStartup drop winsock on
] unless ;
: handle-socket-error ( -- )
WSAGetLastError [
ERROR_IO_PENDING ERROR_SUCCESS
2005-07-16 22:16:18 -04:00
] member? [
2004-12-27 06:56:05 -05:00
win32-error-message throw
] unless ;
: new-socket ( -- socket )
AF_INET SOCK_STREAM 0 NULL NULL WSA_FLAG_OVERLAPPED WSASocket ;
: setup-sockaddr ( port -- sockaddr )
<sockaddr-in> swap
htons over set-sockaddr-in-port
INADDR_ANY over set-sockaddr-in-addr
AF_INET over set-sockaddr-in-family ;
: bind-socket ( port socket -- )
2005-05-01 18:56:31 -04:00
swap setup-sockaddr "sockaddr-in" c-size wsa-bind 0 = [
2004-12-27 06:56:05 -05:00
handle-socket-error
] unless ;
: listen-socket ( socket -- )
20 wsa-listen 0 = [ handle-socket-error ] unless ;
2005-05-01 18:56:31 -04:00
: sockaddr> ( sockaddr -- port host )
dup sockaddr-in-port ntohs swap sockaddr-in-addr inet-ntoa ;
2005-05-01 18:56:31 -04:00
: extract-remote-host ( buffer -- port host )
buffer-ptr <alien> 0 32 32 <indirect-pointer> <indirect-pointer>
<indirect-pointer> dup >r <indirect-pointer>
2005-05-01 18:56:31 -04:00
GetAcceptExSockaddrs r> indirect-pointer-value <alien> sockaddr> ;
C: win32-client-stream ( buf stream -- stream )
[ set-delegate extract-remote-host ] keep
2005-05-01 18:56:31 -04:00
[ set-win32-client-stream-host ] keep
[ set-win32-client-stream-port ] keep ;
M: win32-client-stream client-stream-host win32-client-stream-host ;
2005-05-01 18:56:31 -04:00
M: win32-client-stream client-stream-port win32-client-stream-port ;
2004-12-27 06:56:05 -05:00
C: win32-server ( port -- server )
swap [
2004-12-27 06:56:05 -05:00
maybe-init-winsock new-socket swap over bind-socket dup listen-socket
2004-12-29 02:16:03 -05:00
dup add-completion
2004-12-27 06:56:05 -05:00
socket set
2005-07-23 02:11:07 -04:00
dup stream set
] make-hash over set-win32-server-this ;
2004-12-27 06:56:05 -05:00
2005-02-14 22:15:02 -05:00
M: win32-server stream-close ( server -- )
win32-server-this [ socket get CloseHandle drop ] bind ;
2004-12-27 06:56:05 -05:00
2005-07-23 02:11:07 -04:00
M: win32-server set-timeout ( timeout server -- )
win32-server-this [ timeout set ] bind ;
M: win32-server expire ( -- )
win32-server-this [
timeout get [ millis cutoff get > [ socket get CancelIo ] when ] when
] bind ;
IN: io
2005-05-01 18:56:31 -04:00
: accept ( server -- client )
win32-server-this [
2005-07-23 02:11:07 -04:00
update-timeout new-socket 64 <buffer>
2004-12-27 06:56:05 -05:00
[
2005-07-23 02:11:07 -04:00
stream get alloc-io-callback init-overlapped
>r >r >r socket get r> r>
2005-01-02 16:14:34 -05:00
buffer-ptr <alien> 0 32 32 NULL r> AcceptEx
2005-04-22 00:22:36 -04:00
[ handle-socket-error ] unless stop
2005-02-12 02:23:38 -05:00
] callcc1 pending-error drop
2005-07-23 02:11:07 -04:00
swap dup add-completion <win32-stream> <line-reader>
dupd <win32-client-stream> swap buffer-free
2004-12-27 06:56:05 -05:00
] bind ;