Пример #1
0
//一次写入大量数据(超过mtu即可)
func bigWrite(listenerNewer ListenNewer, Dialer DirectDialer, debug bool) {
	kmgTime.MustNotTimeout(func() {
		listener := runEchoServer(listenerNewer)
		defer listener.Close()

		toWrite := bytes.Repeat([]byte{0}, 1024*100)

		func() {
			//先写后读
			wg := sync.WaitGroup{}
			conn1, err := Dialer()
			mustNotError(err)
			if debug {
				conn1 = kmgNet.NewDebugConn(conn1, "bigWrite1")
			}
			defer conn1.Close()
			wg.Add(1)
			go func() {
				for i := 0; i < 2; i++ {
					mustReadSame(conn1, toWrite)
				}
				wg.Done()
			}()
			for i := 0; i < 2; i++ {
				_, err = conn1.Write(toWrite)
				mustNotError(err)
				time.Sleep(time.Microsecond)
			}
			wg.Wait()
			conn1.Close()
		}()

		func() {
			//先读后写
			conn1, err := Dialer()
			mustNotError(err)
			if debug {
				conn1 = kmgNet.NewDebugConn(conn1, "bigWrite2")
			}
			defer conn1.Close()
			go func() {
				for i := 0; i < 2; i++ {
					_, err := conn1.Write(toWrite)
					mustNotError(err)
					time.Sleep(time.Microsecond)
				}
			}()
			for i := 0; i < 2; i++ {
				mustReadSame(conn1, toWrite)
			}
			conn1.Close()
		}()

		listener.Close()
	}, time.Second)
}
Пример #2
0
//client只写入,不读取
func writeOnly(listenerNewer ListenNewer, Dialer DirectDialer, debug bool) {
	kmgTime.MustNotTimeout(func() {

		toWrite := []byte("hello world")
		listener := listenAccept(listenerNewer, func(c net.Conn) {
			defer c.Close()
			for i := 0; i < 2; i++ {
				mustReadSame(c, toWrite)
				time.Sleep(time.Microsecond)
			}
		})
		defer listener.Close()
		conn1, err := Dialer()
		mustNotError(err)
		if debug {
			conn1 = kmgNet.NewDebugConn(conn1, "writeOnly")
		}
		defer conn1.Close()
		for i := 0; i < 2; i++ {
			_, err = conn1.Write(toWrite)
			mustNotError(err)
			time.Sleep(time.Microsecond)
		}
		conn1.Close()

		listener.Close()
	}, time.Second)

}
Пример #3
0
//client 先写后读
func writeRead(listenerNewer ListenNewer,
	Dialer DirectDialer,
	debug bool) {
	kmgTime.MustNotTimeout(func() {
		listener := runEchoServer(listenerNewer)
		defer listener.Close()

		toWrite := []byte("hello world")

		conn1, err := Dialer()
		mustNotError(err)

		if debug {
			conn1 = kmgNet.NewDebugConn(conn1, "writeRead")
		}
		defer conn1.Close()
		for i := 0; i < 2; i++ {
			_, err = conn1.Write(toWrite)
			mustNotError(err)
			time.Sleep(time.Microsecond)
			mustReadSame(conn1, toWrite)
			time.Sleep(time.Microsecond)
		}
		conn1.Close()
		listener.Close()
	}, time.Second)
}
Пример #4
0
//只dial,没有传输任何数据就挂掉了.
func dialOnly(listenerNewer ListenNewer, Dialer DirectDialer, debug bool) {
	kmgTime.MustNotTimeout(func() {
		listener := listenAccept(listenerNewer, func(c net.Conn) {
			time.Sleep(time.Microsecond) //防止出现reset
			c.Close()
		})
		defer listener.Close()
		//client 双方主动关闭
		for i := 0; i < 2; i++ {
			conn1, err := Dialer()
			mustNotError(err)
			if debug {
				conn1 = kmgNet.NewDebugConn(conn1, fmt.Sprintf("dialOnly 1 %d", i))
			}
			conn1.Close()
		}

		//client 被动关闭
		buf := make([]byte, 1024)
		for i := 0; i < 2; i++ {
			conn1, err := Dialer()
			mustNotError(err)
			if debug {
				conn1 = kmgNet.NewDebugConn(conn1, fmt.Sprintf("dialOnly 2 %d", i))
			}
			defer conn1.Close()
			_, err = conn1.Read(buf)
			if err != io.EOF {
				panic("remote close, local not get io.EOF")
			}
			conn1.Close()
		}

		listener.Close()
	}, time.Second)

	time.Sleep(time.Microsecond)
	kmgTime.MustNotTimeout(func() {
		listener := listenAccept(listenerNewer, func(c net.Conn) {
			buf := make([]byte, 1024)
			defer c.Close()
			_, err := c.Read(buf)
			if err != io.EOF {
				panic("local close, remote not get io.EOF")
			}
		})
		defer listener.Close()
		//client 主动关闭
		for i := 0; i < 2; i++ {
			conn1, err := Dialer()
			mustNotError(err)
			if debug {
				conn1 = kmgNet.NewDebugConn(conn1, fmt.Sprintf("dialOnly 1 %d", i))
			}
			time.Sleep(time.Microsecond) //防止出现reset
			conn1.Close()
		}

		listener.Close()
	}, time.Second)

}