1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-09-27 22:36:12 -04:00

simplify state checking

This commit is contained in:
v2ray 2016-07-26 21:34:00 +02:00
parent 0040881b84
commit 6fe7463bf4
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

View File

@ -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)