mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-11-17 18:06:15 -05:00
54 lines
892 B
Go
54 lines
892 B
Go
package kcp
|
|
|
|
import (
|
|
"io"
|
|
"sync"
|
|
|
|
"v2ray.com/core/common/retry"
|
|
|
|
"v2ray.com/core/common"
|
|
"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 (w *SimpleSegmentWriter) Write(seg Segment) error {
|
|
w.Lock()
|
|
defer w.Unlock()
|
|
|
|
common.Must(w.buffer.Reset(seg.Bytes()))
|
|
_, err := w.writer.Write(w.buffer.Bytes())
|
|
return err
|
|
}
|
|
|
|
type RetryableWriter struct {
|
|
writer SegmentWriter
|
|
}
|
|
|
|
func NewRetryableWriter(writer SegmentWriter) SegmentWriter {
|
|
return &RetryableWriter{
|
|
writer: writer,
|
|
}
|
|
}
|
|
|
|
func (w *RetryableWriter) Write(seg Segment) error {
|
|
return retry.Timed(5, 100).On(func() error {
|
|
return w.writer.Write(seg)
|
|
})
|
|
}
|