github.com/zooyer/miskit@v1.0.71/ssh/io.go (about) 1 package ssh 2 3 import "io" 4 5 type reader struct { 6 size int 7 onRead func(int) 8 reader io.Reader 9 } 10 11 type writer struct { 12 size int 13 onWrite func(int) 14 writer io.Writer 15 } 16 17 func newReader(r io.Reader, onRead func(size int)) *reader { 18 return &reader{ 19 size: 0, 20 onRead: onRead, 21 reader: r, 22 } 23 } 24 25 func newWriter(w io.Writer, onWrite func(size int)) *writer { 26 return &writer{ 27 size: 0, 28 onWrite: onWrite, 29 writer: w, 30 } 31 } 32 33 func (r *reader) Read(buf []byte) (n int, err error) { 34 if n, err = r.reader.Read(buf); err != nil { 35 return 36 } 37 r.size += n 38 if r.onRead != nil { 39 r.onRead(r.size) 40 } 41 return 42 } 43 44 func (w *writer) Write(buf []byte) (n int, err error) { 45 if n, err = w.writer.Write(buf); err != nil { 46 return 47 } 48 w.size += n 49 if w.onWrite != nil { 50 w.onWrite(w.size) 51 } 52 return 53 }