github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/builtins/pipes/net/write.go (about)

     1  package net
     2  
     3  import (
     4  	"io"
     5  
     6  	"github.com/lmorg/murex/lang/stdio"
     7  	"github.com/lmorg/murex/utils"
     8  )
     9  
    10  // Write bytes to net Io interface
    11  func (n *Net) Write(b []byte) (i int, err error) {
    12  	select {
    13  	case <-n.ctx.Done():
    14  		return 0, io.ErrClosedPipe
    15  	default:
    16  	}
    17  
    18  	i, err = n.conn.Write(b)
    19  	n.mutex.Lock()
    20  	n.bWritten += uint64(i)
    21  	n.mutex.Unlock()
    22  	return
    23  }
    24  
    25  // Writeln writes a line to net Io interface
    26  func (n *Net) Writeln(b []byte) (i int, err error) {
    27  	i, err = n.conn.Write(append(b, utils.NewLineByte...))
    28  	n.mutex.Lock()
    29  	n.bWritten += uint64(i)
    30  	n.mutex.Unlock()
    31  	return
    32  }
    33  
    34  // WriteArray performs data type specific buffered writes to an stdio.Io interface
    35  func (n *Net) WriteArray(dataType string) (stdio.ArrayWriter, error) {
    36  	return stdio.WriteArray(n, dataType)
    37  }
    38  
    39  // WriteTo reads from net Io interface and then writes that to foreign Writer interface
    40  func (n *Net) WriteTo(dst io.Writer) (i int64, err error) {
    41  	i, err = io.Copy(dst, n.conn)
    42  	n.mutex.Lock()
    43  	n.bWritten += uint64(i)
    44  	n.mutex.Unlock()
    45  	return
    46  }