1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 18:17:52 -05:00

cleanup kcp connection

This commit is contained in:
Darien Raymond 2017-12-14 23:02:10 +01:00
parent bc9267846c
commit 0032760fdc
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

View File

@ -38,7 +38,7 @@ const (
StatePeerClosed State = 2 // Connection is closed on remote StatePeerClosed State = 2 // Connection is closed on remote
StateTerminating State = 3 // Connection is ready to be destroyed locally StateTerminating State = 3 // Connection is ready to be destroyed locally
StatePeerTerminating State = 4 // Connection is ready to be destroyed on remote StatePeerTerminating State = 4 // Connection is ready to be destroyed on remote
StateTerminated State = 5 // Connection is detroyed. StateTerminated State = 5 // Connection is destroyed.
) )
func nowMillisec() int64 { func nowMillisec() int64 {
@ -137,21 +137,21 @@ func NewUpdater(interval uint32, shouldContinue predicate.Predicate, shouldTermi
return u return u
} }
func (v *Updater) WakeUp() { func (u *Updater) WakeUp() {
select { select {
case v.notifier <- true: case u.notifier <- true:
default: default:
} }
} }
func (v *Updater) Run() { func (u *Updater) Run() {
for <-v.notifier { for <-u.notifier {
if v.shouldTerminate() { if u.shouldTerminate() {
return return
} }
interval := v.Interval() interval := u.Interval()
for v.shouldContinue() { for u.shouldContinue() {
v.updateFunc() u.updateFunc()
time.Sleep(interval) time.Sleep(interval)
} }
} }
@ -280,24 +280,36 @@ func (v *Connection) ReadMultiBuffer() (buf.MultiBuffer, error) {
return nil, io.EOF return nil, io.EOF
} }
duration := time.Minute if err := v.waitForDataInput(); err != nil {
if !v.rd.IsZero() { return nil, err
duration = time.Until(v.rd)
if duration < 0 {
return nil, ErrIOTimeout
}
}
select {
case <-v.dataInput:
case <-time.After(duration):
if !v.rd.IsZero() && v.rd.Before(time.Now()) {
return nil, ErrIOTimeout
}
} }
} }
} }
func (v *Connection) waitForDataInput() error {
if v.State() == StatePeerTerminating {
return io.EOF
}
duration := time.Minute
if !v.rd.IsZero() {
duration = time.Until(v.rd)
if duration < 0 {
return ErrIOTimeout
}
}
select {
case <-v.dataInput:
case <-time.After(duration):
if !v.rd.IsZero() && v.rd.Before(time.Now()) {
return ErrIOTimeout
}
}
return nil
}
// Read implements the Conn Read method. // Read implements the Conn Read method.
func (v *Connection) Read(b []byte) (int, error) { func (v *Connection) Read(b []byte) (int, error) {
if v == nil { if v == nil {
@ -313,28 +325,32 @@ func (v *Connection) Read(b []byte) (int, error) {
return nBytes, nil return nBytes, nil
} }
if v.State() == StatePeerTerminating { if err := v.waitForDataInput(); err != nil {
return 0, io.EOF return 0, err
}
duration := time.Minute
if !v.rd.IsZero() {
duration = time.Until(v.rd)
if duration < 0 {
return 0, ErrIOTimeout
}
}
select {
case <-v.dataInput:
case <-time.After(duration):
if !v.rd.IsZero() && v.rd.Before(time.Now()) {
return 0, ErrIOTimeout
}
} }
} }
} }
func (v *Connection) waitForDataOutput() error {
duration := time.Minute
if !v.wd.IsZero() {
duration = time.Until(v.wd)
if duration < 0 {
return ErrIOTimeout
}
}
select {
case <-v.dataOutput:
case <-time.After(duration):
if !v.wd.IsZero() && v.wd.Before(time.Now()) {
return ErrIOTimeout
}
}
return nil
}
// Write implements io.Writer. // Write implements io.Writer.
func (v *Connection) Write(b []byte) (int, error) { func (v *Connection) Write(b []byte) (int, error) {
totalWritten := 0 totalWritten := 0
@ -359,20 +375,8 @@ func (v *Connection) Write(b []byte) (int, error) {
} }
} }
duration := time.Minute if err := v.waitForDataOutput(); err != nil {
if !v.wd.IsZero() { return totalWritten, err
duration = time.Until(v.wd)
if duration < 0 {
return totalWritten, ErrIOTimeout
}
}
select {
case <-v.dataOutput:
case <-time.After(duration):
if !v.wd.IsZero() && v.wd.Before(time.Now()) {
return totalWritten, ErrIOTimeout
}
} }
} }
} }
@ -400,20 +404,8 @@ func (v *Connection) WriteMultiBuffer(mb buf.MultiBuffer) error {
} }
} }
duration := time.Minute if err := v.waitForDataOutput(); err != nil {
if !v.wd.IsZero() { return err
duration = time.Until(v.wd)
if duration < 0 {
return ErrIOTimeout
}
}
select {
case <-v.dataOutput:
case <-time.After(duration):
if !v.wd.IsZero() && v.wd.Before(time.Now()) {
return ErrIOTimeout
}
} }
} }
} }