diff options
author | xengineering <me@xengineering.eu> | 2023-07-04 22:10:55 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-07-04 22:15:17 +0200 |
commit | 48811e7d2487ebc3db49b8af7e20f57db4ac28f4 (patch) | |
tree | a317f8dc44ac9828ae5806e1fa1dee7547118619 /xmpp/encoder_decoder.go | |
parent | 5570ccd1d6b50042acbf2fad3793afa6dde79ca2 (diff) | |
parent | d9fe0a4360770b1e4b6b4fb3686c3275ad1b6e6e (diff) | |
download | limox-48811e7d2487ebc3db49b8af7e20f57db4ac28f4.tar limox-48811e7d2487ebc3db49b8af7e20f57db4ac28f4.tar.zst limox-48811e7d2487ebc3db49b8af7e20f57db4ac28f4.zip |
Merge branch 'element-handling'
This moves away from the concept to parse each individual XML token from
the token and group them as a []xml.Token slice for further processing.
While this is still possible, receiving aswell as sending has switched
to define structs with XML tags which can be marshalled and unmarshalled
with the xml.Encoder.EncodeElement() and xml.Decoder.DecodeElement()
functions.
Further documentation can be found at https://pkg.go.dev/encoding/xml#Unmarshal
and https://pkg.go.dev/encoding/xml#Marshal.
This merge reduces with its changes the number of code lines in the XMPP
implementation by around 41 percent!
Diffstat (limited to 'xmpp/encoder_decoder.go')
-rw-r--r-- | xmpp/encoder_decoder.go | 80 |
1 files changed, 0 insertions, 80 deletions
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 - } - } - } -} |