You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
v2ray-core/transport/internet/kcp/output.go

54 lines
892 B

9 years ago
package kcp
import (
"io"
9 years ago
"sync"
7 years ago
"v2ray.com/core/common/retry"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
9 years ago
)
9 years ago
type SegmentWriter interface {
Write(seg Segment) error
9 years ago
}
type SimpleSegmentWriter struct {
9 years ago
sync.Mutex
buffer *buf.Buffer
writer io.Writer
9 years ago
}
func NewSegmentWriter(writer io.Writer) SegmentWriter {
return &SimpleSegmentWriter{
9 years ago
writer: writer,
buffer: buf.New(),
9 years ago
}
}
7 years ago
func (w *SimpleSegmentWriter) Write(seg Segment) error {
w.Lock()
defer w.Unlock()
9 years ago
7 years ago
common.Must(w.buffer.Reset(seg.Bytes()))
_, err := w.writer.Write(w.buffer.Bytes())
return err
9 years ago
}
7 years ago
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)
})
}