-
Notifications
You must be signed in to change notification settings - Fork 5
/
writer.go
47 lines (40 loc) · 1021 Bytes
/
writer.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
package rpcstream
import "github.com/aperturerobotics/starpc/srpc"
// RpcStreamWriter implements the Writer only.
type RpcStreamWriter struct {
RpcStream
}
// NewRpcStreamWriter constructs a new rpc stream writer.
func NewRpcStreamWriter(rpcStream RpcStream) *RpcStreamWriter {
return &RpcStreamWriter{RpcStream: rpcStream}
}
// Write writes a packet to the writer.
func (r *RpcStreamWriter) Write(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
err = r.RpcStream.Send(&RpcStreamPacket{
Body: &RpcStreamPacket_Data{
Data: p,
},
})
if err != nil {
return 0, err
}
return len(p), nil
}
// WritePacket writes a packet to the remote.
func (r *RpcStreamWriter) WritePacket(p *srpc.Packet) error {
pktData, err := p.MarshalVT()
if err != nil {
return err
}
_, err = r.Write(pktData)
return err
}
// Close closes the writer.
func (r *RpcStreamWriter) Close() error {
return r.RpcStream.CloseSend()
}
// _ is a type assertion
var _ srpc.PacketWriter = ((*RpcStreamWriter)(nil))