-
-
Notifications
You must be signed in to change notification settings - Fork 415
/
echo.pony
52 lines (40 loc) · 1.26 KB
/
echo.pony
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use "net"
actor Main
new create(env: Env) =>
TCPListener(TCPListenAuth(env.root), Listener(env.out))
class Listener is TCPListenNotify
let _out: OutStream
var _host: String = ""
var _port: String = ""
new iso create(out: OutStream) =>
_out = out
fun ref listening(listen: TCPListener ref) =>
try
(_host, _port) = listen.local_address().name()?
_out.print("listening on " + _host + ":" + _port)
else
_out.print("couldn't get local address")
listen.close()
end
fun ref not_listening(listen: TCPListener ref) =>
_out.print("couldn't listen")
listen.close()
fun ref connected(listen: TCPListener ref): TCPConnectionNotify iso^ =>
Server(_out)
class Server is TCPConnectionNotify
let _out: OutStream
new iso create(out: OutStream) =>
_out = out
fun ref accepted(conn: TCPConnection ref) =>
_out.print("connection accepted")
fun ref received(conn: TCPConnection ref, data: Array[U8] iso,
times: USize): Bool
=>
_out.print("data received, looping it back")
conn.write("server says: ")
conn.write(consume data)
true
fun ref closed(conn: TCPConnection ref) =>
_out.print("server closed")
fun ref connect_failed(conn: TCPConnection ref) =>
_out.print("connect failed")