mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-03 07:56:42 -05:00
introduce a new state: peer terminating
This commit is contained in:
parent
05ed05b9a0
commit
476b3c68d2
@ -22,11 +22,12 @@ var (
|
|||||||
type State int32
|
type State int32
|
||||||
|
|
||||||
const (
|
const (
|
||||||
StateActive State = 0
|
StateActive State = 0
|
||||||
StateReadyToClose State = 1
|
StateReadyToClose State = 1
|
||||||
StatePeerClosed State = 2
|
StatePeerClosed State = 2
|
||||||
StateTerminating State = 3
|
StateTerminating State = 3
|
||||||
StateTerminated State = 4
|
StatePeerTerminating State = 4
|
||||||
|
StateTerminated State = 5
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -177,6 +178,10 @@ func (this *Connection) Read(b []byte) (int, error) {
|
|||||||
return nBytes, nil
|
return nBytes, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if this.State() == StatePeerTerminating {
|
||||||
|
return 0, io.EOF
|
||||||
|
}
|
||||||
|
|
||||||
var timer *time.Timer
|
var timer *time.Timer
|
||||||
if !this.rd.IsZero() {
|
if !this.rd.IsZero() {
|
||||||
duration := this.rd.Sub(time.Now())
|
duration := this.rd.Sub(time.Now())
|
||||||
@ -240,6 +245,8 @@ func (this *Connection) SetState(state State) {
|
|||||||
case StateTerminating:
|
case StateTerminating:
|
||||||
this.receivingWorker.CloseRead()
|
this.receivingWorker.CloseRead()
|
||||||
this.sendingWorker.CloseWrite()
|
this.sendingWorker.CloseWrite()
|
||||||
|
case StatePeerTerminating:
|
||||||
|
this.sendingWorker.CloseWrite()
|
||||||
case StateTerminated:
|
case StateTerminated:
|
||||||
this.receivingWorker.CloseRead()
|
this.receivingWorker.CloseRead()
|
||||||
this.sendingWorker.CloseWrite()
|
this.sendingWorker.CloseWrite()
|
||||||
@ -268,6 +275,9 @@ func (this *Connection) Close() error {
|
|||||||
if state == StatePeerClosed {
|
if state == StatePeerClosed {
|
||||||
this.SetState(StateTerminating)
|
this.SetState(StateTerminating)
|
||||||
}
|
}
|
||||||
|
if state == StatePeerTerminating {
|
||||||
|
this.SetState(StateTerminated)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -405,8 +415,9 @@ func (this *Connection) Input(data []byte) int {
|
|||||||
if seg.Cmd == SegmentCommandTerminated {
|
if seg.Cmd == SegmentCommandTerminated {
|
||||||
state := this.State()
|
state := this.State()
|
||||||
if state == StateActive ||
|
if state == StateActive ||
|
||||||
state == StateReadyToClose ||
|
|
||||||
state == StatePeerClosed {
|
state == StatePeerClosed {
|
||||||
|
this.SetState(StatePeerTerminating)
|
||||||
|
} else if state == StateReadyToClose {
|
||||||
this.SetState(StateTerminating)
|
this.SetState(StateTerminating)
|
||||||
} else if state == StateTerminating {
|
} else if state == StateTerminating {
|
||||||
this.SetState(StateTerminated)
|
this.SetState(StateTerminated)
|
||||||
@ -450,6 +461,9 @@ func (this *Connection) flush() {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if this.State() == StatePeerTerminating && current-atomic.LoadUint32(&this.stateBeginTime) > 4000 {
|
||||||
|
this.SetState(StateTerminating)
|
||||||
|
}
|
||||||
|
|
||||||
if this.State() == StateReadyToClose && current-atomic.LoadUint32(&this.stateBeginTime) > 15000 {
|
if this.State() == StateReadyToClose && current-atomic.LoadUint32(&this.stateBeginTime) > 15000 {
|
||||||
this.SetState(StateTerminating)
|
this.SetState(StateTerminating)
|
||||||
|
@ -59,10 +59,17 @@ func (this *Listener) OnReceive(payload *alloc.Buffer, src v2net.Destination) {
|
|||||||
if !this.running {
|
if !this.running {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if payload.Len() < 4 {
|
||||||
|
return
|
||||||
|
}
|
||||||
conv := serial.BytesToUint16(payload.Value)
|
conv := serial.BytesToUint16(payload.Value)
|
||||||
|
cmd := SegmentCommand(payload.Value[2])
|
||||||
sourceId := src.NetAddr() + "|" + serial.Uint16ToString(conv)
|
sourceId := src.NetAddr() + "|" + serial.Uint16ToString(conv)
|
||||||
conn, found := this.sessions[sourceId]
|
conn, found := this.sessions[sourceId]
|
||||||
if !found {
|
if !found {
|
||||||
|
if cmd == SegmentCommandTerminated {
|
||||||
|
return
|
||||||
|
}
|
||||||
log.Debug("KCP|Listener: Creating session with id(", sourceId, ") from ", src)
|
log.Debug("KCP|Listener: Creating session with id(", sourceId, ") from ", src)
|
||||||
writer := &Writer{
|
writer := &Writer{
|
||||||
id: sourceId,
|
id: sourceId,
|
||||||
|
Loading…
Reference in New Issue
Block a user