Tweak sockets.

This commit is contained in:
Brad Nelson
2022-08-12 16:54:50 -07:00
parent b1612c116b
commit 40f45e47e3
4 changed files with 26 additions and 24 deletions

View File

@ -90,19 +90,23 @@ transfer sockets-builtins
1 constant SOCK_STREAM 1 constant SOCK_STREAM
2 constant SOCK_DGRAM 2 constant SOCK_DGRAM
3 constant SOCK_RAW 3 constant SOCK_RAW
2 constant AF_INET 2 constant AF_INET
16 constant sizeof(sockaddr_in) 16 constant sizeof(sockaddr_in)
1 constant SOL_SOCKET 1 constant SOL_SOCKET
2 constant SO_REUSEADDR 2 constant SO_REUSEADDR
: bs, ( n -- ) dup 256 / c, c, ;
: s, ( n -- ) dup c, 256 / c, ; : bs, ( n -- ) dup 8 rshift c, c, ;
: l, ( n -- ) dup s, 65536 / s, ; : 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, ; : 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@ ( a -- n ) 2 + >r r@ c@ 8 lshift r> 1+ c@ + ;
: ->port! ( n a -- ) 2 + >r dup 256 / r@ c! r> 1+ c! ; : ->port! ( n a -- ) 2 + >r dup 8 rshift r@ c! r> 1+ c! ;
: ->addr@ ( a -- n ) 4 + ul@ ; : ->addr@ ( a -- n ) 4 + ul@ ;
: ->addr! ( n a -- ) 4 + l! ; : ->addr! ( n a -- ) 4 + l! ;
: ->h_addr ( hostent -- n ) 2 cells + 8 + @ @ ul@ ; : ->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 forth definitions
vocabulary interrupts interrupts definitions vocabulary interrupts interrupts definitions

View File

@ -16,12 +16,7 @@
:noname [ ' web-interface >body @ ] literal execute :noname [ ' web-interface >body @ ] literal execute
r| r|
also streams also WiFi also web-interface definitions also streams also WiFi also web-interface
: ip# dup 255 and n. [char] . emit 256 / ;
: ip. ( n -- ) ip# ip# ip# 255 and . ;
also forth definitions
: login ( z z -- ) : login ( z z -- )
WIFI_MODE_STA Wifi.mode WIFI_MODE_STA Wifi.mode

View File

@ -2,34 +2,35 @@
also sockets also sockets
also tasks also tasks
also posix
1024 constant max-msg 1024 constant max-msg
create msg max-msg allot create msg max-msg allot
variable len max-msg len !
-1 value sockfd -1 value sockfd
variable len max-msg len ! sockaddr incoming
sockaddr outgoing
sockaddr received
variable received-len sizeof(sockaddr_in) received-len !
: reader : reader
begin begin
sockfd msg len 0 0 0 recvfrom sockfd msg len 0 received received-len recvfrom
dup 0 >= if dup 0 >= if
msg swap type cr received ->addr@ ip. ." :" received ->port@ . space space msg swap type cr
else drop then else drop then
pause pause
again again
; ;
' reader 10 10 task reader-task ' reader 10 10 task reader-task
sockaddr incoming
sockaddr outgoing
: udp ( port -- ) : udp ( port -- )
incoming ->port! incoming ->port!
AF_INET SOCK_DGRAM 0 socket to sockfd AF_INET SOCK_DGRAM 0 socket to sockfd
sockfd non-block throw sockfd non-block throw
sockfd incoming sizeof(sockaddr_in) bind throw sockfd incoming sizeof(sockaddr_in) bind throw
reader-task start-task reader-task start-task
stdin non-block throw
; ;
: say ( port -- ) : say ( port -- )

View File

@ -42,15 +42,17 @@ z" gethostbyname" 1 sysfunc gethostbyname
1 constant SOL_SOCKET 1 constant SOL_SOCKET
2 constant SO_REUSEADDR 2 constant SO_REUSEADDR
: bs, ( n -- ) dup 256 / c, c, ; : bs, ( n -- ) dup 8 rshift c, c, ;
: s, ( n -- ) dup c, 256 / c, ; : s, ( n -- ) dup c, 8 rshift c, ;
: l, ( n -- ) dup s, 65536 / s, ; : l, ( n -- ) dup s, 16 rshift s, ;
: sockaddr create AF_INET s, 0 bs, 0 l, 0 l, 0 l, ; : 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@ ( a -- n ) 2 + >r r@ c@ 8 lshift r> 1+ c@ + ;
: ->port! ( n a -- ) 2 + >r dup 256 / r@ c! r> 1+ c! ; : ->port! ( n a -- ) 2 + >r dup 8 rshift r@ c! r> 1+ c! ;
: ->addr@ ( a -- n ) 4 + ul@ ; : ->addr@ ( a -- n ) 4 + ul@ ;
: ->addr! ( n a -- ) 4 + l! ; : ->addr! ( n a -- ) 4 + l! ;
: ->h_addr ( hostent -- n ) 2 cells + 8 + @ @ ul@ ; : ->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 ) ( Fixup return )
: sockaccept sockaccept sign-extend ; : sockaccept sockaccept sign-extend ;