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.
v2ray-core/transport/internet/kcp/output.go

56 lines
862 B

9 years ago
package kcp
import (
"io"
9 years ago
"sync"
"v2ray.com/core/common/buf"
9 years ago
)
9 years ago
type SegmentWriter interface {
Write(seg Segment)
9 years ago
}
type BufferedSegmentWriter struct {
9 years ago
sync.Mutex
mtu uint32
buffer *buf.Buffer
writer io.Writer
9 years ago
}
func NewSegmentWriter(writer io.Writer, mtu uint32) *BufferedSegmentWriter {
9 years ago
return &BufferedSegmentWriter{
mtu: mtu,
9 years ago
writer: writer,
8 years ago
buffer: buf.NewSmall(),
9 years ago
}
}
8 years ago
func (v *BufferedSegmentWriter) Write(seg Segment) {
v.Lock()
defer v.Unlock()
9 years ago
nBytes := seg.ByteSize()
8 years ago
if uint32(v.buffer.Len()+nBytes) > v.mtu {
v.FlushWithoutLock()
9 years ago
}
8 years ago
v.buffer.AppendSupplier(seg.Bytes())
9 years ago
}
8 years ago
func (v *BufferedSegmentWriter) FlushWithoutLock() {
v.writer.Write(v.buffer.Bytes())
v.buffer.Clear()
9 years ago
}
8 years ago
func (v *BufferedSegmentWriter) Flush() {
v.Lock()
defer v.Unlock()
9 years ago
if v.buffer.IsEmpty() {
9 years ago
return
}
8 years ago
v.FlushWithoutLock()
9 years ago
}