-
Notifications
You must be signed in to change notification settings - Fork 661
/
reader_client.go
43 lines (33 loc) · 992 Bytes
/
reader_client.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
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package greadcloser
import (
"context"
"errors"
"io"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/ghttp/greadcloser/greadcloserproto"
)
var _ io.ReadCloser = &Client{}
// Client is a read closer that talks over RPC.
type Client struct{ client greadcloserproto.ReaderClient }
// NewClient returns a read closer connected to a remote read closer
func NewClient(client greadcloserproto.ReaderClient) *Client {
return &Client{client: client}
}
func (c *Client) Read(p []byte) (int, error) {
resp, err := c.client.Read(context.Background(), &greadcloserproto.ReadRequest{
Length: int32(len(p)),
})
if err != nil {
return 0, err
}
copy(p, resp.Read)
if resp.Errored {
err = errors.New(resp.Error)
}
return len(resp.Read), err
}
func (c *Client) Close() error {
_, err := c.client.Close(context.Background(), &greadcloserproto.CloseRequest{})
return err
}