diff options
author | xengineering <me@xengineering.eu> | 2023-05-04 20:14:48 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2023-05-04 21:17:13 +0200 |
commit | 8ab83cfe65a5487f62ca6d1d2c612a7505c6bd70 (patch) | |
tree | 2dff49947b2b2b23b1bbc43976b264aaad72e9b8 | |
parent | df397ca402ea075df24175804a54a3d3daff3c9c (diff) | |
download | limox-8ab83cfe65a5487f62ca6d1d2c612a7505c6bd70.tar limox-8ab83cfe65a5487f62ca6d1d2c612a7505c6bd70.tar.zst limox-8ab83cfe65a5487f62ca6d1d2c612a7505c6bd70.zip |
Implement concurrent send / receive
LimoX should receive and send XML elements from and to the server
concurrently to avoid blocking.
-rw-r--r-- | xmpp.go | 51 |
1 files changed, 43 insertions, 8 deletions
@@ -6,6 +6,7 @@ import ( "encoding/xml" "log" "strings" + "reflect" ) const ( @@ -36,19 +37,54 @@ func (l *Limox) xmpp(jid string, pwd string) { l.XmppEvents <- err return } - l.XmppEvents <- XmppConnect + defer conn.Close() + + receiver := make(chan xml.Token) + termination := make(chan bool) + go func() { + quit := false + dec := xml.NewDecoder(conn) + for { + select { + case <-termination: + quit = true + default: + t, _ := dec.Token() + if t != nil { + receiver <- t + } + } + if quit { + break + } + } + log.Println("Done!") + }() sendStreamStart(conn, jid) + l.XmppEvents <- XmppConnect + var closing bool = false for { - ev := <-l.GuiEvents - switch ev { - case Disconnect: - closing = true - default: - log.Fatalf("Unknown GuiEvent '%d'!\n", ev) + select { + case ev := <-l.GuiEvents: + switch ev { + case Disconnect: + termination <- true + closing = true + default: + //log.Fatalf("Unknown GuiEvent '%d'!\n", ev) + } + case rx := <-receiver: + switch rx.(type) { + case xml.CharData: + log.Printf("S: [ %v ] %s\n", reflect.TypeOf(rx), rx) + default: + log.Printf("S: [ %v ] %+v\n", reflect.TypeOf(rx), rx) + } } + if closing { break } @@ -56,7 +92,6 @@ func (l *Limox) xmpp(jid string, pwd string) { log.Print("C: " + StreamCloser) conn.Write([]byte(StreamCloser)) - conn.Close() l.XmppEvents <- XmppDisconnect } |