-
Notifications
You must be signed in to change notification settings - Fork 6
/
tcp_example.v
45 lines (36 loc) · 929 Bytes
/
tcp_example.v
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
module main
import jsonrpc
import net
fn send_error_tcp(err_code int, conn net.Socket) {
mut eres := Response{}
eres.send_error(err_code)
send_tcp_resp(&eres, conn)
}
fn handle_tcp(srv jsonrpc.Server, con net.Socket) {
s := con.read_line()
defer { con.close() or { } }
res := srv.exec(s) or {
err_code := err.int()
send_error_tcp(err_code, conn)
return
}
con.send_string(res.gen_resp_text()) or { }
}
pub fn main() {
port_num := 8000
server := net.listen(port_num) or { panic('Failed to listen to port ${port_num}') }
println('JSON-RPC Server has started on port ${port_num}')
mut srv := jsonrpc.new()
srv.register('greet', fn (ctx mut Context) string {
name := jsonrpc.as_string(ctx.req.params)
return 'Hello, $name'
})
for {
conn := server.accept() or {
send_error_tcp(SERVER_ERROR_START, conn)
server.close() or { }
panic(err)
}
go handle_tcp(conn)
}
}