xmpp/layer1.go
author Chris Jones <christian.jones@sri.com>
Sun, 15 Sep 2013 12:31:59 -0600
changeset 145 21a390dd3506
parent 143 62166e57800e
child 147 d7679d991b17
permissions -rw-r--r--
gofmt

// The lowest level of XMPP protocol, where TLS is applied after the
// initial handshake.

package xmpp

import (
	"io"
	"net"
	"time"
)

func (cl *Client) readTransport(w io.WriteCloser) {
	defer w.Close()
	p := make([]byte, 1024)
	for {
		if cl.socket == nil {
			cl.waitForSocket()
		}
		cl.socket.SetReadDeadline(time.Now().Add(time.Second))
		nr, err := cl.socket.Read(p)
		if nr == 0 {
			if errno, ok := err.(*net.OpError); ok {
				if errno.Timeout() {
					continue
				}
			}
			Warn.Logf("read: %s", err)
			break
		}
		nw, err := w.Write(p[:nr])
		if nw < nr {
			Warn.Logf("read: %s", err)
			break
		}
	}
}

func (cl *Client) writeTransport(r io.Reader) {
	defer cl.socket.Close()
	p := make([]byte, 1024)
	for {
		nr, err := r.Read(p)
		if nr == 0 {
			Warn.Logf("write: %s", err)
			break
		}
		nw, err := cl.socket.Write(p[:nr])
		if nw < nr {
			Warn.Logf("write: %s", err)
			break
		}
	}
}