-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstream.go
133 lines (113 loc) · 3 KB
/
stream.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
// SPDX-License-Identifier: Apache-2.0
package frisbee
import (
"sync"
"sync/atomic"
"github.com/loopholelabs/common/pkg/queue"
"github.com/loopholelabs/frisbee-go/pkg/packet"
)
// DefaultStreamBufferSize is the default size of the stream buffer.
const DefaultStreamBufferSize = 1 << 12
type NewStreamHandler func(*Stream)
type Stream struct {
id uint16
conn *Async
closed atomic.Bool
queue *queue.Circular[packet.Packet, *packet.Packet]
staleMu sync.Mutex
stale []*packet.Packet
}
func newStream(id uint16, conn *Async) *Stream {
return &Stream{
id: id,
conn: conn,
queue: queue.NewCircular[packet.Packet, *packet.Packet](DefaultStreamBufferSize),
}
}
// ReadPacket is a blocking function that will wait until a Frisbee packet is available and then return it (and its content).
// In the event that the connection is closed, ReadPacket will return an error.
func (s *Stream) ReadPacket() (*packet.Packet, error) {
if s.closed.Load() {
s.staleMu.Lock()
if len(s.stale) > 0 {
var p *packet.Packet
p, s.stale = s.stale[0], s.stale[1:]
s.staleMu.Unlock()
return p, nil
}
s.staleMu.Unlock()
return nil, StreamClosed
}
readPacket, err := s.queue.Pop()
if err != nil {
if s.closed.Load() {
s.staleMu.Lock()
if len(s.stale) > 0 {
var p *packet.Packet
p, s.stale = s.stale[0], s.stale[1:]
s.staleMu.Unlock()
return p, nil
}
s.staleMu.Unlock()
return nil, StreamClosed
}
return nil, err
}
return readPacket, nil
}
// WritePacket will write the given packet to the stream but the ID and Operation will be
// overwritten with the stream's ID and the STREAM operation. Packets send to a stream
// must have a ContentLength greater than 0.
func (s *Stream) WritePacket(p *packet.Packet) error {
if s.closed.Load() {
return StreamClosed
}
if p.Metadata.ContentLength == 0 {
return InvalidStreamPacket
}
p.Metadata.Id = s.id
p.Metadata.Operation = STREAM
return s.conn.writePacket(p, true)
}
// ID returns the stream's ID.
func (s *Stream) ID() uint16 {
return s.id
}
// Conn returns the connection that the stream is associated with.
func (s *Stream) Conn() *Async {
return s.conn
}
// Close will close the stream and prevent any further reads or writes.
func (s *Stream) Close() error {
return s.closeSend(true)
}
func (s *Stream) closeSend(lock bool) error {
s.staleMu.Lock()
if s.closed.CompareAndSwap(false, true) {
s.queue.Close()
s.stale = s.queue.Drain()
s.staleMu.Unlock()
p := packet.Get()
p.Metadata.Id = s.id
p.Metadata.Operation = STREAM
err := s.conn.writePacket(p, true)
packet.Put(p)
if lock {
s.conn.streamsMu.Lock()
delete(s.conn.streams, s.id)
s.conn.streamsMu.Unlock()
}
return err
}
s.staleMu.Unlock()
return StreamClosed
}
// close will close the stream and prevent any further reads or writes without sending a stream close packet.
func (s *Stream) close() {
s.staleMu.Lock()
if s.closed.CompareAndSwap(false, true) {
s.queue.Close()
s.stale = s.queue.Drain()
}
s.staleMu.Unlock()
}