1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-05 09:47:56 -05:00
v2fly/transport/internet/kcp/output.go
2017-02-07 00:38:46 +01:00

35 lines
525 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.Reset(seg.Bytes())
_, err := v.writer.Write(v.buffer.Bytes())
return err
}