From 8ab83cfe65a5487f62ca6d1d2c612a7505c6bd70 Mon Sep 17 00:00:00 2001 From: xengineering Date: Thu, 4 May 2023 20:14:48 +0200 Subject: Implement concurrent send / receive LimoX should receive and send XML elements from and to the server concurrently to avoid blocking. --- xmpp.go | 51 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/xmpp.go b/xmpp.go index f1f0d22..a8dbcee 100644 --- a/xmpp.go +++ b/xmpp.go @@ -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 } -- cgit v1.2.3-70-g09d2