2017-06-04 15:32:01 -04:00
|
|
|
package buf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2018-06-27 06:21:22 -04:00
|
|
|
"time"
|
2017-06-04 15:32:01 -04:00
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
"github.com/v2fly/v2ray-core/v4/common/errors"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common/signal"
|
2017-06-04 15:32:01 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type dataHandler func(MultiBuffer)
|
|
|
|
|
|
|
|
type copyHandler struct {
|
2018-11-15 13:59:23 -05:00
|
|
|
onData []dataHandler
|
2017-06-04 15:32:01 -04:00
|
|
|
}
|
|
|
|
|
2018-02-13 05:15:04 -05:00
|
|
|
// SizeCounter is for counting bytes copied by Copy().
|
2017-11-09 16:33:15 -05:00
|
|
|
type SizeCounter struct {
|
|
|
|
Size int64
|
|
|
|
}
|
|
|
|
|
2017-11-21 11:02:55 -05:00
|
|
|
// CopyOption is an option for copying data.
|
2017-06-04 15:32:01 -04:00
|
|
|
type CopyOption func(*copyHandler)
|
|
|
|
|
2017-11-21 11:02:55 -05:00
|
|
|
// UpdateActivity is a CopyOption to update activity on each data copy operation.
|
2017-09-27 09:29:00 -04:00
|
|
|
func UpdateActivity(timer signal.ActivityUpdater) CopyOption {
|
2017-06-04 15:32:01 -04:00
|
|
|
return func(handler *copyHandler) {
|
|
|
|
handler.onData = append(handler.onData, func(MultiBuffer) {
|
|
|
|
timer.Update()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 11:02:55 -05:00
|
|
|
// CountSize is a CopyOption that sums the total size of data copied into the given SizeCounter.
|
2017-11-09 16:33:15 -05:00
|
|
|
func CountSize(sc *SizeCounter) CopyOption {
|
|
|
|
return func(handler *copyHandler) {
|
|
|
|
handler.onData = append(handler.onData, func(b MultiBuffer) {
|
|
|
|
sc.Size += int64(b.Len())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-24 06:02:02 -04:00
|
|
|
type readError struct {
|
|
|
|
error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e readError) Error() string {
|
|
|
|
return e.error.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e readError) Inner() error {
|
|
|
|
return e.error
|
|
|
|
}
|
|
|
|
|
2019-02-10 04:53:06 -05:00
|
|
|
// IsReadError returns true if the error in Copy() comes from reading.
|
2018-10-24 06:02:02 -04:00
|
|
|
func IsReadError(err error) bool {
|
|
|
|
_, ok := err.(readError)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
type writeError struct {
|
|
|
|
error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e writeError) Error() string {
|
|
|
|
return e.error.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e writeError) Inner() error {
|
|
|
|
return e.error
|
|
|
|
}
|
|
|
|
|
2019-02-10 04:53:06 -05:00
|
|
|
// IsWriteError returns true if the error in Copy() comes from writing.
|
2018-10-24 06:02:02 -04:00
|
|
|
func IsWriteError(err error) bool {
|
|
|
|
_, ok := err.(writeError)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2017-06-04 15:32:01 -04:00
|
|
|
func copyInternal(reader Reader, writer Writer, handler *copyHandler) error {
|
|
|
|
for {
|
2018-11-15 13:59:23 -05:00
|
|
|
buffer, err := reader.ReadMultiBuffer()
|
2017-11-24 17:16:01 -05:00
|
|
|
if !buffer.IsEmpty() {
|
|
|
|
for _, handler := range handler.onData {
|
|
|
|
handler(buffer)
|
|
|
|
}
|
|
|
|
|
2018-11-15 13:59:23 -05:00
|
|
|
if werr := writer.WriteMultiBuffer(buffer); werr != nil {
|
2018-10-24 06:02:02 -04:00
|
|
|
return writeError{werr}
|
2017-11-24 17:16:01 -05:00
|
|
|
}
|
2018-02-19 11:50:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2018-10-24 06:02:02 -04:00
|
|
|
return readError{err}
|
2017-06-04 15:32:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-21 11:02:55 -05:00
|
|
|
// Copy dumps all payload from reader to writer or stops when an error occurs. It returns nil when EOF.
|
2017-06-04 15:32:01 -04:00
|
|
|
func Copy(reader Reader, writer Writer, options ...CopyOption) error {
|
2018-08-09 07:30:44 -04:00
|
|
|
var handler copyHandler
|
2017-06-04 15:32:01 -04:00
|
|
|
for _, option := range options {
|
2018-11-15 15:16:54 -05:00
|
|
|
option(&handler)
|
2017-06-04 15:32:01 -04:00
|
|
|
}
|
2018-11-15 15:16:54 -05:00
|
|
|
err := copyInternal(reader, writer, &handler)
|
2017-06-04 15:32:01 -04:00
|
|
|
if err != nil && errors.Cause(err) != io.EOF {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-06-27 06:21:22 -04:00
|
|
|
|
|
|
|
var ErrNotTimeoutReader = newError("not a TimeoutReader")
|
|
|
|
|
|
|
|
func CopyOnceTimeout(reader Reader, writer Writer, timeout time.Duration) error {
|
|
|
|
timeoutReader, ok := reader.(TimeoutReader)
|
|
|
|
if !ok {
|
|
|
|
return ErrNotTimeoutReader
|
|
|
|
}
|
|
|
|
mb, err := timeoutReader.ReadMultiBufferTimeout(timeout)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return writer.WriteMultiBuffer(mb)
|
|
|
|
}
|