github.com/imannamdari/v2ray-core/v5@v5.0.5/transport/internet/udp/copy.go (about) 1 package udp 2 3 import ( 4 gonet "net" 5 6 "github.com/imannamdari/v2ray-core/v5/common/signal" 7 "github.com/imannamdari/v2ray-core/v5/transport/internet" 8 ) 9 10 type dataHandler func(content []byte, address gonet.Addr) 11 12 type copyHandler struct { 13 onData []dataHandler 14 } 15 16 type CopyOption func(*copyHandler) 17 18 func CopyPacketConn(dst internet.AbstractPacketConnWriter, src internet.AbstractPacketConnReader, options ...CopyOption) error { 19 var handler copyHandler 20 for _, option := range options { 21 option(&handler) 22 } 23 var buffer [2048]byte 24 for { 25 n, addr, err := src.ReadFrom(buffer[:]) 26 if err != nil { 27 return err 28 } 29 30 for _, handler := range handler.onData { 31 handler(buffer[:n], addr) 32 } 33 34 _, err = dst.WriteTo(buffer[:n], addr) 35 if err != nil { 36 return err 37 } 38 } 39 } 40 41 func UpdateActivity(timer signal.ActivityUpdater) CopyOption { 42 return func(handler *copyHandler) { 43 handler.onData = append(handler.onData, func(content []byte, address gonet.Addr) { 44 timer.Update() 45 }) 46 } 47 }