-
Notifications
You must be signed in to change notification settings - Fork 8
/
inproc.go
163 lines (146 loc) · 3.83 KB
/
inproc.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package inproc
import (
"context"
"net"
"sync"
"github.com/aperturerobotics/bifrost/peer"
"github.com/aperturerobotics/bifrost/transport"
"github.com/aperturerobotics/bifrost/transport/common/dialer"
"github.com/aperturerobotics/bifrost/transport/common/pconn"
transport_controller "github.com/aperturerobotics/bifrost/transport/controller"
"github.com/aperturerobotics/controllerbus/bus"
"github.com/aperturerobotics/controllerbus/controller"
"github.com/blang/semver/v4"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/sirupsen/logrus"
)
// TransportType is the transport type string for dial addresses.
const TransportType = "inproc"
// ControllerID is the controller identifier.
const ControllerID = "bifrost/inproc"
// Version is the version of the inproc implementation.
var Version = semver.MustParse("0.0.1")
// Inproc implements a Inproc transport.
type Inproc struct {
// Transport is the packet transport
*pconn.Transport
// le is the logger
le *logrus.Entry
// packetConn is the packet conn
packetConn *packetConn
// localAddr is the local addr
localAddr net.Addr
// mtx guards below
mtx sync.Mutex
// remotes are the currently known remotes
// map is from string (net.addr.String()) to *packetConn
remotes map[string]*packetConn
}
// NewInproc builds a new Inproc transport.
// Yields Links to other Inproc transports.
func NewInproc(
ctx context.Context,
le *logrus.Entry,
opts *Config,
pKey crypto.PrivKey,
c transport.TransportHandler,
) (transport.Transport, error) {
peerID, err := peer.IDFromPrivateKey(pKey)
if err != nil {
return nil, err
}
localAddr := NewAddr(peerID)
ip := &Inproc{
le: le,
localAddr: localAddr,
remotes: make(map[string]*packetConn),
}
npc := newPacketConn(
ctx,
localAddr,
ip.writeToAddr,
)
ip.Transport, err = pconn.NewTransport(
ctx,
le,
pKey,
c,
opts.GetPacketOpts(),
0,
npc,
ParseAddr,
opts.GetDialers(),
)
if err != nil {
return nil, err
}
ip.packetConn = npc
return ip, nil
}
// BuildInprocController constructs the in-proc transport controller.
func BuildInprocController(
le *logrus.Entry,
b bus.Bus,
peerIDConstraint peer.ID,
conf *Config,
) *transport_controller.Controller {
return transport_controller.NewController(
le,
b,
controller.NewInfo(ControllerID, Version, "in-proc transport"),
peerIDConstraint,
func(
ctx context.Context,
le *logrus.Entry,
pkey crypto.PrivKey,
handler transport.TransportHandler,
) (transport.Transport, error) {
return NewInproc(
ctx,
le,
conf,
pkey,
handler,
)
},
)
}
// MatchTransportType checks if the given transport type ID matches this transport.
// If returns true, the transport controller will call DialPeer with that tptaddr.
// E.x.: "udp-quic" or "ws"
func (t *Inproc) MatchTransportType(transportType string) bool {
return transportType == TransportType
}
// ConnectToInproc connects the inproc to a remote inproc.
// Will overwrite any existing connection
func (t *Inproc) ConnectToInproc(ctx context.Context, other *Inproc) {
oa := other.localAddr.String()
t.mtx.Lock()
t.remotes[oa] = other.packetConn
t.mtx.Unlock()
}
// DisconnectInproc disconnects a previously connected inproc.
func (t *Inproc) DisconnectInproc(ctx context.Context, other *Inproc) {
oa := other.localAddr.String()
t.mtx.Lock()
delete(t.remotes, oa)
t.mtx.Unlock()
}
// writeToAddr routes outgoing packets.
func (t *Inproc) writeToAddr(ctx context.Context, p []byte, addr net.Addr) (int, error) {
oa := addr.String()
t.mtx.Lock()
out, outOk := t.remotes[oa]
t.mtx.Unlock()
if !outOk {
return 0, &net.AddrError{
Addr: oa,
Err: "remote transport not connected",
}
}
return out.HandlePacket(ctx, p, t.localAddr)
}
// _ is a type assertion.
var _ transport.Transport = ((*Inproc)(nil))
// _ is a type assertion.
var _ dialer.TransportDialer = ((*Inproc)(nil))