-
Notifications
You must be signed in to change notification settings - Fork 388
/
conn.go
110 lines (87 loc) · 2.81 KB
/
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
package mc
import (
"context"
"fmt"
"io"
"net"
"time"
mcdrv "berty.tech/berty/v2/go/internal/multipeer-connectivity-transport/driver"
mcma "berty.tech/berty/v2/go/internal/multipeer-connectivity-transport/multiaddr"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr-net"
"github.com/pkg/errors"
)
// Conn is a manet.Conn.
var _ manet.Conn = &Conn{}
// Conn is the equivalent of a net.Conn object. It is the
// result of calling the Dial or Listen functions in this
// package, with associated local and remote Multiaddrs.
type Conn struct {
readIn *io.PipeWriter
readOut *io.PipeReader
localMa ma.Multiaddr
remoteMa ma.Multiaddr
ctx context.Context
cancel func()
}
// Read reads data from the connection.
// Timeout handled by the native driver.
func (c *Conn) Read(payload []byte) (n int, err error) {
if c.ctx.Err() != nil {
return 0, fmt.Errorf("conn read failed: conn already closed")
}
n, err = c.readOut.Read(payload)
if err != nil {
err = errors.Wrap(err, "conn read failed")
}
return n, err
}
// Write writes data to the connection.
// Timeout handled by the native driver.
func (c *Conn) Write(payload []byte) (n int, err error) {
if c.ctx.Err() != nil {
return 0, fmt.Errorf("conn write failed: conn already closed")
}
// Write to the peer's device using native driver.
if !mcdrv.SendToPeer(c.RemoteAddr().String(), payload) {
return 0, fmt.Errorf("conn write failed: native write failed")
}
return len(payload), nil
}
// Close closes the connection.
// Any blocked Read or Write operations will be unblocked and return errors.
func (c *Conn) Close() error {
c.cancel()
// Closes read pipe
c.readIn.Close()
c.readOut.Close()
// Removes conn from connmgr's connMap
connMap.Delete(c.RemoteAddr().String())
// Notify the native driver that the conn was cloed with this peer.
mcdrv.CloseConnWithPeer(c.RemoteAddr().String())
return nil
}
// LocalAddr returns the local network address.
func (c *Conn) LocalAddr() net.Addr {
lAddr, _ := c.LocalMultiaddr().ValueForProtocol(mcma.P_MC)
return &Addr{
Address: lAddr,
}
}
// LocalAddr returns the remote network address.
func (c *Conn) RemoteAddr() net.Addr {
rAddr, _ := c.RemoteMultiaddr().ValueForProtocol(mcma.P_MC)
return &Addr{
Address: rAddr,
}
}
// LocalMultiaddr returns the local Multiaddr associated
// with this connection.
func (c *Conn) LocalMultiaddr() ma.Multiaddr { return c.localMa }
// RemoteMultiaddr returns the remote Multiaddr associated
// with this connection.
func (c *Conn) RemoteMultiaddr() ma.Multiaddr { return c.remoteMa }
// Noop deadline methods, handled by the native driver.
func (c *Conn) SetDeadline(t time.Time) error { return nil }
func (c *Conn) SetReadDeadline(t time.Time) error { return nil }
func (c *Conn) SetWriteDeadline(t time.Time) error { return nil }