1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-10 14:26:26 -05:00
v2fly/transport/internet/udp/copy.go
2022-01-03 15:55:18 +00:00

48 lines
978 B
Go

package udp
import (
gonet "net"
"github.com/v2fly/v2ray-core/v4/common/signal"
"github.com/v2fly/v2ray-core/v4/transport/internet"
)
type dataHandler func(content []byte, address gonet.Addr)
type copyHandler struct {
onData []dataHandler
}
type CopyOption func(*copyHandler)
func CopyPacketConn(dst internet.AbstractPacketConnWriter, src internet.AbstractPacketConnReader, options ...CopyOption) error {
var handler copyHandler
for _, option := range options {
option(&handler)
}
var buffer [2048]byte
for {
n, addr, err := src.ReadFrom(buffer[:])
if err != nil {
return err
}
for _, handler := range handler.onData {
handler(buffer[:n], addr)
}
n, err = dst.WriteTo(buffer[:n], addr)
if err != nil {
return err
}
}
}
func UpdateActivity(timer signal.ActivityUpdater) CopyOption {
return func(handler *copyHandler) {
handler.onData = append(handler.onData, func(content []byte, address gonet.Addr) {
timer.Update()
})
}
}