forked from zeromq/gomq
/
socket.go
159 lines (125 loc) · 3.19 KB
/
socket.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
package gomq
import (
"errors"
"net"
"strings"
"sync"
"time"
"github.com/zeromq/gomq/zmtp"
)
var (
ClientSocketType = zmtp.ClientSocketType
ServerSocketType = zmtp.ServerSocketType
NullSecurityMechanismType = zmtp.NullSecurityMechanismType
PlainSecurityMechanismType = zmtp.PlainSecurityMechanismType
CurveSecurityMechanismTyp = zmtp.CurveSecurityMechanismType
ErrNotImplemented = errors.New("not implemented")
ErrInvalidSockAction = errors.New("action not valid on this socket")
defaultRetry = 250 * time.Millisecond
)
type Connection struct {
netconn net.Conn
zmtpconn *zmtp.Connection
}
type Socket interface {
Recv() ([]byte, error)
Send([]byte) error
Connect(endpoint string) error
Bind(endpoint string) (net.Addr, error)
SetRetry(retry time.Duration)
GetRetry() time.Duration
}
type socket struct {
sockType zmtp.SocketType
asServer bool
conns []*Connection
retryInterval time.Duration
lock sync.Mutex
mechanism zmtp.SecurityMechanism
messageChan chan *zmtp.Message
}
func NewSocket(sockType zmtp.SocketType, asServer bool, mechanism zmtp.SecurityMechanism) Socket {
return &socket{
asServer: asServer,
sockType: sockType,
retryInterval: defaultRetry,
mechanism: mechanism,
conns: make([]*Connection, 0),
messageChan: make(chan *zmtp.Message),
}
}
func (s *socket) Connect(endpoint string) error {
if s.asServer {
return ErrInvalidSockAction
}
parts := strings.Split(endpoint, "://")
Connect:
netconn, err := net.Dial(parts[0], parts[1])
if err != nil {
time.Sleep(s.GetRetry())
goto Connect
}
zmtpconn := zmtp.NewConnection(netconn)
_, err = zmtpconn.Prepare(s.mechanism, s.sockType, s.asServer, nil)
if err != nil {
return err
}
conn := &Connection{
netconn: netconn,
zmtpconn: zmtpconn,
}
s.conns = append(s.conns, conn)
zmtpconn.Recv(s.messageChan)
return nil
}
func (s *socket) Bind(endpoint string) (net.Addr, error) {
var addr net.Addr
if !s.asServer {
return addr, ErrInvalidSockAction
}
parts := strings.Split(endpoint, "://")
ln, err := net.Listen(parts[0], parts[1])
if err != nil {
return addr, err
}
netconn, err := ln.Accept()
if err != nil {
return addr, err
}
zmtpconn := zmtp.NewConnection(netconn)
_, err = zmtpconn.Prepare(s.mechanism, s.sockType, s.asServer, nil)
if err != nil {
return netconn.LocalAddr(), err
}
conn := &Connection{
netconn: netconn,
zmtpconn: zmtpconn,
}
s.conns = append(s.conns, conn)
zmtpconn.Recv(s.messageChan)
return netconn.LocalAddr(), nil
}
func (s *socket) GetRetry() time.Duration {
return s.retryInterval
}
func (s *socket) SetRetry(r time.Duration) {
s.retryInterval = r
}
func NewSecurityNull() *zmtp.SecurityNull {
return zmtp.NewSecurityNull()
}
func NewClient(mechanism zmtp.SecurityMechanism) Socket {
return NewSocket(ClientSocketType, false, mechanism)
}
func NewServer(mechanism zmtp.SecurityMechanism) Socket {
return NewSocket(ServerSocketType, true, mechanism)
}
func (s *socket) Recv() ([]byte, error) {
msg := <-s.messageChan
if msg.MessageType == zmtp.CommandMessage {
}
return msg.Body, msg.Err
}
func (s *socket) Send(b []byte) error {
return s.conns[0].zmtpconn.SendFrame(b)
}