xmpp/layer1.go
changeset 183 b4bd77d58a3e
parent 167 7ce61108ed86
--- a/xmpp/layer1.go	Sun Feb 09 09:50:38 2014 -0700
+++ b/xmpp/layer1.go	Sun Feb 09 09:52:28 2014 -0700
@@ -5,11 +5,16 @@
 
 import (
 	"crypto/tls"
+	"fmt"
 	"io"
+	"log"
 	"net"
 	"time"
 )
 
+// If enabled, print all sent and received XML.
+var Debug = false
+
 var l1interval = time.Second
 
 type layer1 struct {
@@ -18,15 +23,15 @@
 	sendSocks chan net.Conn
 }
 
-func startLayer1(sock net.Conn, recvWriter io.WriteCloser,
+func (cl *Client) startLayer1(sock net.Conn, recvWriter io.WriteCloser,
 	sendReader io.ReadCloser, status <-chan Status) *layer1 {
 	l1 := layer1{sock: sock}
 	recvSocks := make(chan net.Conn)
 	l1.recvSocks = recvSocks
 	sendSocks := make(chan net.Conn, 1)
 	l1.sendSocks = sendSocks
-	go recvTransport(recvSocks, recvWriter, status)
-	go sendTransport(sendSocks, sendReader)
+	go cl.recvTransport(recvSocks, recvWriter, status)
+	go cl.sendTransport(sendSocks, sendReader)
 	recvSocks <- sock
 	sendSocks <- sock
 	return &l1
@@ -50,7 +55,7 @@
 	l1.recvSocks <- l1.sock
 }
 
-func recvTransport(socks <-chan net.Conn, w io.WriteCloser,
+func (cl *Client) recvTransport(socks <-chan net.Conn, w io.WriteCloser,
 	status <-chan Status) {
 
 	defer w.Close()
@@ -59,7 +64,7 @@
 	for {
 		select {
 		case stat := <-status:
-			if stat == StatusShutdown {
+			if stat.Fatal() {
 				return
 			}
 
@@ -78,27 +83,33 @@
 						continue
 					}
 				}
-				Warn.Logf("recvTransport: %s", err)
+				cl.setError(fmt.Errorf("recv: %v", err))
 				return
 			}
+			if Debug {
+				log.Printf("recv: %s", p[:nr])
+			}
 			nw, err := w.Write(p[:nr])
 			if nw < nr {
-				Warn.Logf("recvTransport: %s", err)
+				cl.setError(fmt.Errorf("recv: %v", err))
 				return
 			}
 		}
 	}
 }
 
-func sendTransport(socks <-chan net.Conn, r io.Reader) {
+func (cl *Client) sendTransport(socks <-chan net.Conn, r io.Reader) {
 	var sock net.Conn
 	p := make([]byte, 1024)
 	for {
 		nr, err := r.Read(p)
 		if nr == 0 {
-			Warn.Logf("sendTransport: %s", err)
+			cl.setError(fmt.Errorf("send: %v", err))
 			break
 		}
+		if nr > 0 && Debug {
+			log.Printf("send: %s", p[:nr])
+		}
 		for nr > 0 {
 			select {
 			case sock = <-socks:
@@ -114,7 +125,7 @@
 				nw, err := sock.Write(p[:nr])
 				nr -= nw
 				if nr != 0 {
-					Warn.Logf("write: %s", err)
+					cl.setError(fmt.Errorf("send: %v", err))
 					break
 				}
 			}