-
Notifications
You must be signed in to change notification settings - Fork 5
/
muxed-conn.go
84 lines (74 loc) · 2.25 KB
/
muxed-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
package srpc
import (
"context"
"io"
"net"
"github.com/libp2p/go-libp2p/core/network"
ymuxer "github.com/libp2p/go-libp2p/p2p/muxer/yamux"
yamux "github.com/libp2p/go-yamux/v4"
)
// NewYamuxConfig builds the default yamux configuration.
func NewYamuxConfig() *yamux.Config {
// Configuration options from go-libp2p-yamux:
config := *ymuxer.DefaultTransport.Config()
config.AcceptBacklog = 512
config.EnableKeepAlive = false
return &config
}
// NewMuxedConn constructs a new MuxedConn from a net.Conn.
//
// If yamuxConf is nil, uses defaults.
func NewMuxedConn(conn net.Conn, outbound bool, yamuxConf *yamux.Config) (network.MuxedConn, error) {
if yamuxConf == nil {
yamuxConf = NewYamuxConfig()
}
var sess *yamux.Session
var err error
if outbound {
sess, err = yamux.Client(conn, yamuxConf, nil)
} else {
sess, err = yamux.Server(conn, yamuxConf, nil)
}
if err != nil {
return nil, err
}
return ymuxer.NewMuxedConn(sess), nil
}
// NewMuxedConnWithRwc builds a new MuxedConn with a io.ReadWriteCloser.
//
// If yamuxConf is nil, uses defaults.
func NewMuxedConnWithRwc(
ctx context.Context,
rwc io.ReadWriteCloser,
outbound bool,
yamuxConf *yamux.Config,
) (network.MuxedConn, error) {
return NewMuxedConn(NewRwcConn(ctx, rwc, nil, nil, 10), outbound, yamuxConf)
}
// NewClientWithConn constructs the muxer and the client.
//
// if yamuxConf is nil, uses defaults.
func NewClientWithConn(conn net.Conn, outbound bool, yamuxConf *yamux.Config) (Client, error) {
mconn, err := NewMuxedConn(conn, outbound, yamuxConf)
if err != nil {
return nil, err
}
return NewClientWithMuxedConn(mconn), nil
}
// NewClientWithMuxedConn constructs a new client with a MuxedConn.
func NewClientWithMuxedConn(conn network.MuxedConn) Client {
openStreamFn := NewOpenStreamWithMuxedConn(conn)
return NewClient(openStreamFn)
}
// NewOpenStreamWithMuxedConn constructs a OpenStream func with a MuxedConn.
func NewOpenStreamWithMuxedConn(conn network.MuxedConn) OpenStreamFunc {
return func(ctx context.Context, msgHandler PacketDataHandler, closeHandler CloseHandler) (PacketWriter, error) {
mstrm, err := conn.OpenStream(ctx)
if err != nil {
return nil, err
}
rw := NewPacketReadWriter(mstrm)
go rw.ReadPump(msgHandler, closeHandler)
return rw, nil
}
}