mirror of
https://github.com/XTLS/Xray-core.git
synced 2024-11-22 16:41:29 +00:00
e93da4bd02
* Increase some tls test timeout * Fix TestUserValidator * Change all tests to VMessAEAD Old VMess MD5 tests will be rejected and fail in 2022 * Chore: auto format code
54 lines
934 B
Go
54 lines
934 B
Go
package kcp
|
|
|
|
import (
|
|
"io"
|
|
"sync"
|
|
|
|
"github.com/xtls/xray-core/common/buf"
|
|
"github.com/xtls/xray-core/common/retry"
|
|
)
|
|
|
|
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()
|
|
|
|
w.buffer.Clear()
|
|
rawBytes := w.buffer.Extend(seg.ByteSize())
|
|
seg.Serialize(rawBytes)
|
|
_, 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)
|
|
})
|
|
}
|