diff options
author | xengineering <me@xengineering.eu> | 2023-07-03 22:17:15 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-07-03 22:17:15 +0200 |
commit | 2fade1039c1842f08b30da5c95b5542b57e38ec6 (patch) | |
tree | 329ae6641f6b00f1e0cbf65321ac44b90d0c15e4 | |
parent | cf520b079743ec95d085a439d00b841c253c564a (diff) | |
download | limox-2fade1039c1842f08b30da5c95b5542b57e38ec6.tar limox-2fade1039c1842f08b30da5c95b5542b57e38ec6.tar.zst limox-2fade1039c1842f08b30da5c95b5542b57e38ec6.zip |
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.
-rw-r--r-- | xmpp/jid.go | 2 | ||||
-rw-r--r-- | xmpp/presence.go | 2 | ||||
-rw-r--r-- | xmpp/sasl.go | 2 | ||||
-rw-r--r-- | xmpp/session.go | 6 | ||||
-rw-r--r-- | xmpp/streams.go | 4 | ||||
-rw-r--r-- | xmpp/xml.go | 12 |
6 files changed, 14 insertions, 14 deletions
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 } |