Newer
Older
import (
"bufio"
"context"
"encoding/json"
"io"
"gfx.cafe/open/jrpc/pkg/codec"
)
type Codec struct {
ctx context.Context
cn func()
rd io.Reader
wr *bufio.Writer
msgs chan json.RawMessage
}
func NewCodec(rd io.Reader, wr io.Writer) *Codec {
ctx, cn := context.WithCancel(context.TODO())
ctx: ctx,
cn: cn,
rd: bufio.NewReader(rd),
wr: bufio.NewWriter(wr),
msgs: make(chan json.RawMessage, 8),
}
go c.listen()
return c
}
func (c *Codec) listen() {
var msg json.RawMessage
for {
err := json.NewDecoder(c.rd).Decode(&msg)
if err != nil {
c.cn()
return
}
c.msgs <- msg
}
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
}
// gets the peer info
func (c *Codec) PeerInfo() codec.PeerInfo {
return codec.PeerInfo{
Transport: "ipc",
RemoteAddr: "",
HTTP: codec.HttpInfo{},
}
}
// json.RawMessage can be an array of requests. if it is, then it is a batch request
func (c *Codec) ReadBatch(ctx context.Context) (msgs json.RawMessage, err error) {
select {
case ans := <-c.msgs:
return ans, nil
case <-ctx.Done():
return nil, ctx.Err()
case <-c.ctx.Done():
return nil, c.ctx.Err()
}
}
// closes the connection
func (c *Codec) Close() error {
c.cn()
return nil
}
func (c *Codec) Write(p []byte) (n int, err error) {
return c.wr.Write(p)
}
func (c *Codec) Flush() (err error) {
return c.wr.Flush()
}
// Closed returns a channel which is closed when the connection is closed.
func (c *Codec) Closed() <-chan struct{} {
return c.ctx.Done()
}
// RemoteAddr returns the peer address of the connection.
func (c *Codec) RemoteAddr() string {
return ""
}
// Dialrdwr attaches an in-process connection to the given RPC server.
//func Dialrdwr(handler *Server) *Client {