mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-07-23 18:34:14 +00:00
Update dispatcher.go
This commit is contained in:
parent
f3dbcbd179
commit
78caac2d77
1 changed files with 12 additions and 25 deletions
|
@ -24,15 +24,6 @@ type connEntry struct {
|
||||||
link *transport.Link
|
link *transport.Link
|
||||||
timer signal.ActivityUpdater
|
timer signal.ActivityUpdater
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
closed bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *connEntry) Close() error {
|
|
||||||
c.closed = true
|
|
||||||
c.cancel()
|
|
||||||
common.Interrupt(c.link.Reader)
|
|
||||||
common.Close(c.link.Writer)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Dispatcher struct {
|
type Dispatcher struct {
|
||||||
|
@ -54,7 +45,8 @@ func (v *Dispatcher) RemoveRay() {
|
||||||
v.Lock()
|
v.Lock()
|
||||||
defer v.Unlock()
|
defer v.Unlock()
|
||||||
if v.conn != nil {
|
if v.conn != nil {
|
||||||
v.conn.Close()
|
common.Interrupt(v.conn.link.Reader)
|
||||||
|
common.Close(v.conn.link.Writer)
|
||||||
v.conn = nil
|
v.conn = nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,32 +56,28 @@ func (v *Dispatcher) getInboundRay(ctx context.Context, dest net.Destination) (*
|
||||||
defer v.Unlock()
|
defer v.Unlock()
|
||||||
|
|
||||||
if v.conn != nil {
|
if v.conn != nil {
|
||||||
if v.conn.closed {
|
return v.conn, nil
|
||||||
v.conn = nil
|
|
||||||
} else {
|
|
||||||
return v.conn, nil
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
errors.LogInfo(ctx, "establishing new connection for ", dest)
|
errors.LogInfo(ctx, "establishing new connection for ", dest)
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(ctx)
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
|
removeRay := func() {
|
||||||
|
cancel()
|
||||||
|
v.RemoveRay()
|
||||||
|
}
|
||||||
|
timer := signal.CancelAfterInactivity(ctx, removeRay, time.Minute)
|
||||||
|
|
||||||
link, err := v.dispatcher.Dispatch(ctx, dest)
|
link, err := v.dispatcher.Dispatch(ctx, dest)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cancel()
|
|
||||||
return nil, errors.New("failed to dispatch request to ", dest).Base(err)
|
return nil, errors.New("failed to dispatch request to ", dest).Base(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
entry := &connEntry{
|
entry := &connEntry{
|
||||||
link: link,
|
link: link,
|
||||||
cancel: cancel,
|
timer: timer,
|
||||||
|
cancel: removeRay,
|
||||||
}
|
}
|
||||||
entryClose := func() {
|
|
||||||
entry.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
entry.timer = signal.CancelAfterInactivity(ctx, entryClose, time.Minute)
|
|
||||||
v.conn = entry
|
v.conn = entry
|
||||||
go handleInput(ctx, entry, dest, v.callback, v.callClose)
|
go handleInput(ctx, entry, dest, v.callback, v.callClose)
|
||||||
return entry, nil
|
return entry, nil
|
||||||
|
@ -108,7 +96,7 @@ func (v *Dispatcher) Dispatch(ctx context.Context, destination net.Destination,
|
||||||
if outputStream != nil {
|
if outputStream != nil {
|
||||||
if err := outputStream.WriteMultiBuffer(buf.MultiBuffer{payload}); err != nil {
|
if err := outputStream.WriteMultiBuffer(buf.MultiBuffer{payload}); err != nil {
|
||||||
errors.LogInfoInner(ctx, err, "failed to write first UDP payload")
|
errors.LogInfoInner(ctx, err, "failed to write first UDP payload")
|
||||||
conn.Close()
|
conn.cancel()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -116,7 +104,7 @@ func (v *Dispatcher) Dispatch(ctx context.Context, destination net.Destination,
|
||||||
|
|
||||||
func handleInput(ctx context.Context, conn *connEntry, dest net.Destination, callback ResponseCallback, callClose func() error) {
|
func handleInput(ctx context.Context, conn *connEntry, dest net.Destination, callback ResponseCallback, callClose func() error) {
|
||||||
defer func() {
|
defer func() {
|
||||||
conn.Close()
|
conn.cancel()
|
||||||
if callClose != nil {
|
if callClose != nil {
|
||||||
callClose()
|
callClose()
|
||||||
}
|
}
|
||||||
|
@ -148,7 +136,6 @@ func handleInput(ctx context.Context, conn *connEntry, dest net.Destination, cal
|
||||||
Payload: b,
|
Payload: b,
|
||||||
Source: dest,
|
Source: dest,
|
||||||
})
|
})
|
||||||
b.Release()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue