-
Notifications
You must be signed in to change notification settings - Fork 1
/
transport.go
50 lines (44 loc) · 1.05 KB
/
transport.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
package tentacle
import (
"github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr/net"
)
type transport interface {
listen(multiaddr.Multiaddr) (manet.Listener, error)
dial(multiaddr.Multiaddr) (manet.Conn, error)
}
func multiListen(addr multiaddr.Multiaddr, config serviceConfig) (manet.Listener, error) {
switch findTy(addr) {
case tcp:
return newTCPTransport(config.timeout, config.tcpBind).listen(addr)
case ws:
return newWSTransport(config.timeout, config.wsBind).listen(addr)
default:
return nil, ErrNotSupport
}
}
func multiDial(addr multiaddr.Multiaddr, config serviceConfig) (manet.Conn, error) {
switch findTy(addr) {
case tcp:
return newTCPTransport(config.timeout, config.tcpBind).dial(addr)
case ws:
return newWSTransport(config.timeout, config.wsBind).dial(addr)
default:
return nil, ErrNotSupport
}
}
const (
tcp int8 = iota
ws
)
func findTy(addr multiaddr.Multiaddr) int8 {
var ty int8 = tcp
ps := addr.Protocols()
for _, p := range ps {
switch p.Name {
case "ws":
ty = ws
}
}
return ty
}