From eca0649c1e0a72fbea4899a295443eac69600bfd Mon Sep 17 00:00:00 2001 From: xengineering Date: Mon, 3 Jul 2023 20:49:19 +0200 Subject: Use xml.Encoder.EncodeElement() for presence This required the trick of defining an empty struct as `presence` type but works pretty well. For sending data EncodeElement instead of token handling should always work. Since it is simpler it should be used. --- xmpp/presence.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'xmpp/presence.go') diff --git a/xmpp/presence.go b/xmpp/presence.go index b6ea3b5..4adae6e 100644 --- a/xmpp/presence.go +++ b/xmpp/presence.go @@ -5,20 +5,17 @@ import ( "log" ) +type presence struct{} + func (s *session) sendPresence() { start := xml.StartElement{ xml.Name{"", "presence"}, []xml.Attr{}, } - end := start.End() - - tokens := [...]xml.Token{start, end} - for _, v := range tokens { - err := s.ed.encodeToken(v) - if err != nil { - log.Println("Could not encode presence!") - return - } + err := s.ed.tx.EncodeElement(presence{}, start) + if err != nil { + log.Println("Could not encode presence!") + return } } -- cgit v1.2.3-70-g09d2 From 2fade1039c1842f08b30da5c95b5542b57e38ec6 Mon Sep 17 00:00:00 2001 From: xengineering Date: Mon, 3 Jul 2023 22:17:15 +0200 Subject: Move xml.Encoder to session struct The encoderDecoder sub-struct of the session struct should be removed in little steps. This is the first one. --- xmpp/jid.go | 2 +- xmpp/presence.go | 2 +- xmpp/sasl.go | 2 +- xmpp/session.go | 6 ++++++ xmpp/streams.go | 4 ++-- xmpp/xml.go | 12 +++--------- 6 files changed, 14 insertions(+), 14 deletions(-) (limited to 'xmpp/presence.go') diff --git a/xmpp/jid.go b/xmpp/jid.go index 332073b..83772fd 100644 --- a/xmpp/jid.go +++ b/xmpp/jid.go @@ -81,7 +81,7 @@ func (s *session) sendBind() { inner.Bind.Xmlns = "urn:ietf:params:xml:ns:xmpp-bind" inner.Bind.Resource.Content = "limox-" + fmt.Sprintf("%08x", rand.Uint32()) - err := s.ed.tx.EncodeElement(inner, start) + err := s.tx.EncodeElement(inner, start) if err != nil { log.Println("Could not encode ressource binding!") } diff --git a/xmpp/presence.go b/xmpp/presence.go index 4adae6e..e2b1841 100644 --- a/xmpp/presence.go +++ b/xmpp/presence.go @@ -13,7 +13,7 @@ func (s *session) sendPresence() { []xml.Attr{}, } - err := s.ed.tx.EncodeElement(presence{}, start) + err := s.tx.EncodeElement(presence{}, start) if err != nil { log.Println("Could not encode presence!") return diff --git a/xmpp/sasl.go b/xmpp/sasl.go index 24edc9a..0c13f36 100644 --- a/xmpp/sasl.go +++ b/xmpp/sasl.go @@ -23,7 +23,7 @@ func (s *session) sasl() { inner.Payload = make([]byte, base64.StdEncoding.EncodedLen(len(data))) base64.StdEncoding.Encode(inner.Payload, data) - err := s.ed.tx.EncodeElement(inner, start) + err := s.tx.EncodeElement(inner, start) if err != nil { log.Println("Could not encode SASL PLAIN element!") } diff --git a/xmpp/session.go b/xmpp/session.go index a43e4f4..b4a8fab 100644 --- a/xmpp/session.go +++ b/xmpp/session.go @@ -4,6 +4,7 @@ import ( "crypto/tls" "crypto/x509" "encoding/xml" + "io" "log" ) @@ -18,6 +19,7 @@ type session struct { out chan<- any transport *tls.Conn ed encoderDecoder + tx *xml.Encoder rx chan xml.Token resourceReq string } @@ -49,6 +51,10 @@ func (s *session) run() { go s.ed.run() defer func() { s.ed.terminator <- true }() + lw := logger{"[TX] "} + w := io.MultiWriter(s.transport, lw) + s.tx = xml.NewEncoder(w) + s.out <- SessionConnect{} runStreamPair(s) diff --git a/xmpp/streams.go b/xmpp/streams.go index 87df86a..5ba4c1d 100644 --- a/xmpp/streams.go +++ b/xmpp/streams.go @@ -47,7 +47,7 @@ func openStream(s *session) xml.EndElement { } end := start.End() - err := s.ed.encodeToken(start) + err := s.encodeToken(start) if err != nil { log.Println("Could not encode stream start!") } @@ -86,7 +86,7 @@ func syncStreams(s *session) { } func closeStream(s *session, end xml.EndElement) { - err := s.ed.encodeToken(end) + err := s.encodeToken(end) if err != nil { log.Println("Could not encode stream end!") } diff --git a/xmpp/xml.go b/xmpp/xml.go index b0ea77b..f547210 100644 --- a/xmpp/xml.go +++ b/xmpp/xml.go @@ -9,7 +9,6 @@ import ( type encoderDecoder struct { session *session - tx *xml.Encoder rx *xml.Decoder terminator chan bool } @@ -19,11 +18,6 @@ func newEncoderDecoder(s *session) encoderDecoder { ed.session = s - lw := logger{"[TX] "} - w := io.MultiWriter(s.transport, lw) - ed.tx = xml.NewEncoder(w) - ed.tx.Indent("", "") - lr := logger{"[RX] "} r := io.TeeReader(s.transport, lr) ed.rx = xml.NewDecoder(r) @@ -31,7 +25,7 @@ func newEncoderDecoder(s *session) encoderDecoder { return ed } -func (ed *encoderDecoder) encodeToken(t xml.Token) error { +func (s *session) encodeToken(t xml.Token) error { var err error defer func() { if err != nil { @@ -39,11 +33,11 @@ func (ed *encoderDecoder) encodeToken(t xml.Token) error { } }() - err = ed.tx.EncodeToken(t) + err = s.tx.EncodeToken(t) if err != nil { return err } - err = ed.tx.Flush() + err = s.tx.Flush() if err != nil { return err } -- cgit v1.2.3-70-g09d2 From 8e84ca8a4d340956bd0aaead59d5c79dcaede6a5 Mon Sep 17 00:00:00 2001 From: xengineering Date: Tue, 4 Jul 2023 12:43:47 +0200 Subject: Remove xmpp.presence type This was empty and has no benefit. --- xmpp/presence.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'xmpp/presence.go') diff --git a/xmpp/presence.go b/xmpp/presence.go index e2b1841..00eaa47 100644 --- a/xmpp/presence.go +++ b/xmpp/presence.go @@ -5,15 +5,13 @@ import ( "log" ) -type presence struct{} - func (s *session) sendPresence() { start := xml.StartElement{ xml.Name{"", "presence"}, []xml.Attr{}, } - err := s.tx.EncodeElement(presence{}, start) + err := s.tx.EncodeElement(struct{}{}, start) if err != nil { log.Println("Could not encode presence!") return -- cgit v1.2.3-70-g09d2