Tweak sockets.
This commit is contained in:
@ -90,19 +90,23 @@ transfer sockets-builtins
|
||||
1 constant SOCK_STREAM
|
||||
2 constant SOCK_DGRAM
|
||||
3 constant SOCK_RAW
|
||||
|
||||
2 constant AF_INET
|
||||
16 constant sizeof(sockaddr_in)
|
||||
1 constant SOL_SOCKET
|
||||
2 constant SO_REUSEADDR
|
||||
: bs, ( n -- ) dup 256 / c, c, ;
|
||||
: s, ( n -- ) dup c, 256 / c, ;
|
||||
: l, ( n -- ) dup s, 65536 / s, ;
|
||||
|
||||
: bs, ( n -- ) dup 8 rshift c, c, ;
|
||||
: s, ( n -- ) dup c, 8 rshift c, ;
|
||||
: l, ( n -- ) dup s, 16 rshift s, ;
|
||||
: sockaddr create 16 c, AF_INET c, 0 bs, 0 l, 0 l, 0 l, ;
|
||||
: ->port@ ( a -- n ) 2 + >r r@ c@ 256 * r> 1+ c@ + ;
|
||||
: ->port! ( n a -- ) 2 + >r dup 256 / r@ c! r> 1+ c! ;
|
||||
: ->port@ ( a -- n ) 2 + >r r@ c@ 8 lshift r> 1+ c@ + ;
|
||||
: ->port! ( n a -- ) 2 + >r dup 8 rshift r@ c! r> 1+ c! ;
|
||||
: ->addr@ ( a -- n ) 4 + ul@ ;
|
||||
: ->addr! ( n a -- ) 4 + l! ;
|
||||
: ->h_addr ( hostent -- n ) 2 cells + 8 + @ @ ul@ ;
|
||||
: ip# ( n -- n ) dup 255 and n. [char] . emit 8 rshift ;
|
||||
: ip. ( n -- ) ip# ip# ip# 255 and n. ;
|
||||
forth definitions
|
||||
|
||||
vocabulary interrupts interrupts definitions
|
||||
|
||||
@ -16,12 +16,7 @@
|
||||
|
||||
:noname [ ' web-interface >body @ ] literal execute
|
||||
r|
|
||||
also streams also WiFi also web-interface definitions
|
||||
|
||||
: ip# dup 255 and n. [char] . emit 256 / ;
|
||||
: ip. ( n -- ) ip# ip# ip# 255 and . ;
|
||||
|
||||
also forth definitions
|
||||
also streams also WiFi also web-interface
|
||||
|
||||
: login ( z z -- )
|
||||
WIFI_MODE_STA Wifi.mode
|
||||
|
||||
@ -2,34 +2,35 @@
|
||||
|
||||
also sockets
|
||||
also tasks
|
||||
also posix
|
||||
|
||||
1024 constant max-msg
|
||||
create msg max-msg allot
|
||||
variable len max-msg len !
|
||||
-1 value sockfd
|
||||
|
||||
variable len max-msg len !
|
||||
sockaddr incoming
|
||||
sockaddr outgoing
|
||||
|
||||
sockaddr received
|
||||
variable received-len sizeof(sockaddr_in) received-len !
|
||||
|
||||
: reader
|
||||
begin
|
||||
sockfd msg len 0 0 0 recvfrom
|
||||
sockfd msg len 0 received received-len recvfrom
|
||||
dup 0 >= if
|
||||
msg swap type cr
|
||||
received ->addr@ ip. ." :" received ->port@ . space space msg swap type cr
|
||||
else drop then
|
||||
pause
|
||||
again
|
||||
;
|
||||
' reader 10 10 task reader-task
|
||||
|
||||
sockaddr incoming
|
||||
sockaddr outgoing
|
||||
|
||||
: udp ( port -- )
|
||||
incoming ->port!
|
||||
AF_INET SOCK_DGRAM 0 socket to sockfd
|
||||
sockfd non-block throw
|
||||
sockfd incoming sizeof(sockaddr_in) bind throw
|
||||
reader-task start-task
|
||||
stdin non-block throw
|
||||
;
|
||||
|
||||
: say ( port -- )
|
||||
|
||||
@ -42,15 +42,17 @@ z" gethostbyname" 1 sysfunc gethostbyname
|
||||
1 constant SOL_SOCKET
|
||||
2 constant SO_REUSEADDR
|
||||
|
||||
: bs, ( n -- ) dup 256 / c, c, ;
|
||||
: s, ( n -- ) dup c, 256 / c, ;
|
||||
: l, ( n -- ) dup s, 65536 / s, ;
|
||||
: bs, ( n -- ) dup 8 rshift c, c, ;
|
||||
: s, ( n -- ) dup c, 8 rshift c, ;
|
||||
: l, ( n -- ) dup s, 16 rshift s, ;
|
||||
: sockaddr create AF_INET s, 0 bs, 0 l, 0 l, 0 l, ;
|
||||
: ->port@ ( a -- n ) 2 + >r r@ c@ 256 * r> 1+ c@ + ;
|
||||
: ->port! ( n a -- ) 2 + >r dup 256 / r@ c! r> 1+ c! ;
|
||||
: ->port@ ( a -- n ) 2 + >r r@ c@ 8 lshift r> 1+ c@ + ;
|
||||
: ->port! ( n a -- ) 2 + >r dup 8 rshift r@ c! r> 1+ c! ;
|
||||
: ->addr@ ( a -- n ) 4 + ul@ ;
|
||||
: ->addr! ( n a -- ) 4 + l! ;
|
||||
: ->h_addr ( hostent -- n ) 2 cells + 8 + @ @ ul@ ;
|
||||
: ip# ( n -- n ) dup 255 and n. [char] . emit 8 rshift ;
|
||||
: ip. ( n -- ) ip# ip# ip# 255 and n. ;
|
||||
|
||||
( Fixup return )
|
||||
: sockaccept sockaccept sign-extend ;
|
||||
|
||||
Reference in New Issue
Block a user