1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-16 17:38:45 -05: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 return size
} }
func (this *Config) GetSendingQueueSize() uint32 {
return this.WriteBuffer / this.Mtu
}
func (this *Config) GetReceivingWindowSize() uint32 { func (this *Config) GetReceivingWindowSize() uint32 {
size := this.DownlinkCapacity * 1024 * 1024 / this.Mtu / (1000 / this.Tti) / 2 size := this.DownlinkCapacity * 1024 * 1024 / this.Mtu / (1000 / this.Tti) / 2
if size == 0 { if size == 0 {
@ -30,6 +34,10 @@ func (this *Config) GetReceivingWindowSize() uint32 {
return size return size
} }
func (this *Config) GetReceivingQueueSize() uint32 {
return this.ReadBuffer / this.Mtu
}
func DefaultConfig() Config { func DefaultConfig() Config {
return Config{ return Config{
Mtu: 1350, Mtu: 1350,

View File

@ -40,18 +40,17 @@ func nowMillisec() int64 {
// Connection is a KCP connection over UDP. // Connection is a KCP connection over UDP.
type Connection struct { type Connection struct {
sync.RWMutex sync.RWMutex
state ConnState state ConnState
kcp *KCP // the core ARQ kcp *KCP // the core ARQ
kcpAccess sync.Mutex kcpAccess sync.Mutex
block Authenticator block Authenticator
needUpdate bool needUpdate bool
local, remote net.Addr local, remote net.Addr
wd time.Time // write deadline wd time.Time // write deadline
chReadEvent chan struct{} chReadEvent chan struct{}
writer io.WriteCloser writer io.WriteCloser
since int64 since int64
terminateOnce signal.Once terminateOnce signal.Once
writeBufferSize uint32
} }
// NewConnection create a new KCP connection between local and remote. // 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.block = block
conn.writer = writerCloser conn.writer = writerCloser
conn.since = nowMillisec() conn.since = nowMillisec()
conn.writeBufferSize = effectiveConfig.WriteBuffer / effectiveConfig.Mtu
authWriter := &AuthenticationWriter{ authWriter := &AuthenticationWriter{
Authenticator: block, Authenticator: block,
Writer: writerCloser, 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.NoDelay(effectiveConfig.Tti, 2, effectiveConfig.Congestion)
conn.kcp.current = conn.Elapsed() 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 // NewKCP create a new kcp control object, 'conv' must equal in two endpoint
// from the same connection. // 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) log.Debug("KCP|Core: creating KCP ", conv)
kcp := new(KCP) kcp := new(KCP)
kcp.conv = conv kcp.conv = conv
kcp.snd_wnd = sendingWindowSize kcp.snd_wnd = effectiveConfig.GetSendingWindowSize()
kcp.rcv_wnd = receivingWindowSize kcp.rcv_wnd = effectiveConfig.GetReceivingWindowSize()
kcp.rmt_wnd = IKCP_WND_RCV kcp.rmt_wnd = IKCP_WND_RCV
kcp.mss = output.Mtu() - DataSegmentOverhead kcp.mss = output.Mtu() - DataSegmentOverhead
kcp.rx_rto = IKCP_RTO_DEF kcp.rx_rto = IKCP_RTO_DEF
kcp.interval = IKCP_INTERVAL kcp.interval = IKCP_INTERVAL
kcp.output = NewSegmentWriter(output) kcp.output = NewSegmentWriter(output)
kcp.rcv_buf = NewReceivingWindow(receivingWindowSize) kcp.rcv_buf = NewReceivingWindow(effectiveConfig.GetReceivingWindowSize())
kcp.snd_queue = NewSendingQueue(sendingQueueSize) kcp.snd_queue = NewSendingQueue(effectiveConfig.GetSendingQueueSize())
kcp.rcv_queue = NewReceivingQueue() kcp.rcv_queue = NewReceivingQueue()
kcp.acklist = NewACKList(kcp) kcp.acklist = NewACKList(kcp)
kcp.snd_buf = NewSendingWindow(kcp, sendingWindowSize) kcp.snd_buf = NewSendingWindow(kcp, effectiveConfig.GetSendingWindowSize())
kcp.cwnd = kcp.snd_wnd kcp.cwnd = kcp.snd_wnd
return kcp return kcp
} }

View File

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