-
Notifications
You must be signed in to change notification settings - Fork 2
/
buffered_stream.go
37 lines (31 loc) · 995 Bytes
/
buffered_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
package liblpc
import "github.com/gen-iot/std"
type BufferedStream struct {
*Stream
bytesBuffer std.RwBuffer
onBufferedReadCb BufferedStreamOnRead
}
type BufferedStreamOnRead func(sw StreamWriter, buf std.ReadableBuffer)
func NewBufferedConnStream(loop *IOEvtLoop, fd int, onRead BufferedStreamOnRead) *BufferedStream {
s := new(BufferedStream)
s.Stream = NewConnStream(loop, fd, s.onStreamRead)
s.SetWatcher(s)
s.bytesBuffer = std.NewByteBuffer()
s.onBufferedReadCb = onRead
return s
}
func NewBufferedClientStream(loop *IOEvtLoop, fd int, onRead BufferedStreamOnRead) *BufferedStream {
s := new(BufferedStream)
s.Stream = NewClientStream(loop, fd, s.onStreamRead)
s.SetWatcher(s)
s.bytesBuffer = std.NewByteBuffer()
s.onBufferedReadCb = onRead
return s
}
func (this *BufferedStream) onStreamRead(sw StreamWriter, data []byte, len int) {
this.bytesBuffer.Write(data[:len])
if this.onBufferedReadCb == nil {
return
}
this.onBufferedReadCb(sw, this.bytesBuffer)
}