Skip to content

Commit

Permalink
Fix #2654 (#2941)
Browse files Browse the repository at this point in the history
* fix udp dispatcher

* fix test
  • Loading branch information
dyhkwong authored Jan 15, 2024
1 parent 77376ed commit da5a28a
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions transport/internet/udp/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,43 @@ type connEntry struct {

type Dispatcher struct {
sync.RWMutex
conns map[net.Destination]*connEntry
conn *connEntry
dispatcher routing.Dispatcher
callback ResponseCallback
callClose func() error
}

func NewDispatcher(dispatcher routing.Dispatcher, callback ResponseCallback) *Dispatcher {
return &Dispatcher{
conns: make(map[net.Destination]*connEntry),
dispatcher: dispatcher,
callback: callback,
}
}

func (v *Dispatcher) RemoveRay(dest net.Destination) {
func (v *Dispatcher) RemoveRay() {
v.Lock()
defer v.Unlock()
if conn, found := v.conns[dest]; found {
common.Close(conn.link.Reader)
common.Close(conn.link.Writer)
delete(v.conns, dest)
if v.conn != nil {
common.Close(v.conn.link.Reader)
common.Close(v.conn.link.Writer)
v.conn = nil
}
}

func (v *Dispatcher) getInboundRay(ctx context.Context, dest net.Destination) (*connEntry, error) {
v.Lock()
defer v.Unlock()

if entry, found := v.conns[dest]; found {
return entry, nil
if v.conn != nil {
return v.conn, nil
}

newError("establishing new connection for ", dest).WriteToLog()

ctx, cancel := context.WithCancel(ctx)
removeRay := func() {
cancel()
v.RemoveRay(dest)
v.RemoveRay()
}
timer := signal.CancelAfterInactivity(ctx, removeRay, time.Minute)

Expand All @@ -79,7 +78,7 @@ func (v *Dispatcher) getInboundRay(ctx context.Context, dest net.Destination) (*
timer: timer,
cancel: removeRay,
}
v.conns[dest] = entry
v.conn = entry
go handleInput(ctx, entry, dest, v.callback, v.callClose)
return entry, nil
}
Expand Down Expand Up @@ -130,6 +129,9 @@ func handleInput(ctx context.Context, conn *connEntry, dest net.Destination, cal
}
timer.Update()
for _, b := range mb {
if b.UDP != nil {
dest = *b.UDP
}
callback(ctx, &udp.Packet{
Payload: b,
Source: dest,
Expand All @@ -153,7 +155,6 @@ func DialDispatcher(ctx context.Context, dispatcher routing.Dispatcher) (net.Pac
}

d := &Dispatcher{
conns: make(map[net.Destination]*connEntry),
dispatcher: dispatcher,
callback: c.callback,
callClose: c.Close,
Expand Down Expand Up @@ -199,7 +200,9 @@ func (c *dispatcherConn) WriteTo(p []byte, addr net.Addr) (int, error) {
n := copy(raw, p)
buffer.Resize(0, int32(n))

c.dispatcher.Dispatch(c.ctx, net.DestinationFromAddr(addr), buffer)
destination := net.DestinationFromAddr(addr)
buffer.UDP = &destination
c.dispatcher.Dispatch(c.ctx, destination, buffer)
return n, nil
}

Expand Down

0 comments on commit da5a28a

Please sign in to comment.