mirror of https://github.com/v2ray/v2ray-core
simplify state checking
parent
0040881b84
commit
6fe7463bf4
|
@ -21,6 +21,15 @@ var (
|
||||||
|
|
||||||
type State int32
|
type State int32
|
||||||
|
|
||||||
|
func (this State) Is(states ...State) bool {
|
||||||
|
for _, state := range states {
|
||||||
|
if this == state {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
StateActive State = 0
|
StateActive State = 0
|
||||||
StateReadyToClose State = 1
|
StateReadyToClose State = 1
|
||||||
|
@ -172,7 +181,7 @@ func (this *Connection) Read(b []byte) (int, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
if this.State() == StateReadyToClose || this.State() == StateTerminating || this.State() == StateTerminated {
|
if this.State().Is(StateReadyToClose, StateTerminating, StateTerminated) {
|
||||||
return 0, io.EOF
|
return 0, io.EOF
|
||||||
}
|
}
|
||||||
nBytes := this.receivingWorker.Read(b)
|
nBytes := this.receivingWorker.Read(b)
|
||||||
|
@ -206,9 +215,6 @@ func (this *Connection) Read(b []byte) (int, error) {
|
||||||
|
|
||||||
// Write implements the Conn Write method.
|
// Write implements the Conn Write method.
|
||||||
func (this *Connection) Write(b []byte) (int, error) {
|
func (this *Connection) Write(b []byte) (int, error) {
|
||||||
if this == nil || this.State() != StateActive {
|
|
||||||
return 0, io.ErrClosedPipe
|
|
||||||
}
|
|
||||||
totalWritten := 0
|
totalWritten := 0
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
@ -278,9 +284,7 @@ func (this *Connection) Close() error {
|
||||||
this.dataOutputCond.Broadcast()
|
this.dataOutputCond.Broadcast()
|
||||||
|
|
||||||
state := this.State()
|
state := this.State()
|
||||||
if state == StateReadyToClose ||
|
if state.Is(StateReadyToClose, StateTerminating, StateTerminated) {
|
||||||
state == StateTerminating ||
|
|
||||||
state == StateTerminated {
|
|
||||||
return errClosedConnection
|
return errClosedConnection
|
||||||
}
|
}
|
||||||
log.Info("KCP|Connection: Closing connection to ", this.remote)
|
log.Info("KCP|Connection: Closing connection to ", this.remote)
|
||||||
|
|
Loading…
Reference in New Issue