tractor.dev/toolkit-go@v0.0.0-20241010005851-214d91207d07/duplex/mux/frame/encoder.go (about) 1 package frame 2 3 import ( 4 "fmt" 5 "io" 6 "sync" 7 ) 8 9 // Encoder encodes messages given an io.Writer 10 type Encoder struct { 11 w io.Writer 12 sync.Mutex 13 } 14 15 func NewEncoder(w io.Writer) *Encoder { 16 return &Encoder{w: w} 17 } 18 19 func (enc *Encoder) Encode(msg Message) error { 20 enc.Lock() 21 defer enc.Unlock() 22 23 _, err := enc.w.Write(msg.Bytes()) 24 25 if Debug != nil { 26 fmt.Fprintln(Debug, "<<ENC", msg) 27 } 28 return err 29 }