github.com/haraldrudell/parl@v0.4.176/pio/tls-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  	"crypto/tls"
    10  	"io"
    11  
    12  	"github.com/haraldrudell/parl"
    13  )
    14  
    15  type TLSTap struct {
    16  	*tls.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 NewTLSTap(conn *tls.Conn, readsWriter, writesWriter io.Writer, addError parl.AddError) (socketTap io.ReadWriter) {
    30  	if conn == nil {
    31  		panic(parl.NilError("conn"))
    32  	}
    33  	socketTap = &TLSTap{
    34  		Conn: conn,
    35  		tap:  NewTap(readsWriter, writesWriter, addError),
    36  	}
    37  	return
    38  }
    39  
    40  func (t *TLSTap) Read(p []byte) (n int, err error) { return t.tap.Read(t.Conn, p) }
    41  
    42  func (t *TLSTap) Write(p []byte) (n int, err error) { return t.tap.Write(t.Conn, p) }
    43  
    44  func (t *TLSTap) Close() (err error) { return t.tap.Close(t.Conn) }