-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrecv.go
52 lines (41 loc) · 1.03 KB
/
recv.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
package cypress
import "io"
// A type which can recieve a stream of Messages reliabliy.
// Recv works in coordination with Send to reliablity send Messages
// using ack'ing.
type Recv struct {
rw io.ReadWriter
dec *StreamDecoder
}
// Create a new Recv, reading and writing from rw.
func NewRecv(rw io.ReadWriter) (*Recv, error) {
dec, err := NewStreamDecoder(rw)
if err != nil {
return nil, err
}
return &Recv{rw, dec}, nil
}
func (r *Recv) recvMessage() (*Message, error) {
return r.dec.Generate()
}
var reliableAckBytes = []byte{'k'}
func (r *Recv) sendAck() error {
_, err := r.rw.Write(reliableAckBytes)
return err
}
// Generate a new Message reading from the stream. If the stream
// is in reliable mode (the default) then an ack is sent back.
func (r *Recv) Generate() (*Message, error) {
m, err := r.recvMessage()
if err != nil {
return nil, err
}
if r.dec.Header.GetMode() == StreamHeader_RELIABLE {
r.sendAck()
}
return m, nil
}
// To satisify the Generator interface
func (r *Recv) Close() error {
return nil
}