2016-06-27 16:52:48 -04:00
|
|
|
package kcp
|
|
|
|
|
|
|
|
import (
|
2016-06-29 04:34:34 -04:00
|
|
|
"io"
|
2016-06-27 16:52:48 -04:00
|
|
|
"sync"
|
|
|
|
|
2016-12-09 05:35:27 -05:00
|
|
|
"v2ray.com/core/common/buf"
|
2016-06-27 16:52:48 -04:00
|
|
|
)
|
|
|
|
|
2016-07-02 15:26:50 -04:00
|
|
|
type SegmentWriter interface {
|
2016-12-20 16:53:58 -05:00
|
|
|
Write(seg Segment) error
|
2016-07-02 15:26:50 -04:00
|
|
|
}
|
|
|
|
|
2016-12-20 16:53:58 -05:00
|
|
|
type SimpleSegmentWriter struct {
|
2016-06-27 16:52:48 -04:00
|
|
|
sync.Mutex
|
2016-12-09 05:35:27 -05:00
|
|
|
buffer *buf.Buffer
|
2016-12-08 10:27:41 -05:00
|
|
|
writer io.Writer
|
2016-06-27 16:52:48 -04:00
|
|
|
}
|
|
|
|
|
2016-12-20 16:53:58 -05:00
|
|
|
func NewSegmentWriter(writer io.Writer) SegmentWriter {
|
|
|
|
return &SimpleSegmentWriter{
|
2016-06-27 16:52:48 -04:00
|
|
|
writer: writer,
|
2016-12-09 06:08:25 -05:00
|
|
|
buffer: buf.NewSmall(),
|
2016-06-27 16:52:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 16:53:58 -05:00
|
|
|
func (v *SimpleSegmentWriter) Write(seg Segment) error {
|
2016-11-27 15:39:09 -05:00
|
|
|
v.Lock()
|
|
|
|
defer v.Unlock()
|
2016-06-27 16:52:48 -04:00
|
|
|
|
2017-02-06 18:38:46 -05:00
|
|
|
v.buffer.Reset(seg.Bytes())
|
2016-12-20 16:53:58 -05:00
|
|
|
_, err := v.writer.Write(v.buffer.Bytes())
|
|
|
|
return err
|
2016-06-27 16:52:48 -04:00
|
|
|
}
|