mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 18:17:52 -05:00
36 lines
552 B
Go
36 lines
552 B
Go
package kcp
|
|
|
|
import (
|
|
"io"
|
|
"sync"
|
|
|
|
"v2ray.com/core/common/buf"
|
|
)
|
|
|
|
type SegmentWriter interface {
|
|
Write(seg Segment) error
|
|
}
|
|
|
|
type SimpleSegmentWriter struct {
|
|
sync.Mutex
|
|
buffer *buf.Buffer
|
|
writer io.Writer
|
|
}
|
|
|
|
func NewSegmentWriter(writer io.Writer) SegmentWriter {
|
|
return &SimpleSegmentWriter{
|
|
writer: writer,
|
|
buffer: buf.NewSmall(),
|
|
}
|
|
}
|
|
|
|
func (v *SimpleSegmentWriter) Write(seg Segment) error {
|
|
v.Lock()
|
|
defer v.Unlock()
|
|
|
|
v.buffer.AppendSupplier(seg.Bytes())
|
|
_, err := v.writer.Write(v.buffer.Bytes())
|
|
v.buffer.Clear()
|
|
return err
|
|
}
|