aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/world/net/udp.lux
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib/source/lux/world/net/udp.lux')
-rw-r--r--stdlib/source/lux/world/net/udp.lux123
1 files changed, 0 insertions, 123 deletions
diff --git a/stdlib/source/lux/world/net/udp.lux b/stdlib/source/lux/world/net/udp.lux
deleted file mode 100644
index 44a1354cc..000000000
--- a/stdlib/source/lux/world/net/udp.lux
+++ /dev/null
@@ -1,123 +0,0 @@
-(.module:
- [lux #*
- [host (#+ import:)]
- ["@" target]
- [abstract
- monad]
- [control
- ["ex" exception (#+ exception:)]
- ["." io (#+ IO)]
- [concurrency
- ["." promise (#+ Promise)]]
- [security
- ["!" capability]]]
- [data
- ["." error (#+ Error)]
- ["." maybe]
- [collection
- ["." array]]]
- [world
- ["." binary (#+ Binary)]]]
- ["." // (#+ Location Can-Read Can-Write Can-Close)])
-
-(exception: #export (cannot-resolve-address {address //.Address})
- (ex.report ["Address" address]))
-
-(exception: #export (multiple-candidate-addresses {address //.Address})
- (ex.report ["Address" address]))
-
-(signature: #export (UDP !)
- (: (Can-Read ! [Nat Location Binary])
- read)
-
- (: (Can-Write ! [Location Binary])
- write)
-
- (: (Can-Close !)
- close))
-
-(def: #export (async udp)
- (-> (UDP IO) (UDP Promise))
- (`` (structure (~~ (template [<name> <forge>]
- [(def: <name> (<forge> (|>> (!.use (:: udp <name>)) promise.future)))]
-
- [read //.can-read]
- [write //.can-write]
- [close //.can-close])))))
-
-(with-expansions [<for-jvm> (as-is (import: #long java/lang/String)
-
- (import: #long java/lang/AutoCloseable
- (close [] #io #try void))
-
- (import: #long java/net/InetAddress
- (#static getAllByName [java/lang/String] #io #try [java/net/InetAddress])
- (getHostAddress [] java/lang/String))
-
- (import: #long java/net/DatagramPacket
- (new #as new|send [[byte] int int java/net/InetAddress int])
- (new #as new|receive [[byte] int int])
- (getAddress [] java/net/InetAddress)
- (getPort [] int)
- (getLength [] int))
-
- (import: #long java/net/DatagramSocket
- (new #as new|client [] #io #try)
- (new #as new|server [int] #io #try)
- (receive [java/net/DatagramPacket] #io #try void)
- (send [java/net/DatagramPacket] #io #try void))
-
- (def: (resolve address)
- (-> //.Address (IO (Error java/net/InetAddress)))
- (do (error.with io.monad)
- [addresses (java/net/InetAddress::getAllByName address)]
- (: (IO (Error java/net/InetAddress))
- (case (array.size addresses)
- 0 (io.io (ex.throw cannot-resolve-address address))
- 1 (wrap (maybe.assume (array.read 0 addresses)))
- _ (io.io (ex.throw multiple-candidate-addresses address))))))
-
- (def: (udp socket)
- (-> java/net/DatagramSocket (UDP IO))
- (structure (def: read
- (//.can-read
- (function (read size)
- (let [data (binary.create size)
- packet (java/net/DatagramPacket::new|receive data +0 (.int size))]
- (do (error.with io.monad)
- [_ (java/net/DatagramSocket::receive packet socket)
- #let [bytes-read (.nat (java/net/DatagramPacket::getLength packet))]]
- (wrap [bytes-read
- {#//.address (|> packet java/net/DatagramPacket::getAddress java/net/InetAddress::getHostAddress)
- #//.port (.nat (java/net/DatagramPacket::getPort packet))}
- data]))))))
-
- (def: write
- (//.can-write
- (function (write [location data])
- (do (error.with io.monad)
- [address (resolve (get@ #//.address location))]
- (java/net/DatagramSocket::send (java/net/DatagramPacket::new|send data +0 (.int (binary.size data)) address (.int (get@ #//.port location)))
- socket)))))
-
- (def: close
- (//.can-close
- (function (close _)
- (java/lang/AutoCloseable::close socket))))))
-
- (def: #export client
- (IO (Error (UDP IO)))
- (|> (java/net/DatagramSocket::new|client)
- (:: (error.with io.monad) map ..udp)))
-
- (def: #export server
- (-> //.Port (IO (Error (UDP IO))))
- (|>> .int
- java/net/DatagramSocket::new|server
- (:: (error.with io.monad) map ..udp)))
- )]
- (`` (for {(~~ (static @.old))
- (as-is <for-jvm>)
-
- (~~ (static @.jvm))
- (as-is <for-jvm>)})))