1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-28 23:06:14 -04:00

refine kcp constructor

This commit is contained in:
v2ray 2016-07-02 11:19:32 +02:00
parent a2abdc3d2f
commit 94fb16fdfa
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
4 changed files with 27 additions and 21 deletions

View File

@ -22,6 +22,10 @@ func (this *Config) GetSendingWindowSize() uint32 {
return size
}
func (this *Config) GetSendingQueueSize() uint32 {
return this.WriteBuffer / this.Mtu
}
func (this *Config) GetReceivingWindowSize() uint32 {
size := this.DownlinkCapacity * 1024 * 1024 / this.Mtu / (1000 / this.Tti) / 2
if size == 0 {
@ -30,6 +34,10 @@ func (this *Config) GetReceivingWindowSize() uint32 {
return size
}
func (this *Config) GetReceivingQueueSize() uint32 {
return this.ReadBuffer / this.Mtu
}
func DefaultConfig() Config {
return Config{
Mtu: 1350,

View File

@ -40,18 +40,17 @@ func nowMillisec() int64 {
// Connection is a KCP connection over UDP.
type Connection struct {
sync.RWMutex
state ConnState
kcp *KCP // the core ARQ
kcpAccess sync.Mutex
block Authenticator
needUpdate bool
local, remote net.Addr
wd time.Time // write deadline
chReadEvent chan struct{}
writer io.WriteCloser
since int64
terminateOnce signal.Once
writeBufferSize uint32
state ConnState
kcp *KCP // the core ARQ
kcpAccess sync.Mutex
block Authenticator
needUpdate bool
local, remote net.Addr
wd time.Time // write deadline
chReadEvent chan struct{}
writer io.WriteCloser
since int64
terminateOnce signal.Once
}
// NewConnection create a new KCP connection between local and remote.
@ -63,13 +62,12 @@ func NewConnection(conv uint16, writerCloser io.WriteCloser, local *net.UDPAddr,
conn.block = block
conn.writer = writerCloser
conn.since = nowMillisec()
conn.writeBufferSize = effectiveConfig.WriteBuffer / effectiveConfig.Mtu
authWriter := &AuthenticationWriter{
Authenticator: block,
Writer: writerCloser,
}
conn.kcp = NewKCP(conv, effectiveConfig.GetSendingWindowSize(), effectiveConfig.GetReceivingWindowSize(), conn.writeBufferSize, authWriter)
conn.kcp = NewKCP(conv, authWriter)
conn.kcp.NoDelay(effectiveConfig.Tti, 2, effectiveConfig.Congestion)
conn.kcp.current = conn.Elapsed()

View File

@ -65,22 +65,22 @@ type KCP struct {
// NewKCP create a new kcp control object, 'conv' must equal in two endpoint
// from the same connection.
func NewKCP(conv uint16, sendingWindowSize uint32, receivingWindowSize uint32, sendingQueueSize uint32, output *AuthenticationWriter) *KCP {
func NewKCP(conv uint16, output *AuthenticationWriter) *KCP {
log.Debug("KCP|Core: creating KCP ", conv)
kcp := new(KCP)
kcp.conv = conv
kcp.snd_wnd = sendingWindowSize
kcp.rcv_wnd = receivingWindowSize
kcp.snd_wnd = effectiveConfig.GetSendingWindowSize()
kcp.rcv_wnd = effectiveConfig.GetReceivingWindowSize()
kcp.rmt_wnd = IKCP_WND_RCV
kcp.mss = output.Mtu() - DataSegmentOverhead
kcp.rx_rto = IKCP_RTO_DEF
kcp.interval = IKCP_INTERVAL
kcp.output = NewSegmentWriter(output)
kcp.rcv_buf = NewReceivingWindow(receivingWindowSize)
kcp.snd_queue = NewSendingQueue(sendingQueueSize)
kcp.rcv_buf = NewReceivingWindow(effectiveConfig.GetReceivingWindowSize())
kcp.snd_queue = NewSendingQueue(effectiveConfig.GetSendingQueueSize())
kcp.rcv_queue = NewReceivingQueue()
kcp.acklist = NewACKList(kcp)
kcp.snd_buf = NewSendingWindow(kcp, sendingWindowSize)
kcp.snd_buf = NewSendingWindow(kcp, effectiveConfig.GetSendingWindowSize())
kcp.cwnd = kcp.snd_wnd
return kcp
}

View File

@ -67,7 +67,7 @@ type ReceivingQueue struct {
func NewReceivingQueue() *ReceivingQueue {
return &ReceivingQueue{
queue: make(chan *alloc.Buffer, effectiveConfig.ReadBuffer/effectiveConfig.Mtu),
queue: make(chan *alloc.Buffer, effectiveConfig.GetReceivingQueueSize()),
}
}