package xmpp import ( "crypto/tls" "encoding/xml" "log" "os" "io" "errors" ) type decoder struct { terminator chan bool data chan xml.Token decoder *xml.Decoder debug *xml.Encoder } func newDecoder(conn *tls.Conn) decoder { d := decoder{ make(chan bool), make(chan xml.Token), xml.NewDecoder(conn), xml.NewEncoder(os.Stdout), } d.debug.Indent("S: ", " ") return d } func (r *decoder) run() { for { select { case <-r.terminator: return default: t, err := r.decoder.Token() if t != nil && err == nil { err := r.debug.EncodeToken(t) if err != nil { log.Println("Could not encode received XML token to debug output!") } err = r.debug.Flush() if err != nil { log.Println("Could not flush debug output after receive!") } r.data <- t } if err != nil { if errors.Is(err, io.EOF) { return } else { log.Printf("Decoding error: %v", err) } } } } } func (r *decoder) stop() { r.terminator <- true }