lean4-htt/tests/lean/run/async_tcp_half.lean
Sofia Rodrigues d7d1754e69
feat: socket support using LibUV (#6683)
This PR introduces TCP socket support using the LibUV library, enabling
asynchronous I/O operations with it.

---------

Co-authored-by: Henrik Böving <hargonix@gmail.com>
Co-authored-by: Markus Himmel <markus@himmel-villmar.de>
2025-03-19 13:54:51 +00:00

66 lines
1.5 KiB
Text
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Std.Internal.Async
import Std.Internal.UV
import Std.Net.Addr
open Std.Internal.IO.Async
open Std.Net
-- Define the Async monad
structure Async (α : Type) where
run : IO (AsyncTask α)
namespace Async
-- Monad instance for Async
instance : Monad Async where
pure x := Async.mk (pure (AsyncTask.pure x))
bind ma f := Async.mk do
let task ← ma.run
task.bindIO fun a => (f a).run
-- Await function to simplify AsyncTask handling
def await (task : IO (AsyncTask α)) : Async α :=
Async.mk task
instance : MonadLift IO Async where
monadLift io := Async.mk (io >>= (pure ∘ AsyncTask.pure))
/-- Joe is another client. -/
def runJoe (addr: SocketAddress) : Async Unit := do
let client ← TCP.Socket.Client.mk
await (client.connect addr)
await (client.send (String.toUTF8 "hello robert!"))
await client.shutdown
def listenClose : IO Unit := do
let addr := SocketAddressV4.mk (.ofParts 127 0 0 1) 8080
let server ← TCP.Socket.Server.mk
server.bind addr
server.listen 128
def acceptClose : IO Unit := do
let addr := SocketAddressV4.mk (.ofParts 127 0 0 1) 8082
let server ← TCP.Socket.Server.mk
server.bind addr
server.listen 128
let _ ← (runJoe addr).run
let task ← server.accept
let client ← task.block
let mes ← client.recv? 1024
let msg ← mes.block
assert! (String.fromUTF8! <$> msg) == "hello robert!"
let mes ← client.recv? 1024
let msg ← mes.block
assert! (String.fromUTF8! <$> msg) == none
#eval acceptClose
#eval listenClose