-
Notifications
You must be signed in to change notification settings - Fork 10
/
rpc_conn.go
118 lines (100 loc) · 2.8 KB
/
rpc_conn.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package stream_api_rpc
import (
"io"
"net"
"time"
"github.com/aperturerobotics/bifrost/peer"
)
// NetConn wraps an RPC into a net.Conn compat interface.
type NetConn struct {
rpc RPC
localPeerID peer.ID
remotePeerID peer.ID
}
// NewNetConn constructs a new NetConn.
func NewNetConn(localPeerID, remotePeerID peer.ID, rpc RPC) *NetConn {
return &NetConn{
rpc: rpc,
localPeerID: localPeerID,
remotePeerID: remotePeerID,
}
}
// Read reads data from the connection.
// Read can be made to time out and return an Error with Timeout() == true
// after a fixed time limit; see SetDeadline and SetReadDeadline.
func (n *NetConn) Read(b []byte) (int, error) {
for {
data, err := n.rpc.Recv()
if err != nil {
return 0, err
}
buf := data.Data
if len(buf) == 0 {
continue
}
copy(b, buf)
if len(buf) > len(b) {
return len(buf), io.ErrShortBuffer
}
return len(buf), nil
}
}
// Write writes data to the connection.
// Write can be made to time out and return an Error with Timeout() == true
// after a fixed time limit; see SetDeadline and SetWriteDeadline.
func (n *NetConn) Write(b []byte) (nw int, err error) {
nw = len(b)
err = n.rpc.Send(&Data{
Data: b,
})
return
}
// Close closes the connection.
// Any blocked Read or Write operations will be unblocked and return errors.
func (n *NetConn) Close() error {
// TODO
return nil
}
// LocalAddr returns the local network address.
func (n *NetConn) LocalAddr() net.Addr {
if n.localPeerID == "" {
return &net.UDPAddr{IP: net.IP{127, 0, 0, 1}, Port: 2}
}
return peer.NewNetAddr(n.localPeerID)
}
// RemoteAddr returns the remote network address.
func (n *NetConn) RemoteAddr() net.Addr {
if n.remotePeerID == "" {
return &net.UDPAddr{IP: net.IP{127, 0, 0, 1}, Port: 3}
}
return peer.NewNetAddr(n.remotePeerID)
}
// SetDeadline sets the read and write deadlines associated
// with the connection. It is equivalent to calling both
// SetReadDeadline and SetWriteDeadline.
//
// A zero value for t means I/O operations will not time out.
// Deadlines are stubbed here.
func (n *NetConn) SetDeadline(t time.Time) error {
_ = n.SetReadDeadline(t)
_ = n.SetWriteDeadline(t)
return nil
}
// SetReadDeadline sets the deadline for future Read calls
// and any currently-blocked Read call.
// A zero value for t means Read will not time out.
func (n *NetConn) SetReadDeadline(t time.Time) error {
// TODO: stub
return nil
}
// SetWriteDeadline sets the deadline for future Write calls
// and any currently-blocked Write call.
// Even if write times out, it may return n > 0, indicating that
// some of the data was successfully written.
// A zero value for t means Write will not time out.
func (n *NetConn) SetWriteDeadline(t time.Time) error {
// TODO: stub
return nil
}
// _ is a type assertion
var _ net.Conn = ((*NetConn)(nil))