diff --git a/common/buf/copy.go b/common/buf/copy.go index 87a9518e..c1721eb5 100644 --- a/common/buf/copy.go +++ b/common/buf/copy.go @@ -8,6 +8,11 @@ import ( "v2ray.com/core/common/signal" ) +// ActivityNotifiable is a object that accepts activity notification outside the object +type ActivityNotifiable interface { + NotifyActivity() error +} + type dataHandler func(MultiBuffer) type copyHandler struct { @@ -31,6 +36,15 @@ func UpdateActivity(timer signal.ActivityUpdater) CopyOption { } } +// NotifyActivity is a CopyOption to notify activity on each data copy operation. +func NotifyActivity(notifier ActivityNotifiable) CopyOption { + return func(handler *copyHandler) { + handler.onData = append(handler.onData, func(MultiBuffer) { + notifier.NotifyActivity() + }) + } +} + // CountSize is a CopyOption that sums the total size of data copied into the given SizeCounter. func CountSize(sc *SizeCounter) CopyOption { return func(handler *copyHandler) {