...
1use "net"
2use "files"
3
4class ClientSide is TCPConnectionNotify
5 let _env: Env
6
7 new iso create(env: Env) =>
8 _env = env
9
10 fun ref connecting(conn: TCPConnection ref, count: U32) =>
11 _env.out.print("connecting: " + count.string())
12
13 fun ref connected(conn: TCPConnection ref) =>
14 try
15 (let host, let service) = conn.remote_address().name()?
16 _env.out.print("connected to " + host + ":" + service)
17 conn.set_nodelay(true)
18 conn.set_keepalive(10)
19 conn.write("client says hi")
20 end
21
22class Listener is TCPListenNotify
23 let _env: Env
24 let _limit: USize
25 var _host: String = ""
26 var _count: USize = 0
27
28 new create(env: Env, limit: USize) =>
29 _env = env
30 _limit = limit
31
32 fun ref connected(listen: TCPListener ref): TCPConnectionNotify iso^ =>
33 let env = _env
34
35 _env.out.print("Server starting")
36
37 let server = ServerSide(env)
38
39 _spawn(listen)
40 server
41
42 fun ref _spawn(listen: TCPListener ref) =>
43 if (_limit > 0) and (_count >= _limit) then
44 listen.dispose()
45 return
46 end
47
48 _count = _count + 1
49 _env.out.print("spawn " + _count.string())
50
51 try
52 let env = _env
53
54 _env.out.print("Client starting")
55 TCPConnection(
56 _env.root as AmbientAuth,
57 ClientSide(env),
58 _host,
59 _service)
60 else
61 _env.out.print("couldn't create client side")
62 listen.close()
63 end
64
65actor Main
66 new create(env: Env) =>
67 let limit = try
68 env.args(1)?.usize()?
69 else
70 1
71 end
72
73 try
74 let auth = env.root as AmbientAuth
75 TCPListener(auth, recover Listener(env, limit) end)
76 UDPSocket(auth, recover Pong(env) end)
77 else
78 env.out.print("unable to use the network")
79 end
80 be test() =>
81 nonsensical.stuff.here()
82
View as plain text