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/xml.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/xml.go')
-rw-r--r-- | xmpp/xml.go | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/xmpp/xml.go b/xmpp/xml.go new file mode 100644 index 0000000..e6fccee --- /dev/null +++ b/xmpp/xml.go @@ -0,0 +1,68 @@ +package xmpp + +import ( + "context" + "crypto/tls" + "encoding/xml" + "errors" + "io" + "log" +) + +func runRx(ctx context.Context, chn chan<- any, conn *tls.Conn) { + + l := logger{"[RX] "} + r := io.TeeReader(conn, l) + d := xml.NewDecoder(r) + + for { + select { + case <-ctx.Done(): + return + default: + t, err := d.Token() + if t != nil && err == nil { + switch e := t.(type) { + case xml.StartElement: + if e.Name.Local == "stream" { + // new server-side stream TODO what to do with this info? + } else { + route(&e, d, chn) + } + case xml.EndElement: + if e.Name.Local == "stream" { + // TODO end complete session + return + } + } + } + if err != nil { + if errors.Is(err, io.EOF) { + return + } + log.Println(err) // FIXME terminate session on error + return + } + } + } +} + +func (s *session) encodeToken(t xml.Token) error { + var err error + defer func() { + if err != nil { + log.Println(err) + } + }() + + err = s.tx.EncodeToken(t) + if err != nil { + return err + } + err = s.tx.Flush() + if err != nil { + return err + } + + return nil +} |