File: net_conn.go

package info (click to toggle)
golang-github-azure-go-amqp 1.0.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,192 kB
  • sloc: makefile: 22
file content (515 lines) | stat: -rw-r--r-- 14,915 bytes parent folder | download | duplicates (2)
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package fake

import (
	"errors"
	"math"
	"net"
	"time"

	"github.com/Azure/go-amqp/internal/buffer"
	"github.com/Azure/go-amqp/internal/encoding"
	"github.com/Azure/go-amqp/internal/frames"
)

// NewNetConn creates a new instance of NetConn.
// Responder is invoked by Write when a frame is received.
// Return a zero-value Response/nil error to swallow the frame.
// Return a non-nil error to simulate a write error.
// NOTE: resp is called on a separate goroutine so it MUST NOT access any *testing.T etc
func NewNetConn(resp func(remoteChannel uint16, fr frames.FrameBody) (Response, error)) *NetConn {
	netConn := &NetConn{
		ReadErr:  make(chan error),
		WriteErr: make(chan error, 1),
		resp:     resp,
		// during shutdown, connReader can close before connWriter as they both
		// both return on c.Done being closed, so there is some non-determinism
		// here.  this means that sometimes writes can still happen but there's
		// no reader to consume them.  we used a buffered channel to prevent these
		// writes from blocking shutdown. the size was arbitrarily picked.
		readData: make(chan []byte, 10),
		// used to serialize writes so the frames are returned in their specified order.
		// buffering is necessary because write() will sleep when a write delay was
		// specified and we don't want to stall Write(). the size was arbitrarily picked.
		writeResp: make(chan Response, 10),
		close:     make(chan struct{}),
		readDL:    newNopTimer(), // default, no deadline
	}

	go netConn.write()
	return netConn
}

// NetConn is a fake network connection that satisfies the net.Conn interface.
type NetConn struct {
	// OnClose is called from Close() before it returns.
	// The value returned from OnClose is returned from Close().
	OnClose func() error

	// ReadErr is used to simulate a connReader error.
	// The error written to this channel is returned
	// from the call to NetConn.Read.
	ReadErr chan error

	// WriteErr is used to simulate a connWriter error.
	// The error sent here is returned from the call to NetConn.Write.
	// Has a buffer of one so setting a pending error won't block.
	WriteErr chan error

	resp      func(uint16, frames.FrameBody) (Response, error)
	readDL    readTimer
	readData  chan []byte
	writeResp chan Response
	close     chan struct{}
	closed    bool
}

// SendFrame sends the encoded frame to the client.
// Use this to send a frame at an arbitrary time.
func (n *NetConn) SendFrame(f []byte) {
	n.readData <- f
}

// SendKeepAlive sends a keep-alive frame to the client.
func (n *NetConn) SendKeepAlive() {
	// empty frame
	n.readData <- []uint8{0, 0, 0, 8, 2, 0, 0, 0}
}

// SendMultiFrameTransfer splits payload into 32-byte chunks, encodes, and sends to the client.
// Payload must be big enough for at least two chunks.
func (n *NetConn) SendMultiFrameTransfer(channel uint16, linkHandle, deliveryID uint32, payload []byte, edit func(int, *frames.PerformTransfer)) error {
	bb, err := encodeMultiFrameTransfer(channel, linkHandle, deliveryID, payload, edit)
	if err != nil {
		return err
	}
	for _, b := range bb {
		n.readData <- b
	}
	return nil
}

// Response is the response returned from a responder function.
type Response struct {
	// Payload is the marshalled frame to send to Conn.connReader
	Payload []byte

	// WriteDelay is the duration to wait before writing Payload.
	// Use this to introduce a delay when waiting for a response.
	WriteDelay time.Duration
}

// ErrAlreadyClosed is returned by Close() if [NetConn] is already closed.
var ErrAlreadyClosed = errors.New("fake already closed")

///////////////////////////////////////////////////////
// following methods are for the net.Conn interface
///////////////////////////////////////////////////////

// NOTE: Read, Write, and Close are all called by separate goroutines!

// Read is invoked by conn.connReader to recieve frame data.
// It blocks until Write or Close are called, or the read
// deadline expires which will return an error.
func (n *NetConn) Read(b []byte) (int, error) {
	select {
	case <-n.close:
		return 0, net.ErrClosed
	default:
		// not closed yet
	}

	select {
	case <-n.close:
		return 0, net.ErrClosed
	case <-n.readDL.C():
		return 0, errors.New("fake connection read deadline exceeded")
	case rd := <-n.readData:
		return copy(b, rd), nil
	case err := <-n.ReadErr:
		return 0, err
	}
}

// Write is invoked by conn.connWriter when we're being sent frame
// data.  Every call to Write will invoke the responder callback that
// must reply with one of three possibilities.
//  1. an encoded frame and nil error
//  2. a non-nil error to similate a write failure
//  3. a nil slice and nil error indicating the frame should be ignored
func (n *NetConn) Write(b []byte) (int, error) {
	select {
	case <-n.close:
		return 0, net.ErrClosed
	default:
		// not closed yet
	}

	select {
	case err := <-n.WriteErr:
		return 0, err
	default:
		// no fake write error
	}

	remoteChannel, frame, err := decodeFrame(b)
	if err != nil {
		return 0, err
	}
	resp, err := n.resp(remoteChannel, frame)
	if err != nil {
		return 0, err
	}
	if resp.Payload != nil {
		select {
		case n.writeResp <- resp:
			// resp was sent to write()
		default:
			// this means we incorrectly sized writeResp.
			// we do this to ensure that we never stall
			// waiting to write to writeResp.
			panic("writeResp full")
		}
	}
	return len(b), nil
}

func (n *NetConn) write() {
	for {
		select {
		case <-n.close:
			return
		case resp := <-n.writeResp:
			// any write delay MUST happen outside of NetConn.Write
			// else all we do is stall Conn.connWriter() which doesn't
			// actually simulate a delayed response to a frame.
			time.Sleep(resp.WriteDelay)
			n.readData <- resp.Payload
		}
	}
}

// Close is called by conn.close.
func (n *NetConn) Close() error {
	if n.closed {
		return ErrAlreadyClosed
	}
	n.closed = true
	close(n.close)
	if n.OnClose != nil {
		return n.OnClose()
	}
	return nil
}

func (n *NetConn) LocalAddr() net.Addr {
	return &net.IPAddr{
		IP: net.IPv4(127, 0, 0, 2),
	}
}

func (n *NetConn) RemoteAddr() net.Addr {
	return &net.IPAddr{
		IP: net.IPv4(127, 0, 0, 2),
	}
}

func (n *NetConn) SetDeadline(t time.Time) error {
	return errors.New("not used")
}

func (n *NetConn) SetReadDeadline(t time.Time) error {
	// called by conn.connReader before calling Read
	// stop the last timer if available
	if n.readDL != nil && !n.readDL.Stop() {
		<-n.readDL.C()
	}
	n.readDL = timer{t: time.NewTimer(time.Until(t))}
	return nil
}

func (n *NetConn) SetWriteDeadline(t time.Time) error {
	// called by conn.connWriter before calling Write
	return nil
}

///////////////////////////////////////////////////////
///////////////////////////////////////////////////////

// ProtoID indicates the type of protocol (copied from conn.go)
type ProtoID uint8

const (
	ProtoAMQP ProtoID = 0x0
	ProtoTLS  ProtoID = 0x2
	ProtoSASL ProtoID = 0x3
)

// ProtoHeader adds the initial handshake frame to the list of responses.
// This frame, and PerformOpen, are needed when calling amqp.New() to create a client.
func ProtoHeader(id ProtoID) ([]byte, error) {
	return []byte{'A', 'M', 'Q', 'P', byte(id), 1, 0, 0}, nil
}

// PerformOpen appends a PerformOpen frame with the specified container ID.
// This frame, and ProtoHeader, are needed when calling amqp.New() to create a client.
func PerformOpen(containerID string) ([]byte, error) {
	// send the default values for max channels and frame size
	return EncodeFrame(frames.TypeAMQP, 0, &frames.PerformOpen{
		ChannelMax:   65535,
		ContainerID:  containerID,
		IdleTimeout:  time.Minute,
		MaxFrameSize: 4294967295,
	})
}

// PerformBegin appends a PerformBegin frame with the specified remote channel ID.
// This frame is needed when making a call to Client.NewSession().
func PerformBegin(channel, remoteChannel uint16) ([]byte, error) {
	return EncodeFrame(frames.TypeAMQP, channel, &frames.PerformBegin{
		RemoteChannel:  &remoteChannel,
		NextOutgoingID: 1,
		IncomingWindow: 5000,
		OutgoingWindow: 1000,
		HandleMax:      math.MaxInt16,
	})
}

// SenderAttach encodes a PerformAttach frame with the specified values.
// This frame is needed when making a call to Session.NewSender().
func SenderAttach(channel uint16, linkName string, linkHandle uint32, mode encoding.SenderSettleMode) ([]byte, error) {
	return EncodeFrame(frames.TypeAMQP, channel, &frames.PerformAttach{
		Name:   linkName,
		Handle: linkHandle,
		Role:   encoding.RoleReceiver,
		Target: &frames.Target{
			Address:      "test",
			Durable:      encoding.DurabilityNone,
			ExpiryPolicy: encoding.ExpirySessionEnd,
		},
		SenderSettleMode: &mode,
		MaxMessageSize:   math.MaxUint32,
	})
}

// ReceiverAttach appends a PerformAttach frame with the specified values.
// This frame is needed when making a call to Session.NewReceiver().
func ReceiverAttach(channel uint16, linkName string, linkHandle uint32, mode encoding.ReceiverSettleMode, filter encoding.Filter) ([]byte, error) {
	return EncodeFrame(frames.TypeAMQP, channel, &frames.PerformAttach{
		Name:   linkName,
		Handle: linkHandle,
		Role:   encoding.RoleSender,
		Source: &frames.Source{
			Address:      "test",
			Durable:      encoding.DurabilityNone,
			ExpiryPolicy: encoding.ExpirySessionEnd,
			Filter:       filter,
		},
		ReceiverSettleMode: &mode,
		MaxMessageSize:     math.MaxUint32,
	})
}

// PerformTransfer appends a PerformTransfer frame with the specified values.
// The linkHandle MUST match the linkHandle value specified in ReceiverAttach.
func PerformTransfer(channel uint16, linkHandle, deliveryID uint32, payload []byte) ([]byte, error) {
	format := uint32(0)
	payloadBuf := &buffer.Buffer{}
	encoding.WriteDescriptor(payloadBuf, encoding.TypeCodeApplicationData)
	err := encoding.WriteBinary(payloadBuf, payload)
	if err != nil {
		return nil, err
	}
	return EncodeFrame(frames.TypeAMQP, channel, &frames.PerformTransfer{
		Handle:        linkHandle,
		DeliveryID:    &deliveryID,
		DeliveryTag:   []byte("tag"),
		MessageFormat: &format,
		Payload:       payloadBuf.Detach(),
	})
}

// PerformDisposition appends a PerformDisposition frame with the specified values.
// The firstID MUST match the deliveryID value specified in PerformTransfer.
func PerformDisposition(role encoding.Role, channel uint16, firstID uint32, lastID *uint32, state encoding.DeliveryState) ([]byte, error) {
	return EncodeFrame(frames.TypeAMQP, channel, &frames.PerformDisposition{
		Role:    role,
		First:   firstID,
		Last:    lastID,
		Settled: true,
		State:   state,
	})
}

// PerformDetach encodes a PerformDetach frame with an optional error.
func PerformDetach(channel uint16, linkHandle uint32, e *encoding.Error) ([]byte, error) {
	return EncodeFrame(frames.TypeAMQP, channel, &frames.PerformDetach{Handle: linkHandle, Closed: true, Error: e})
}

// PerformEnd encodes a PerformEnd frame with an optional error.
func PerformEnd(channel uint16, e *encoding.Error) ([]byte, error) {
	return EncodeFrame(frames.TypeAMQP, channel, &frames.PerformEnd{Error: e})
}

// PerformClose encodes a PerformClose frame with an optional error.
func PerformClose(e *encoding.Error) ([]byte, error) {
	return EncodeFrame(frames.TypeAMQP, 0, &frames.PerformClose{Error: e})
}

// AMQPProto is the frame type passed to FrameCallback() for the initial protocal handshake.
type AMQPProto struct {
	frames.FrameBody
}

// KeepAlive is the frame type passed to FrameCallback() for keep-alive frames.
type KeepAlive struct {
	frames.FrameBody
}

type frameHeader frames.Header

func (f frameHeader) Marshal(wr *buffer.Buffer) error {
	wr.AppendUint32(f.Size)
	wr.AppendByte(f.DataOffset)
	wr.AppendByte(byte(f.FrameType))
	wr.AppendUint16(f.Channel)
	return nil
}

// EncodeFrame encodes the specified frame to be sent over the wire.
func EncodeFrame(t frames.Type, channel uint16, f frames.FrameBody) ([]byte, error) {
	bodyBuf := buffer.New([]byte{})
	if err := encoding.Marshal(bodyBuf, f); err != nil {
		return nil, err
	}
	// create the frame header, needs size of the body plus itself
	header := frameHeader{
		Size:       uint32(bodyBuf.Len()) + 8,
		DataOffset: 2,
		FrameType:  uint8(t),
		Channel:    channel,
	}
	headerBuf := buffer.New([]byte{})
	if err := encoding.Marshal(headerBuf, header); err != nil {
		return nil, err
	}
	// concatenate header + body
	raw := headerBuf.Detach()
	raw = append(raw, bodyBuf.Detach()...)
	return raw, nil
}

func decodeFrame(b []byte) (uint16, frames.FrameBody, error) {
	if len(b) > 3 && b[0] == 'A' && b[1] == 'M' && b[2] == 'Q' && b[3] == 'P' {
		return 0, &AMQPProto{}, nil
	}
	buf := buffer.New(b)
	header, err := frames.ParseHeader(buf)
	if err != nil {
		return 0, nil, err
	}
	bodySize := int64(header.Size - frames.HeaderSize)
	if bodySize == 0 {
		// keep alive frame
		return 0, &KeepAlive{}, nil
	}
	// parse the frame
	b, ok := buf.Next(bodySize)
	if !ok {
		return 0, nil, err
	}
	fr, err := frames.ParseBody(buffer.New(b))
	if err != nil {
		return 0, nil, err
	}
	return header.Channel, fr, nil
}

func encodeMultiFrameTransfer(channel uint16, linkHandle, deliveryID uint32, payload []byte, edit func(int, *frames.PerformTransfer)) ([][]byte, error) {
	frameData := [][]byte{}
	format := uint32(0)
	payloadBuf := &buffer.Buffer{}
	// determine the number of frames to create
	chunks := len(payload) / 32
	if r := len(payload) % 32; r > 0 {
		chunks++
	}
	if chunks < 2 {
		return nil, errors.New("payload is too small for multi-frame transfer")
	}
	more := true
	for chunk := 0; chunk < chunks; chunk++ {
		encoding.WriteDescriptor(payloadBuf, encoding.TypeCodeApplicationData)
		var err error
		if chunk+1 < chunks {
			err = encoding.WriteBinary(payloadBuf, payload[chunk*32:chunk*32+32])
		} else {
			// final frame
			err = encoding.WriteBinary(payloadBuf, payload[chunk*32:])
			more = false
		}
		if err != nil {
			return nil, err
		}
		var fr *frames.PerformTransfer
		if chunk == 0 {
			// first frame requires extra data
			fr = &frames.PerformTransfer{
				Handle:        linkHandle,
				DeliveryID:    &deliveryID,
				DeliveryTag:   []byte("tag"),
				MessageFormat: &format,
				More:          true,
				Payload:       payloadBuf.Detach(),
			}
		} else {
			fr = &frames.PerformTransfer{
				Handle:  linkHandle,
				More:    more,
				Payload: payloadBuf.Detach(),
			}
		}
		if edit != nil {
			edit(chunk, fr)
		}
		b, err := EncodeFrame(frames.TypeAMQP, channel, fr)
		if err != nil {
			return nil, err
		}
		frameData = append(frameData, b)
	}
	return frameData, nil
}

type readTimer interface {
	C() <-chan time.Time
	Stop() bool
}

func newNopTimer() nopTimer {
	return nopTimer{t: make(chan time.Time)}
}

type nopTimer struct {
	t chan time.Time
}

func (n nopTimer) C() <-chan time.Time {
	return n.t
}

func (n nopTimer) Stop() bool {
	close(n.t)
	return true
}

type timer struct {
	t *time.Timer
}

func (t timer) C() <-chan time.Time {
	return t.t.C
}

func (t timer) Stop() bool {
	return t.t.Stop()
}