-
Notifications
You must be signed in to change notification settings - Fork 671
/
conn_server.go
88 lines (75 loc) · 2.24 KB
/
conn_server.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package gconn
import (
"context"
"net"
"time"
"google.golang.org/protobuf/types/known/emptypb"
"github.com/ava-labs/avalanchego/api/proto/gconnproto"
"github.com/ava-labs/avalanchego/vms/rpcchainvm/grpcutils"
)
var _ gconnproto.ConnServer = &Server{}
// Server is an http.Conn that is managed over RPC.
type Server struct {
gconnproto.UnimplementedConnServer
conn net.Conn
closer *grpcutils.ServerCloser
}
// NewServer returns an http.Conn managed remotely
func NewServer(conn net.Conn, closer *grpcutils.ServerCloser) *Server {
return &Server{
conn: conn,
closer: closer,
}
}
func (s *Server) Read(ctx context.Context, req *gconnproto.ReadRequest) (*gconnproto.ReadResponse, error) {
buf := make([]byte, int(req.Length))
n, err := s.conn.Read(buf)
resp := &gconnproto.ReadResponse{
Read: buf[:n],
}
if err != nil {
resp.Errored = true
resp.Error = err.Error()
}
return resp, nil
}
func (s *Server) Write(ctx context.Context, req *gconnproto.WriteRequest) (*gconnproto.WriteResponse, error) {
n, err := s.conn.Write(req.Payload)
if err != nil {
return nil, err
}
return &gconnproto.WriteResponse{
Length: int32(n),
}, nil
}
func (s *Server) Close(ctx context.Context, req *emptypb.Empty) (*emptypb.Empty, error) {
err := s.conn.Close()
s.closer.Stop()
return &emptypb.Empty{}, err
}
func (s *Server) SetDeadline(ctx context.Context, req *gconnproto.SetDeadlineRequest) (*emptypb.Empty, error) {
deadline := time.Time{}
err := deadline.UnmarshalBinary(req.Time)
if err != nil {
return nil, err
}
return &emptypb.Empty{}, s.conn.SetDeadline(deadline)
}
func (s *Server) SetReadDeadline(ctx context.Context, req *gconnproto.SetDeadlineRequest) (*emptypb.Empty, error) {
deadline := time.Time{}
err := deadline.UnmarshalBinary(req.Time)
if err != nil {
return nil, err
}
return &emptypb.Empty{}, s.conn.SetReadDeadline(deadline)
}
func (s *Server) SetWriteDeadline(ctx context.Context, req *gconnproto.SetDeadlineRequest) (*emptypb.Empty, error) {
deadline := time.Time{}
err := deadline.UnmarshalBinary(req.Time)
if err != nil {
return nil, err
}
return &emptypb.Empty{}, s.conn.SetWriteDeadline(deadline)
}