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

80 lines
1.5 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,
}
}
func (this *BufferedSegmentWriter) Write(seg Segment) {
9 years ago
this.Lock()
defer this.Unlock()
nBytes := seg.ByteSize()
if uint32(this.buffer.Len()+nBytes) > this.mtu {
this.FlushWithoutLock()
}
if this.buffer == nil {
this.buffer = alloc.NewLocalBuffer(2048).Clear()
9 years ago
}
this.buffer.Value = seg.Bytes(this.buffer.Value)
9 years ago
}
9 years ago
func (this *BufferedSegmentWriter) FlushWithoutLock() {
go this.writer.Write(this.buffer)
9 years ago
this.buffer = nil
}
9 years ago
func (this *BufferedSegmentWriter) Flush() {
9 years ago
this.Lock()
defer this.Unlock()
if this.buffer.Len() == 0 {
return
}
this.FlushWithoutLock()
}
type AuthenticationWriter struct {
Authenticator internet.Authenticator
Writer io.Writer
}
func (this *AuthenticationWriter) Write(payload *alloc.Buffer) error {
defer payload.Release()
this.Authenticator.Seal(payload)
_, err := this.Writer.Write(payload.Value)
return err
}
func (this *AuthenticationWriter) Release() {}
func (this *AuthenticationWriter) Mtu() uint32 {
return effectiveConfig.Mtu.GetValue() - uint32(this.Authenticator.Overhead())
}