forked from nanomsg/mangos-v1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
265 lines (230 loc) · 6.92 KB
/
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
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
262
263
264
265
// 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 mangos
import (
"encoding/binary"
"io"
"net"
"sync"
)
// conn implements the Pipe interface on top of net.Conn. The
// assumption is that transports using this have similar wire protocols,
// and conn is meant to be used as a building block.
type conn struct {
c net.Conn
rlock sync.Mutex
wlock sync.Mutex
proto Protocol
open bool
props map[string]interface{}
}
// connipc is *almost* like a regular conn, but the IPC protocol insists
// on stuffing a leading byte (valued 1) in front of messages. This is for
// compatibility with nanomsg -- the value cannot ever be anything but 1.
type connipc struct {
conn
}
// Recv implements the Pipe Recv method. The message received is expected as
// a 64-bit size (network byte order) followed by the message itself.
func (p *conn) Recv() (*Message, error) {
var sz int64
var err error
var msg *Message
// prevent interleaved reads
p.rlock.Lock()
defer p.rlock.Unlock()
if err = binary.Read(p.c, binary.BigEndian, &sz); err != nil {
return nil, err
}
// TBD: This fixed limit is kind of silly, but it keeps
// a bogus peer from causing us to try to allocate ridiculous
// amounts of memory. If you don't like it, then prealloc
// a buffer. But for protocols that only use small messages
// this can actually be more efficient since we don't allocate
// any more space than our peer says we need to.
if sz > 1024*1024 || sz < 0 {
p.c.Close()
return nil, ErrTooLong
}
msg = NewMessage(int(sz))
msg.Body = msg.Body[0:sz]
if _, err = io.ReadFull(p.c, msg.Body); err != nil {
msg.Free()
return nil, err
}
return msg, nil
}
// Send implements the Pipe Send method. The message is sent as a 64-bit
// size (network byte order) followed by the message itself.
func (p *conn) Send(msg *Message) error {
l := uint64(len(msg.Header) + len(msg.Body))
// prevent interleaved writes
p.wlock.Lock()
defer p.wlock.Unlock()
// send length header
if err := binary.Write(p.c, binary.BigEndian, l); err != nil {
return err
}
if _, err := p.c.Write(msg.Header); err != nil {
return err
}
// hope this works
if _, err := p.c.Write(msg.Body); err != nil {
return err
}
msg.Free()
return nil
}
// LocalProtocol returns our local protocol number.
func (p *conn) LocalProtocol() uint16 {
return p.proto.Number()
}
// RemoteProtocol returns our peer's protocol number.
func (p *conn) RemoteProtocol() uint16 {
return p.proto.PeerNumber()
}
// Close implements the Pipe Close method.
func (p *conn) Close() error {
p.open = false
return p.c.Close()
}
// IsOpen implements the PipeIsOpen method.
func (p *conn) IsOpen() bool {
return p.open
}
func (p *conn) GetProp(n string) (interface{}, error) {
if v, ok := p.props[n]; ok {
return v, nil
}
return nil, ErrBadProperty
}
// NewConnPipe allocates a new Pipe using the supplied net.Conn, and
// initializes it. It performs the handshake required at the SP layer,
// only returning the Pipe once the SP layer negotiation is complete.
//
// Stream oriented transports can utilize this to implement a Transport.
// The implementation will also need to implement PipeDialer, PipeAccepter,
// and the Transport enclosing structure. Using this layered interface,
// the implementation needn't bother concerning itself with passing actual
// SP messages once the lower layer connection is established.
func NewConnPipe(c net.Conn, proto Protocol) (Pipe, error) {
p := &conn{c: c, proto: proto, props: make(map[string]interface{})}
if err := p.handshake(); err != nil {
return nil, err
}
return p, nil
}
// NewConnPipeIPC allocates a new Pipe using the IPC exchange protocol.
func NewConnPipeIPC(c net.Conn, proto Protocol) (Pipe, error) {
p := &connipc{conn: conn{c: c, proto: proto, props: make(map[string]interface{})}}
if err := p.handshake(); err != nil {
return nil, err
}
return p, nil
}
func (p *connipc) Send(msg *Message) error {
l := uint64(len(msg.Header) + len(msg.Body))
one := [1]byte{1}
var err error
// prevent interleaved writes
p.wlock.Lock()
defer p.wlock.Unlock()
// send length header
if _, err = p.c.Write(one[:]); err != nil {
return err
}
if err = binary.Write(p.c, binary.BigEndian, l); err != nil {
return err
}
if _, err = p.c.Write(msg.Header); err != nil {
return err
}
// hope this works
if _, err = p.c.Write(msg.Body); err != nil {
return err
}
msg.Free()
return nil
}
func (p *connipc) Recv() (*Message, error) {
var sz int64
var err error
var msg *Message
var one [1]byte
// prevent interleaved reads
p.rlock.Lock()
defer p.rlock.Unlock()
if _, err = p.c.Read(one[:]); err != nil {
return nil, err
}
if err = binary.Read(p.c, binary.BigEndian, &sz); err != nil {
return nil, err
}
// TBD: This fixed limit is kind of silly, but it keeps
// a bogus peer from causing us to try to allocate ridiculous
// amounts of memory. If you don't like it, then prealloc
// a buffer. But for protocols that only use small messages
// this can actually be more efficient since we don't allocate
// any more space than our peer says we need to.
if sz > 1024*1024 || sz < 0 {
p.c.Close()
return nil, ErrTooLong
}
msg = NewMessage(int(sz))
msg.Body = msg.Body[0:sz]
if _, err = io.ReadFull(p.c, msg.Body); err != nil {
msg.Free()
return nil, err
}
return msg, nil
}
// connHeader is exchanged during the initial handshake.
type connHeader struct {
Zero byte // must be zero
S byte // 'S'
P byte // 'P'
Version byte // only zero at present
Proto uint16
Rsvd uint16 // always zero at present
}
// handshake establishes an SP connection between peers. Both sides must
// send the header, then both sides must wait for the peer's header.
// As a side effect, the peer's protocol number is stored in the conn.
func (p *conn) handshake() error {
var err error
h := connHeader{S: 'S', P: 'P', Proto: p.proto.Number()}
if err = binary.Write(p.c, binary.BigEndian, &h); err != nil {
return err
}
if err = binary.Read(p.c, binary.BigEndian, &h); err != nil {
p.c.Close()
return err
}
if h.Zero != 0 || h.S != 'S' || h.P != 'P' || h.Rsvd != 0 {
p.c.Close()
return ErrBadHeader
}
// The only version number we support at present is "0", at offset 3.
if h.Version != 0 {
p.c.Close()
return ErrBadVersion
}
// The protocol number lives as 16-bits (big-endian) at offset 4.
if h.Proto != p.proto.PeerNumber() {
p.c.Close()
return ErrBadProto
}
p.open = true
return nil
}