summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2023-05-10 18:01:40 +0200
committerxengineering <me@xengineering.eu>2023-05-10 18:01:40 +0200
commit73061a440f663bf8bb82f03861fddbaf15e48ae9 (patch)
tree73e3ae5063c8e8f7a076ddf8caace6cd3b33f304
parenta3f1bd19945491608bb3acc9f408e0cfbc84331d (diff)
downloadlimox-73061a440f663bf8bb82f03861fddbaf15e48ae9.tar
limox-73061a440f663bf8bb82f03861fddbaf15e48ae9.tar.zst
limox-73061a440f663bf8bb82f03861fddbaf15e48ae9.zip
Use central XML encoder / decoder
-rw-r--r--xmpp.go31
1 files changed, 20 insertions, 11 deletions
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)
}