diff options
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 +} |