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

81 lines
1.4 KiB

9 years ago
package kcp
import (
"io"
9 years ago
"sync"
"v2ray.com/core/common/alloc"
v2io "v2ray.com/core/common/io"
"v2ray.com/core/transport/internet"
9 years ago
)
9 years ago
type SegmentWriter interface {
Write(seg Segment)
9 years ago
}
type BufferedSegmentWriter struct {
9 years ago
sync.Mutex
mtu uint32
buffer *alloc.Buffer
writer v2io.Writer
}
9 years ago
func NewSegmentWriter(writer *AuthenticationWriter) *BufferedSegmentWriter {
return &BufferedSegmentWriter{
mtu: writer.Mtu(),
9 years ago
writer: writer,
}
}
8 years ago
func (v *BufferedSegmentWriter) Write(seg Segment) {
v.Lock()
defer v.Unlock()
9 years ago
nBytes := seg.ByteSize()
8 years ago
if uint32(v.buffer.Len()+nBytes) > v.mtu {
v.FlushWithoutLock()
9 years ago
}
8 years ago
if v.buffer == nil {
v.buffer = alloc.NewSmallBuffer().Clear()
9 years ago
}
8 years ago
v.buffer.Value = seg.Bytes(v.buffer.Value)
9 years ago
}
8 years ago
func (v *BufferedSegmentWriter) FlushWithoutLock() {
v.writer.Write(v.buffer)
v.buffer = nil
9 years ago
}
8 years ago
func (v *BufferedSegmentWriter) Flush() {
v.Lock()
defer v.Unlock()
9 years ago
8 years ago
if v.buffer.Len() == 0 {
9 years ago
return
}
8 years ago
v.FlushWithoutLock()
9 years ago
}
type AuthenticationWriter struct {
Authenticator internet.Authenticator
Writer io.Writer
Config *Config
}
8 years ago
func (v *AuthenticationWriter) Write(payload *alloc.Buffer) error {
defer payload.Release()
8 years ago
v.Authenticator.Seal(payload)
_, err := v.Writer.Write(payload.Value)
return err
}
8 years ago
func (v *AuthenticationWriter) Release() {}
8 years ago
func (v *AuthenticationWriter) Mtu() uint32 {
return v.Config.Mtu.GetValue() - uint32(v.Authenticator.Overhead())
}