mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 18:17:52 -05:00
56 lines
870 B
Go
56 lines
870 B
Go
package kcp
|
|
|
|
import (
|
|
"io"
|
|
"sync"
|
|
|
|
"v2ray.com/core/common/alloc"
|
|
)
|
|
|
|
type SegmentWriter interface {
|
|
Write(seg Segment)
|
|
}
|
|
|
|
type BufferedSegmentWriter struct {
|
|
sync.Mutex
|
|
mtu uint32
|
|
buffer *alloc.Buffer
|
|
writer io.Writer
|
|
}
|
|
|
|
func NewSegmentWriter(writer io.Writer, mtu uint32) *BufferedSegmentWriter {
|
|
return &BufferedSegmentWriter{
|
|
mtu: mtu,
|
|
writer: writer,
|
|
buffer: alloc.NewSmallBuffer(),
|
|
}
|
|
}
|
|
|
|
func (v *BufferedSegmentWriter) Write(seg Segment) {
|
|
v.Lock()
|
|
defer v.Unlock()
|
|
|
|
nBytes := seg.ByteSize()
|
|
if uint32(v.buffer.Len()+nBytes) > v.mtu {
|
|
v.FlushWithoutLock()
|
|
}
|
|
|
|
v.buffer.AppendFunc(seg.Bytes())
|
|
}
|
|
|
|
func (v *BufferedSegmentWriter) FlushWithoutLock() {
|
|
v.writer.Write(v.buffer.Bytes())
|
|
v.buffer.Clear()
|
|
}
|
|
|
|
func (v *BufferedSegmentWriter) Flush() {
|
|
v.Lock()
|
|
defer v.Unlock()
|
|
|
|
if v.buffer.IsEmpty() {
|
|
return
|
|
}
|
|
|
|
v.FlushWithoutLock()
|
|
}
|