Skip to content

Instantly share code, notes, and snippets.

@anacrolix
Created June 29, 2020 04:02
Show Gist options
  • Save anacrolix/dea5407822c317abb09c40416c0d2368 to your computer and use it in GitHub Desktop.
Save anacrolix/dea5407822c317abb09c40416c0d2368 to your computer and use it in GitHub Desktop.
use "net"
actor Main
new create(env: Env) =>
let auth =
try
env.root as AmbientAuth
else
env.err.print("couldn't get auth")
env.exitcode(1)
return
end
Test(env, auth)
actor Test
let _env: Env
let _sock: UDPSocket
new create(env: Env, auth: AmbientAuth) =>
_env = env
_sock = UDPSocket(
auth,
recover MyUDPNotify(this~print_net_address()) end
where host="0.0.0.0")
be print_net_address(event: String, na: NetAddress) =>
_env.out.print("got event " + event + " and " +
try
(let host, let service) = na.name()?
"address " + host + ":" + service
else
"an error from the address name"
end
)
close_socket()
be close_socket() =>
_sock.dispose()
type MyUDPNotifyCallback is {(String, NetAddress)}
class MyUDPNotify
let callback: MyUDPNotifyCallback
new create(callback': MyUDPNotifyCallback) =>
callback = callback'
fun not_listening(sock: UDPSocket ref) =>
callback("not_listening", sock.local_address())
fun listening(sock: UDPSocket ref) =>
callback("listening", sock.local_address())
fun received(sock: UDPSocket ref, _: Array[U8], _: NetAddress) =>
callback("received", sock.local_address())
fun closed(sock: UDPSocket ref) =>
callback("closed", sock.local_address())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment