/
rpc.go
73 lines (59 loc) · 1.4 KB
/
rpc.go
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package rpc
import (
"context"
"io"
"net"
"strings"
"github.com/sourcegraph/jsonrpc2"
)
type HandlerFunc func(context.Context, *jsonrpc2.Conn, *jsonrpc2.Request)
func (h HandlerFunc) Handle(ctx context.Context, c *jsonrpc2.Conn, r *jsonrpc2.Request) {
h(ctx, c, r)
}
type CustomStream struct {
io.ReadCloser
io.WriteCloser
}
func (conn *CustomStream) Read(p []byte) (n int, err error) {
return conn.ReadCloser.Read(p)
}
func (conn *CustomStream) Write(p []byte) (n int, err error) {
return conn.WriteCloser.Write(p)
}
func (conn *CustomStream) Close() error {
if err := conn.ReadCloser.Close(); err != nil {
return err
} else if err := conn.WriteCloser.Close(); err != nil {
return err
}
return nil
}
func StartServer(addr string, codec jsonrpc2.ObjectCodec, h jsonrpc2.Handler) error {
if strings.HasPrefix(addr, ":") {
// Prepend 127.0.0.1 to the address. This is for us to be able to
// run the server without firewall prompts. (for Windows)
// https://stackoverflow.com/a/66486551
addr = "127.0.0.1" + addr
}
l, err := net.Listen("tcp", addr)
if err != nil {
return err
}
defer l.Close()
asyncH := jsonrpc2.AsyncHandler(h)
for {
conn, err := l.Accept()
if err != nil {
return err
}
go func() {
cn := jsonrpc2.NewConn(
context.Background(),
jsonrpc2.NewBufferedStream(conn, codec),
asyncH,
)
defer cn.Close()
<-cn.DisconnectNotify()
}()
}
}