github.com/haraldrudell/parl@v0.4.176/pio/net-conn-tap.go (about) 1 /* 2 © 2023–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/) 3 ISC License 4 */ 5 6 package pio 7 8 import ( 9 "io" 10 "net" 11 12 "github.com/haraldrudell/parl" 13 ) 14 15 type NetConnTap struct { 16 net.Conn 17 tap *Tap 18 } 19 20 // NewNetConnTap returns a data tap for a bidirectional data stream 21 // - data from readWriter.Read is written to reads.Write if non-nil 22 // - data written to readWriter.Write is written to writes.Write if non-nil 23 // - a non-nil errs receives all errors from delegated Read Write reads and writes 24 // - if errs is nil, an error from the reads and writes taps is panic 25 // - ReadWriterTap impements idempotent Close 26 // - if any of readWriter, reads or writes implements io.Close, they are closed on socketTap.Close 27 // - the consumer may invoke socketTap.Close to ensure reads and writes are closed 28 // - errors in reads or writes do not affect the socketTap consumer 29 func NewNetConnTap(conn net.Conn, readsWriter, writesWriter io.Writer, addError parl.AddError) (socketTap io.ReadWriter) { 30 if conn == nil { 31 panic(parl.NilError("readWriter")) 32 } 33 socketTap = &NetConnTap{ 34 Conn: conn, 35 tap: NewTap(readsWriter, writesWriter, addError), 36 } 37 return 38 } 39 40 func (t *NetConnTap) Read(p []byte) (n int, err error) { return t.tap.Read(t.Conn, p) } 41 42 func (t *NetConnTap) Write(p []byte) (n int, err error) { return t.tap.Write(t.Conn, p) } 43 44 func (t *NetConnTap) Close() (err error) { return t.tap.Close(t.Conn) }