-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.pony
53 lines (42 loc) · 1.36 KB
/
server.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
53
use "net"
class Listener is TCPListenNotify
let _out: OutStream
var _host: String = ""
var _port: String = ""
let _cm: ConnectionManager tag
new iso create(out: OutStream, cm: ConnectionManager tag) =>
_out = out
_cm = cm
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, _cm)
class Server is TCPConnectionNotify
let _out: OutStream
let _cm: ConnectionManager tag
new iso create(out: OutStream, cm: ConnectionManager tag) =>
_out = out
_cm = cm
fun ref accepted(conn: TCPConnection ref) =>
_out.print("connection accepted")
_cm.accepted(conn)
fun ref received(conn: TCPConnection ref, data: Array[U8] iso,
times: USize): Bool =>
let cmd = String.from_iso_array(consume data)
cmd.rstrip()
_cm.cmdreceived(conn, consume cmd)
true
fun ref closed(conn: TCPConnection ref) =>
_out.print("server closed")
_cm.closed(conn)
fun ref connect_failed(conn: TCPConnection ref) =>
_out.print("connect failed")