mirror of https://github.com/v2ray/v2ray-core
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
864 B
56 lines
864 B
package kcp
|
|
|
|
import (
|
|
"io"
|
|
"sync"
|
|
|
|
"v2ray.com/core/common/buf"
|
|
)
|
|
|
|
type SegmentWriter interface {
|
|
Write(seg Segment)
|
|
}
|
|
|
|
type BufferedSegmentWriter struct {
|
|
sync.Mutex
|
|
mtu uint32
|
|
buffer *buf.Buffer
|
|
writer io.Writer
|
|
}
|
|
|
|
func NewSegmentWriter(writer io.Writer, mtu uint32) *BufferedSegmentWriter {
|
|
return &BufferedSegmentWriter{
|
|
mtu: mtu,
|
|
writer: writer,
|
|
buffer: buf.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()
|
|
}
|