github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/kmgNet/netTester/readOnly.go (about)

     1  package netTester
     2  
     3  import (
     4  	"net"
     5  	"time"
     6  
     7  	"github.com/bronze1man/kmg/kmgNet"
     8  	"github.com/bronze1man/kmg/kmgTime"
     9  )
    10  
    11  //client 只读取数据,不写入数据
    12  func readOnly(listenerNewer ListenNewer, Dialer DirectDialer, debug bool) {
    13  	kmgTime.MustNotTimeout(func() {
    14  		toWrite := []byte("hello world")
    15  		listener := listenAccept(listenerNewer, func(c net.Conn) {
    16  			defer c.Close()
    17  			for i := 0; i < 2; i++ {
    18  				_, err := c.Write(toWrite)
    19  				mustNotError(err)
    20  				time.Sleep(time.Microsecond)
    21  			}
    22  		})
    23  		defer listener.Close()
    24  		conn1, err := Dialer()
    25  		mustNotError(err)
    26  		if debug {
    27  			conn1 = kmgNet.NewDebugConn(conn1, "readOnly")
    28  		}
    29  		defer conn1.Close()
    30  		for i := 0; i < 2; i++ {
    31  			mustReadSame(conn1, toWrite)
    32  			time.Sleep(time.Microsecond)
    33  		}
    34  		conn1.Close()
    35  
    36  		listener.Close()
    37  	}, time.Second)
    38  
    39  }