mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-09 03:37:37 -05:00
35 lines
520 B
Go
35 lines
520 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.New(),
|
|
}
|
|
}
|
|
|
|
func (v *SimpleSegmentWriter) Write(seg Segment) error {
|
|
v.Lock()
|
|
defer v.Unlock()
|
|
|
|
v.buffer.Reset(seg.Bytes())
|
|
_, err := v.writer.Write(v.buffer.Bytes())
|
|
return err
|
|
}
|