stream.go
changeset 29 a456133ed0ac
parent 26 4d0a369079ce
child 30 a77fc342e013
--- a/stream.go	Thu Dec 29 09:48:36 2011 -0700
+++ b/stream.go	Thu Dec 29 11:02:21 2011 -0700
@@ -195,6 +195,9 @@
 			default:
 				send = true
 			}
+			if !send {
+				continue
+			}
 			st, ok := x.(Stanza)
 			if !ok {
 				log.Printf("Unhandled non-stanza: %v",
@@ -213,14 +216,29 @@
 	}
 }
 
-// BUG(cjyar) Disable this loop until resource binding is
-// complete. Otherwise the app might inject something weird into our
-// negotiation stream.
-func writeStream(srvOut chan<- interface{}, cliIn <-chan Stanza) {
+// This loop is paused until resource binding is complete. Otherwise
+// the app might inject something inappropriate into our negotiations
+// with the server. The control channel controls this loop's
+// activity.
+func writeStream(srvOut chan<- interface{}, cliIn <-chan Stanza,
+	control <-chan int) {
 	defer tryClose(srvOut, cliIn)
 
-	for x := range cliIn {
-		srvOut <- x
+	var input <-chan Stanza
+	for {
+		select {
+		case status := <- control:
+			switch status {
+			case 0:
+				input = nil
+			case 1:
+				input = cliIn
+			case -1:
+				break
+			}
+		case x := <- input:
+			srvOut <- x
+		}
 	}
 }
 
@@ -513,7 +531,8 @@
 		}
 		cl.Jid = *jid
 		log.Printf("Bound resource: %s", cl.Jid.String())
-		return true
+		cl.bindDone()
+		return false
 	}
 	cl.HandleStanza(msg.Id, f)
 	cl.xmlOut <- msg