1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-01-02 23:47:07 -05:00

fix data input and output signal

This commit is contained in:
Darien Raymond 2016-12-08 22:50:12 +01:00
parent 40249dfacb
commit 41fcffbfab
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

View File

@ -174,8 +174,8 @@ type Connection struct {
rd time.Time
wd time.Time // write deadline
since int64
dataInputCond *sync.Cond
dataOutputCond *sync.Cond
dataInput chan bool
dataOutput chan bool
Config *Config
conv uint16
@ -207,8 +207,8 @@ func NewConnection(conv uint16, sysConn SystemConnection, recycler internal.Conn
conn: sysConn,
connRecycler: recycler,
since: nowMillisec(),
dataInputCond: sync.NewCond(new(sync.Mutex)),
dataOutputCond: sync.NewCond(new(sync.Mutex)),
dataInput: make(chan bool, 1),
dataOutput: make(chan bool, 1),
Config: config,
output: NewSegmentWriter(sysConn, config.GetMtu().GetValue()-uint32(sysConn.Overhead())),
mss: config.GetMtu().GetValue() - uint32(sysConn.Overhead()) - DataSegmentOverhead,
@ -247,6 +247,20 @@ func (v *Connection) Elapsed() uint32 {
return uint32(nowMillisec() - v.since)
}
func (v *Connection) OnDataInput() {
select {
case v.dataInput <- true:
default:
}
}
func (v *Connection) OnDataOutput() {
select {
case v.dataOutput <- true:
default:
}
}
// Read implements the Conn Read method.
func (v *Connection) Read(b []byte) (int, error) {
if v == nil {
@ -266,24 +280,22 @@ func (v *Connection) Read(b []byte) (int, error) {
return 0, io.EOF
}
var timer *time.Timer
duration := time.Duration(time.Minute)
if !v.rd.IsZero() {
duration := v.rd.Sub(time.Now())
if duration <= 0 {
duration = v.rd.Sub(time.Now())
if duration < 0 {
return 0, ErrIOTimeout
}
timer = time.AfterFunc(duration, v.dataInputCond.Signal)
}
v.dataInputCond.L.Lock()
v.dataInputCond.Wait()
v.dataInputCond.L.Unlock()
if timer != nil {
timer.Stop()
}
select {
case <-v.dataInput:
case <-time.After(duration):
if !v.rd.IsZero() && v.rd.Before(time.Now()) {
return 0, ErrIOTimeout
}
}
}
}
// Write implements the Conn Write method.
@ -304,26 +316,22 @@ func (v *Connection) Write(b []byte) (int, error) {
}
}
var timer *time.Timer
if !v.wd.IsZero() {
duration := v.wd.Sub(time.Now())
if duration <= 0 {
duration := time.Duration(time.Minute)
if !v.rd.IsZero() {
duration = v.wd.Sub(time.Now())
if duration < 0 {
return totalWritten, ErrIOTimeout
}
timer = time.AfterFunc(duration, v.dataOutputCond.Signal)
}
v.dataOutputCond.L.Lock()
v.dataOutputCond.Wait()
v.dataOutputCond.L.Unlock()
if timer != nil {
timer.Stop()
}
select {
case <-v.dataOutput:
case <-time.After(duration):
if !v.wd.IsZero() && v.wd.Before(time.Now()) {
return totalWritten, ErrIOTimeout
}
}
}
}
func (v *Connection) SetState(state State) {
@ -360,8 +368,8 @@ func (v *Connection) Close() error {
return ErrClosedConnection
}
v.dataInputCond.Broadcast()
v.dataOutputCond.Broadcast()
v.OnDataInput()
v.OnDataOutput()
state := v.State()
if state.Is(StateReadyToClose, StateTerminating, StateTerminated) {
@ -447,8 +455,9 @@ func (v *Connection) Terminate() {
log.Info("KCP|Connection: Terminating connection to ", v.RemoteAddr())
//v.SetState(StateTerminated)
v.dataInputCond.Broadcast()
v.dataOutputCond.Broadcast()
v.OnDataInput()
v.OnDataOutput()
if v.Config.ConnectionReuse.IsEnabled() && v.reusable {
v.connRecycler.Put(v.conn.Id(), v.conn)
} else {
@ -481,19 +490,21 @@ func (v *Connection) Input(segments []Segment) {
for _, seg := range segments {
if seg.Conversation() != v.conv {
return
break
}
switch seg := seg.(type) {
case *DataSegment:
v.HandleOption(seg.Option)
v.receivingWorker.ProcessSegment(seg)
v.dataInputCond.Signal()
if seg.Number == v.receivingWorker.nextNumber {
v.OnDataInput()
}
v.dataUpdater.WakeUp()
case *AckSegment:
v.HandleOption(seg.Option)
v.sendingWorker.ProcessSegment(current, seg, v.roundTrip.Timeout())
v.dataOutputCond.Signal()
v.OnDataOutput()
v.dataUpdater.WakeUp()
case *CmdOnlySegment:
v.HandleOption(seg.Option)