xmpp/layer1.go
changeset 143 62166e57800e
child 145 21a390dd3506
equal deleted inserted replaced
142:0ff033eed887 143:62166e57800e
       
     1 // The lowest level of XMPP protocol, where TLS is applied after the
       
     2 // initial handshake.
       
     3 
       
     4 package xmpp
       
     5 
       
     6 import (
       
     7 	"io"
       
     8 	"time"
       
     9 	"net"
       
    10 )
       
    11 
       
    12 func (cl *Client) readTransport(w io.WriteCloser) {
       
    13 	defer w.Close()
       
    14 	p := make([]byte, 1024)
       
    15 	for {
       
    16 		if cl.socket == nil {
       
    17 			cl.waitForSocket()
       
    18 		}
       
    19 		cl.socket.SetReadDeadline(time.Now().Add(time.Second))
       
    20 		nr, err := cl.socket.Read(p)
       
    21 		if nr == 0 {
       
    22 			if errno, ok := err.(*net.OpError); ok {
       
    23 				if errno.Timeout() {
       
    24 					continue
       
    25 				}
       
    26 			}
       
    27 			Warn.Logf("read: %s", err)
       
    28 			break
       
    29 		}
       
    30 		nw, err := w.Write(p[:nr])
       
    31 		if nw < nr {
       
    32 			Warn.Logf("read: %s", err)
       
    33 			break
       
    34 		}
       
    35 	}
       
    36 }
       
    37 
       
    38 func (cl *Client) writeTransport(r io.Reader) {
       
    39 	defer cl.socket.Close()
       
    40 	p := make([]byte, 1024)
       
    41 	for {
       
    42 		nr, err := r.Read(p)
       
    43 		if nr == 0 {
       
    44 			Warn.Logf("write: %s", err)
       
    45 			break
       
    46 		}
       
    47 		nw, err := cl.socket.Write(p[:nr])
       
    48 		if nw < nr {
       
    49 			Warn.Logf("write: %s", err)
       
    50 			break
       
    51 		}
       
    52 	}
       
    53 }