diff options
author | xengineering <me@xengineering.eu> | 2023-05-10 18:01:40 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-05-10 18:01:40 +0200 |
commit | 73061a440f663bf8bb82f03861fddbaf15e48ae9 (patch) | |
tree | 73e3ae5063c8e8f7a076ddf8caace6cd3b33f304 /xmpp.go | |
parent | a3f1bd19945491608bb3acc9f408e0cfbc84331d (diff) | |
download | limox-73061a440f663bf8bb82f03861fddbaf15e48ae9.tar limox-73061a440f663bf8bb82f03861fddbaf15e48ae9.tar.zst limox-73061a440f663bf8bb82f03861fddbaf15e48ae9.zip |
Use central XML encoder / decoder
Diffstat (limited to 'xmpp.go')
-rw-r--r-- | xmpp.go | 31 |
1 files changed, 20 insertions, 11 deletions
@@ -32,7 +32,8 @@ func (l *Limox) xmpp(jid string, pwd string) { l.XmppEvents <- err return } - defer conn.Close() + + enc := xml.NewEncoder(conn) receiver := make(chan xml.Token) termination := make(chan bool) @@ -56,7 +57,7 @@ func (l *Limox) xmpp(jid string, pwd string) { log.Println("Done!") }() - end := sendStreamStart(conn, jid) + end := sendStreamStart(enc, jid) l.XmppEvents <- XmppConnect var closing bool = false @@ -79,7 +80,8 @@ func (l *Limox) xmpp(jid string, pwd string) { } } - sendStreamEnd(conn, end) + sendStreamEnd(enc, end) + conn.Close() l.XmppEvents <- XmppDisconnect } @@ -99,7 +101,7 @@ func logToken(t xml.Token, isTx bool) { } } -func sendStreamStart(t *tls.Conn, jid string) (xml.EndElement) { +func sendStreamStart(enc *xml.Encoder, jid string) (xml.EndElement) { start := xml.StartElement{ xml.Name{"jabber:client", "stream:stream"}, []xml.Attr{ @@ -111,24 +113,31 @@ func sendStreamStart(t *tls.Conn, jid string) (xml.EndElement) { }, } - e := xml.NewEncoder(t) - err := e.EncodeToken(start) + err := enc.EncodeToken(start) if err != nil { log.Fatal("Could not encode stream start!") } - e.Close() + + err = enc.Flush() + if err != nil { + log.Fatal("Could not flush XML encoder!") + } logToken(start, true) return start.End() } -func sendStreamEnd(t *tls.Conn, end xml.EndElement) { - e := xml.NewEncoder(t) - err := e.EncodeToken(end) +func sendStreamEnd(enc *xml.Encoder, end xml.EndElement) { + err := enc.EncodeToken(end) if err != nil { log.Fatal("Could not encode stream end!") } - e.Close() + + err = enc.Flush() + if err != nil { + log.Fatal("Could not flush XML encoder!") + } + logToken(end, true) } |