From 73061a440f663bf8bb82f03861fddbaf15e48ae9 Mon Sep 17 00:00:00 2001 From: xengineering Date: Wed, 10 May 2023 18:01:40 +0200 Subject: Use central XML encoder / decoder --- xmpp.go | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'xmpp.go') diff --git a/xmpp.go b/xmpp.go index 4e69e22..c40dee0 100644 --- a/xmpp.go +++ b/xmpp.go @@ -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) } -- cgit v1.2.3-70-g09d2