forked from nanomsg/mangos-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inproc.go
261 lines (215 loc) · 5.54 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Copyright 2015 The Mangos Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use file except in compliance with the License.
// You may obtain a copy of the license at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package inproc implements an simple inproc transport for mangos.
package inproc
import (
"sync"
"github.com/gdamore/mangos"
)
// inproc implements the Pipe interface on top of channels.
type inproc struct {
rq chan *mangos.Message
wq chan *mangos.Message
closeq chan struct{}
readyq chan struct{}
proto mangos.Protocol
addr string
peer *inproc
}
type listener struct {
addr string
proto mangos.Protocol
accepters []*inproc
}
type inprocTran struct{}
var listeners struct {
// Who is listening, on which "address"?
byAddr map[string]*listener
cv sync.Cond
mx sync.Mutex
}
func init() {
listeners.byAddr = make(map[string]*listener)
listeners.cv.L = &listeners.mx
}
func (p *inproc) Recv() (*mangos.Message, error) {
if p.peer == nil {
return nil, mangos.ErrClosed
}
select {
case m, ok := <-p.rq:
if m == nil || !ok {
return nil, mangos.ErrClosed
}
// Upper protocols expect to have to pick header and
// body part. So mush them back together.
//msg.Body = append(msg.Header, msg.Body...)
//msg.Header = make([]byte, 0, 32)
return m, nil
case <-p.closeq:
return nil, mangos.ErrClosed
}
}
func (p *inproc) Send(m *mangos.Message) error {
if p.peer == nil {
return mangos.ErrClosed
}
// Upper protocols expect to have to pick header and body part.
// Also we need to have a fresh copy of the message for receiver, to
// break ownership.
nmsg := mangos.NewMessage(len(m.Header) + len(m.Body))
nmsg.Body = append(nmsg.Body, m.Header...)
nmsg.Body = append(nmsg.Body, m.Body...)
select {
case p.wq <- nmsg:
return nil
case <-p.closeq:
nmsg.Free()
return mangos.ErrClosed
}
}
func (p *inproc) LocalProtocol() uint16 {
return p.proto.Number()
}
func (p *inproc) RemoteProtocol() uint16 {
return p.proto.PeerNumber()
}
func (p *inproc) Close() error {
close(p.closeq)
return nil
}
func (p *inproc) IsOpen() bool {
select {
case <-p.closeq:
return false
default:
return true
}
}
func (p *inproc) GetProp(string) (interface{}, error) {
// We have no special properties
return nil, mangos.ErrBadProperty
}
type dialer struct {
addr string
proto mangos.Protocol
}
func (d *dialer) Dial() (mangos.Pipe, error) {
var server *inproc
client := &inproc{proto: d.proto, addr: d.addr}
client.readyq = make(chan struct{})
client.closeq = make(chan struct{})
listeners.mx.Lock()
// NB: No timeouts here!
for {
var l *listener
var ok bool
if l, ok = listeners.byAddr[d.addr]; !ok || l == nil {
listeners.mx.Unlock()
return nil, mangos.ErrConnRefused
}
if !mangos.ValidPeers(client.proto, l.proto) {
return nil, mangos.ErrBadProto
}
if len(l.accepters) != 0 {
server = l.accepters[len(l.accepters)-1]
l.accepters = l.accepters[:len(l.accepters)-1]
break
}
listeners.cv.Wait()
continue
}
listeners.mx.Unlock()
server.wq = make(chan *mangos.Message)
server.rq = make(chan *mangos.Message)
client.rq = server.wq
client.wq = server.rq
server.peer = client
client.peer = server
close(server.readyq)
close(client.readyq)
return client, nil
}
func (*dialer) SetOption(string, interface{}) error {
return mangos.ErrBadOption
}
func (*dialer) GetOption(string) (interface{}, error) {
return nil, mangos.ErrBadOption
}
func (l *listener) Listen() error {
listeners.mx.Lock()
if _, ok := listeners.byAddr[l.addr]; ok {
listeners.mx.Unlock()
return mangos.ErrAddrInUse
}
listeners.byAddr[l.addr] = l
listeners.cv.Broadcast()
listeners.mx.Unlock()
return nil
}
func (l *listener) Accept() (mangos.Pipe, error) {
server := &inproc{proto: l.proto, addr: l.addr}
server.readyq = make(chan struct{})
server.closeq = make(chan struct{})
listeners.mx.Lock()
l.accepters = append(l.accepters, server)
listeners.cv.Broadcast()
listeners.mx.Unlock()
select {
case <-server.readyq:
return server, nil
case <-server.closeq:
return nil, mangos.ErrClosed
}
}
func (*listener) SetOption(string, interface{}) error {
return mangos.ErrBadOption
}
func (*listener) GetOption(string) (interface{}, error) {
return nil, mangos.ErrBadOption
}
func (l *listener) Close() error {
listeners.mx.Lock()
if listeners.byAddr[l.addr] == l {
delete(listeners.byAddr, l.addr)
}
servers := l.accepters
l.accepters = nil
listeners.cv.Broadcast()
listeners.mx.Unlock()
for _, s := range servers {
close(s.closeq)
}
return nil
}
func (t *inprocTran) Scheme() string {
return "inproc"
}
func (t *inprocTran) NewDialer(addr string, proto mangos.Protocol) (mangos.PipeDialer, error) {
return &dialer{addr: addr, proto: proto}, nil
}
func (t *inprocTran) NewListener(addr string, proto mangos.Protocol) (mangos.PipeListener, error) {
l := &listener{addr: addr, proto: proto}
return l, nil
}
func (*inprocTran) SetOption(string, interface{}) error {
return mangos.ErrBadOption
}
func (*inprocTran) GetOption(string) (interface{}, error) {
return nil, mangos.ErrBadOption
}
// NewTransport allocates a new inproc:// transport.
func NewTransport() mangos.Transport {
return &inprocTran{}
}