-
Notifications
You must be signed in to change notification settings - Fork 5
/
stream.go
62 lines (53 loc) · 990 Bytes
/
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
package smux
import (
"io"
"net"
)
type Stream struct {
conn net.Conn
id uint32
writer *io.PipeWriter
reader *io.PipeReader
in chan []byte
}
func NewStream(id uint32, in chan []byte, c *Conn) Stream {
pr, pw := io.Pipe()
return Stream{
id: id,
in: in,
conn: c,
reader: pr,
writer: pw,
}
}
func (s Stream) Poll() {
for payload := range s.in {
s.writer.Write(payload)
}
s.writer.Close()
}
func (s Stream) Read(b []byte) (int, error) {
return s.reader.Read(b)
}
func (s Stream) Write(b []byte) (int, error) {
return s.write(b, false)
}
func (s Stream) WriteOnce(b []byte) (int, error) {
return s.write(b, true)
}
func (s Stream) write(b []byte, seal bool) (int, error) {
frames := packing(s.id, b, seal)
sum := 0
for i, _ := range frames {
n, err := s.conn.Write(frames[i])
if err != nil {
return 0, err
}
sum += n
}
return sum, nil
}
func (s Stream) Close() error {
_, err := s.conn.Write(sealing(s.id))
return err
}