-
Notifications
You must be signed in to change notification settings - Fork 3
/
transport.go
66 lines (50 loc) · 1.22 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package transport
import (
"errors"
"fmt"
"net"
)
type transport struct {
trMap map[Protocol]genTransport
}
type genTransport func(addr string) (net.Listener, error)
var Transport = &transport{
trMap: map[Protocol]genTransport{},
}
func (t *transport) register(trType Protocol, genTr genTransport) {
t.trMap[trType] = genTr
}
func (t *transport) Gen(trType Protocol, addr string) (net.Listener, error) {
gFn, ex := t.trMap[trType]
if !ex {
return nil, errors.New(fmt.Sprintf("%s not funod", trType))
}
return gFn(addr)
}
type transportClient struct {
trMap map[Protocol]genTransportClient
}
type genTransportClient func(addr string) (net.Conn, error)
var Client = &transportClient{
trMap: map[Protocol]genTransportClient{},
}
func (t *transportClient) register(trType Protocol, genTr genTransportClient) {
t.trMap[trType] = genTr
}
func (t *transportClient) Gen(trType Protocol, addr string) (net.Conn, error) {
gFn, ex := t.trMap[trType]
if !ex {
return nil, errors.New(fmt.Sprintf("%s not funod", trType))
}
return gFn(addr)
}
type Protocol string
const (
TCP Protocol = "tcp"
KCP Protocol = "kcp"
MQTT Protocol = "mqtt"
UNIX Protocol = "unix"
)
func (p Protocol) String() string {
return string(p)
}