diff options
author | xengineering <me@xengineering.eu> | 2023-05-15 20:23:16 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-05-15 20:23:16 +0200 |
commit | 757a8b7856bb7f72db6d95c208ba8cf66c0d54e7 (patch) | |
tree | 93a48c67205adaca09dc55837bd35dc19e87e21c | |
parent | c6090c0d375f28002166a463cfc7b7cda4425626 (diff) | |
download | limox-757a8b7856bb7f72db6d95c208ba8cf66c0d54e7.tar limox-757a8b7856bb7f72db6d95c208ba8cf66c0d54e7.tar.zst limox-757a8b7856bb7f72db6d95c208ba8cf66c0d54e7.zip |
Introduce type xmppReceiver
This simplifies the setup of the XML decoder.
-rw-r--r-- | xmpp.go | 51 |
1 files changed, 33 insertions, 18 deletions
@@ -23,10 +23,9 @@ func xmpp(rxChan chan GuiEvent, txChan chan any, jid string, pwd string) { } defer conn.Close() - receiver := make(chan xml.Token) - termination := make(chan bool) - go rxRoutine(conn, receiver, termination) - defer func() { termination <- true }() + receiver := newXmppReceiver(conn) + go receiver.run() + defer receiver.stop() enc := xml.NewEncoder(conn) defer enc.Close() @@ -48,7 +47,7 @@ func xmpp(rxChan chan GuiEvent, txChan chan any, jid string, pwd string) { default: log.Printf("Unknown GuiEvent '%d'!\n", ev) } - case rx := <-receiver: + case rx := <-receiver.data: dbg.Indent("S: ", " ") dbg.EncodeToken(rx) dbg.Flush() @@ -56,36 +55,52 @@ func xmpp(rxChan chan GuiEvent, txChan chan any, jid string, pwd string) { } } -func setupConn(jid string) (*tls.Conn, error) { - domain := domainpart(jid) +type xmppReceiver struct { + terminator chan bool + data chan xml.Token + decoder *xml.Decoder +} - roots, err := x509.SystemCertPool() - if err != nil { - return nil, err +func newXmppReceiver(conn *tls.Conn) xmppReceiver { + return xmppReceiver{ + make(chan bool), + make(chan xml.Token), + xml.NewDecoder(conn), } - - return tls.Dial("tcp", domain+":"+"5223", &tls.Config{RootCAs: roots}) } -func rxRoutine(conn *tls.Conn, tokens chan xml.Token, terminator chan bool) { - dec := xml.NewDecoder(conn) - +func (r *xmppReceiver) run() { for { select { - case <-terminator: + case <-r.terminator: return default: - t, err := dec.Token() + t, err := r.decoder.Token() if err != nil { log.Print(err) } if t != nil { - tokens <- t + r.data <- t } } } } +func (r *xmppReceiver) stop() { + r.terminator <- true +} + +func setupConn(jid string) (*tls.Conn, error) { + domain := domainpart(jid) + + roots, err := x509.SystemCertPool() + if err != nil { + return nil, err + } + + return tls.Dial("tcp", domain+":"+"5223", &tls.Config{RootCAs: roots}) +} + func sendStreamStart(enc *xml.Encoder, dbg *xml.Encoder, jid string) xml.EndElement { start := xml.StartElement{ xml.Name{"jabber:client", "stream:stream"}, |