From cf520b079743ec95d085a439d00b841c253c564a Mon Sep 17 00:00:00 2001 From: xengineering Date: Mon, 3 Jul 2023 21:10:51 +0200 Subject: Rename encoder_decoder.go to xml.go This is way shorter and serves the same purpose: It reflects the responsibility of encoding and decoding XML. The encoderDecoder struct should be removed soon. --- xmpp/encoder_decoder.go | 80 ------------------------------------------------- xmpp/xml.go | 80 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 80 deletions(-) delete mode 100644 xmpp/encoder_decoder.go create mode 100644 xmpp/xml.go diff --git a/xmpp/encoder_decoder.go b/xmpp/encoder_decoder.go deleted file mode 100644 index b0ea77b..0000000 --- a/xmpp/encoder_decoder.go +++ /dev/null @@ -1,80 +0,0 @@ -package xmpp - -import ( - "encoding/xml" - "errors" - "io" - "log" -) - -type encoderDecoder struct { - session *session - tx *xml.Encoder - rx *xml.Decoder - terminator chan bool -} - -func newEncoderDecoder(s *session) encoderDecoder { - ed := encoderDecoder{} - - ed.session = s - - lw := logger{"[TX] "} - w := io.MultiWriter(s.transport, lw) - ed.tx = xml.NewEncoder(w) - ed.tx.Indent("", "") - - lr := logger{"[RX] "} - r := io.TeeReader(s.transport, lr) - ed.rx = xml.NewDecoder(r) - - return ed -} - -func (ed *encoderDecoder) encodeToken(t xml.Token) error { - var err error - defer func() { - if err != nil { - log.Println(err) - } - }() - - err = ed.tx.EncodeToken(t) - if err != nil { - return err - } - err = ed.tx.Flush() - if err != nil { - return err - } - - return nil -} - -func (ed *encoderDecoder) run() { - for { - select { - case <-ed.terminator: - return - default: - t, err := ed.rx.Token() - if t != nil && err == nil { - switch t.(type) { - case xml.ProcInst: - case xml.Directive: - case xml.Comment: - default: - c := xml.CopyToken(t) - ed.session.rx <- c - } - } - if err != nil { - if errors.Is(err, io.EOF) { - return - } - log.Println(err) // FIXME terminate session on error - return - } - } - } -} diff --git a/xmpp/xml.go b/xmpp/xml.go new file mode 100644 index 0000000..b0ea77b --- /dev/null +++ b/xmpp/xml.go @@ -0,0 +1,80 @@ +package xmpp + +import ( + "encoding/xml" + "errors" + "io" + "log" +) + +type encoderDecoder struct { + session *session + tx *xml.Encoder + rx *xml.Decoder + terminator chan bool +} + +func newEncoderDecoder(s *session) encoderDecoder { + ed := encoderDecoder{} + + ed.session = s + + lw := logger{"[TX] "} + w := io.MultiWriter(s.transport, lw) + ed.tx = xml.NewEncoder(w) + ed.tx.Indent("", "") + + lr := logger{"[RX] "} + r := io.TeeReader(s.transport, lr) + ed.rx = xml.NewDecoder(r) + + return ed +} + +func (ed *encoderDecoder) encodeToken(t xml.Token) error { + var err error + defer func() { + if err != nil { + log.Println(err) + } + }() + + err = ed.tx.EncodeToken(t) + if err != nil { + return err + } + err = ed.tx.Flush() + if err != nil { + return err + } + + return nil +} + +func (ed *encoderDecoder) run() { + for { + select { + case <-ed.terminator: + return + default: + t, err := ed.rx.Token() + if t != nil && err == nil { + switch t.(type) { + case xml.ProcInst: + case xml.Directive: + case xml.Comment: + default: + c := xml.CopyToken(t) + ed.session.rx <- c + } + } + if err != nil { + if errors.Is(err, io.EOF) { + return + } + log.Println(err) // FIXME terminate session on error + return + } + } + } +} -- cgit v1.2.3-70-g09d2