]> gitweb.factorcode.org Git - factor.git/commitdiff
io.sockets: Add more utility words for working with udp.
authorDoug Coleman <doug.coleman@gmail.com>
Sat, 20 Feb 2016 02:58:43 +0000 (18:58 -0800)
committerDoug Coleman <doug.coleman@gmail.com>
Sat, 20 Feb 2016 03:21:13 +0000 (19:21 -0800)
basis/io/sockets/sockets-docs.factor
basis/io/sockets/sockets.factor
extra/benchmark/tcp-echo0/tcp-echo0.factor
extra/dns/dns.factor
extra/ntp/ntp.factor
extra/wake-on-lan/wake-on-lan.factor

index d40991991bc615904e2eed125db0bb9580ef2702..7e3ec264e9c46350eba6d54621fe100ff2b3bb05 100644 (file)
@@ -216,15 +216,82 @@ HELP: <datagram>
 { $errors "Throws an error if the port is already in use, or if the OS forbids access." } ;
 
 HELP: receive
-{ $values { "datagram" "a datagram socket" } { "packet" byte-array } { "addrspec" "an address specifier" } }
+{ $values { "datagram" "a datagram socket" } { "bytes" byte-array } { "addrspec" "an address specifier" } }
 { $description "Waits for an incoming packet on the given datagram socket. Outputs the packet data, as well as the sender's address." }
 { $errors "Throws an error if the packet could not be received." } ;
 
 HELP: send
-{ $values { "packet" byte-array } { "addrspec" "an address specifier" } { "datagram" "a datagram socket" } }
+{ $values { "bytes" byte-array } { "addrspec" "an address specifier" } { "datagram" "a datagram socket" } }
 { $description "Sends a packet to the given address." }
 { $errors "Throws an error if the packet could not be sent." } ;
 
+HELP: send-once
+{ $values
+    { "bytes" byte-array } { "addrspec" "an address specifier" }
+}
+{ $examples
+    "Send a datagram to localhost, port 7777:"
+    { $example "USING: io.sockets prettyprint ;"
+        "B{ 1 2 3 } f 7777 <inet4> send-once"
+        ""
+    }
+}
+{ $description "Sends a packet one time to the address and closes the sending datagram port." } ;
+
+HELP: send-n-times
+{ $values
+    { "bytes" byte-array } { "addrspec" "an address specifier" } { "n" integer }
+}
+{ $examples
+    "Send a datagram 10 times to localhost, port 7777:"
+    { $example "USING: io.sockets prettyprint ;"
+        "B{ 1 2 3 } f 7777 <inet4> 10 send-n-times"
+        ""
+    }
+}
+{ $description "Sends a packet n times to the address and closes the sending datagram port." } ;
+
+HELP: broadcast-once
+{ $values
+    { "bytes" byte-array } { "addrspec" "an address specifier" }
+}
+{ $examples
+    "Send a datagram to localhost, port 7777:"
+    { $example "USING: io.sockets prettyprint ;"
+        "B{ 1 2 3 } f 7777 <inet4> broadcast-once"
+        ""
+    }
+}
+{ $description "Broadcasts a packet one time to the address and closes the sending broadcast port." } ;
+
+HELP: broadcast-n-times
+{ $values
+    { "bytes" byte-array } { "addrspec" "an address specifier" } { "n" integer }
+}
+{ $examples
+    "Broadcast a datagram 10 times to localhost, port 7777:"
+    { $example "USING: io.sockets prettyprint ;"
+        "B{ 1 2 3 } f 7777 <inet4> 10 broadcast-n-times"
+        ""
+    }
+}
+{ $description "Broadcasts a packet n times to the address and closes the sending broadcast port." } ;
+
+
+
+HELP: with-random-local-broadcast
+{ $values
+    { "quot" quotation }
+}
+{ $description "Creates a broadcast datagram socket and calls the quotation with this datagram on top of the stack, cleaning up afterwards." } ;
+
+HELP: with-random-local-datagram
+{ $values
+    { "quot" quotation }
+}
+{ $description "Creates a datagram socket and calls the quotation with this datagram on top of the stack, cleaning up afterwards." } ;
+
+
 HELP: resolve-host
 { $values { "addrspec" "an address specifier" } { "seq" "a sequence of address specifiers" } }
 { $description "Resolves host names to IP addresses." }
index ff01ecf037b1f3e852e14509d7e4535387a9ebfd..e786ad0bc961b6f3b9c420c8c5e77794c1c52cc3 100644 (file)
@@ -119,6 +119,8 @@ TUPLE: inet4 < ipv4 { port integer read-only } ;
 : <inet4> ( host port -- inet4 )
     over check-ipv4 inet4 boa ;
 
+: <random-local-inet4> ( -- inet4 ) f 0 <inet4> ;
+
 M: ipv4 with-port [ host>> ] dip <inet4> ;
 
 M: inet4 parse-sockaddr ( sockaddr-in addrspec -- newaddrspec )
@@ -288,20 +290,20 @@ HOOK: (receive-unsafe) io-backend ( n buf datagram -- count addrspec )
 
 ERROR: invalid-port object ;
 
-: check-port ( packet addrspec port -- packet addrspec port )
+: check-port ( bytes addrspec port -- bytes addrspec port )
     2dup addr>> [ class-of ] bi@ assert=
     pick class-of byte-array assert= ;
 
 : check-connectionless-port ( port -- port )
     dup { [ datagram-port? ] [ raw-port? ] } 1|| [ invalid-port ] unless ;
 
-: check-send ( packet addrspec port -- packet addrspec port )
+: check-send ( bytes addrspec port -- bytes addrspec port )
     check-connectionless-port check-disposed check-port ;
 
 : check-receive ( port -- port )
     check-connectionless-port check-disposed ;
 
-HOOK: (send) io-backend ( packet addrspec datagram -- )
+HOOK: (send) io-backend ( bytes addrspec datagram -- )
 
 : addrinfo>addrspec ( addrinfo -- addrspec )
     [ [ addr>> ] [ family>> ] bi sockaddr-of-family ]
@@ -375,7 +377,7 @@ SYMBOL: remote-address
 
 CONSTANT: datagram-size 65536
 
-:: receive ( datagram -- packet addrspec )
+:: receive ( datagram -- bytes addrspec )
     datagram-size (byte-array) :> buf
     datagram-size buf datagram
     receive-unsafe :> ( count addrspec )
@@ -386,9 +388,37 @@ CONSTANT: datagram-size 65536
     n buf datagram receive-unsafe :> ( count addrspec )
     buf count head-slice addrspec ; inline
 
-: send ( packet addrspec datagram -- )
+: send ( bytes addrspec datagram -- )
     check-send (send) ; inline
 
+: <random-local-datagram> ( -- datagram )
+    <random-local-inet4> <datagram> ;
+
+: <random-local-broadcast> ( -- datagram )
+    <random-local-inet4> <broadcast> ;
+
+: with-random-local-datagram ( quot -- )
+    [ <random-local-datagram> ] dip with-disposal ; inline
+
+: with-random-local-broadcast ( quot -- )
+    [ <random-local-broadcast> ] dip with-disposal ; inline
+
+: send-once ( bytes addrspec -- )
+    [ send ] with-random-local-datagram ;
+
+:: send-n-times ( bytes addrspec n -- )
+    [
+        n swap '[ bytes addrspec _ send ] times
+    ] with-random-local-datagram ;
+
+: broadcast-once ( bytes addrspec -- )
+    [ send ] with-random-local-broadcast ;
+
+:: broadcast-n-times ( bytes addrspec n -- )
+    [
+        n swap '[ bytes addrspec _ send ] times
+    ] with-random-local-broadcast ;
+
 MEMO: ipv6-supported? ( -- ? )
     [ "::1" 0 <inet6> binary <server> dispose t ] [ drop f ] recover ;
 
index 60500b3aa8de307f00aa3fda0a28524b0069c60a..c2bf0c03ff8f6a4f3c2e5f4a9366287aa0533e49 100644 (file)
@@ -16,7 +16,7 @@ TUPLE: tcp-echo < threaded-server #times #bytes ;
     binary \ tcp-echo new-threaded-server
         swap >>#bytes
         swap >>#times
-        f 0 <inet4> >>insecure ;
+        <random-local-inet4> >>insecure ;
 
 ERROR: incorrect-#bytes ;
 
index d8e3f6bdf77a58eb64ac1bdab830eabddd8445e3..011949a37bf8e5d02296d35f271e60e2ffa2ae0a 100644 (file)
@@ -325,10 +325,10 @@ M: TXT rdata>byte-array
     ] B{ } append-outputs-as ;
 
 : udp-query ( bytes server -- bytes' )
-    f 0 <inet4> <datagram>
-    10 seconds over set-timeout [
+    [
+        10 seconds over set-timeout
         [ send ] [ receive drop ] bi
-    ] with-disposal ;
+    ] with-random-local-datagram ;
 
 : <dns-inet4> ( -- inet4 )
     dns-servers get random 53 <inet4> ;
index eab1d794869ce1755d829538a10326c99b870aa0..ec42762a7076af153598edf2d8253a017bfb17e3 100644 (file)
@@ -103,10 +103,10 @@ PRIVATE>
 ! - why does <inet4> resolve-host not work?
 
 : <ntp> ( host -- ntp )
-    123 <inet> resolve-host [ inet4? ] filter random
-    f 0 <inet4> <datagram> [
+    123 <inet> resolve-host
+    [ inet4? ] filter random [
         [ REQUEST ] 2dip [ send ] [ receive drop ] bi (ntp)
-    ] with-disposal ;
+    ] with-random-local-datagram ;
 
 : default-ntp ( -- ntp )
     "pool.ntp.org" <ntp> ;
index 43082904d0d5ddc6ccc62f1c190201c3541b6e7b..fb04153debb89832b3b43d99b4c066f567d3c341 100644 (file)
@@ -11,7 +11,7 @@ IN: wake-on-lan
 : mac-address-bytes ( mac-address -- byte-array )
     ":-" split [ hex> ] B{ } map-as ;
 
-: wake-on-lan-packet ( mac-address -- bytearray )
+: wake-on-lan-packet ( mac-address -- byte-array )
     [ 16 ] [ mac-address-bytes ] bi* <array> concat
     B{ 0xff 0xff 0xff 0xff 0xff 0xff } prepend ;
 
@@ -19,4 +19,4 @@ PRIVATE>
 
 : wake-on-lan ( mac-address broadcast-ip -- )
     [ wake-on-lan-packet ] [ 9 <inet4> ] bi*
-    f 0 <inet4> <broadcast> [ send ] with-disposal ;
+    broadcast-once ;