コード例 #1
0
ファイル: plugin_test.go プロジェクト: postfix/xudp
func loop(t *testing.T, c *xudp.Connection) {
	tick := time.NewTicker(time.Second / 30)

	for {
		select {
		case <-tick.C:
			addr, payload, err := c.Recv()

			if err != nil {
				return
			}

			if len(payload) != len(Payload) {
				t.Errorf("Payload size mismatch: Want %d, have %d",
					len(Payload), len(payload))
				return
			}

			for i := range payload {
				if Payload[i] != payload[i] {
					t.Errorf("Payload mismatch at %d: Want %d, have %d",
						i, Payload[i] != payload[i])
					return
				}
			}

			err = c.Send(addr, payload)

			if err != nil {
				return
			}
		}
	}
}
コード例 #2
0
ファイル: plugin_test.go プロジェクト: postfix/xudp
func loop(t *testing.T, c *xudp.Connection) {
	for {
		addr, payload, err := c.Recv()

		if err != nil {
			return
		}

		if len(payload) != len(Payload) {
			t.Errorf("Payload size mismatch: Want %d, have %d",
				len(Payload), len(payload))
			return
		}

		for i := range payload {
			if Payload[i] != payload[i] {
				t.Errorf("Payload mismatch at %d: Want %d, have %d",
					i, Payload[i] != payload[i])
				return
			}
		}

		err = c.Send(addr, payload)

		if err != nil {
			return
		}
	}
}
コード例 #3
0
ファイル: main.go プロジェクト: postfix/xudp
// The main 'game' loop.
func loop(c *xudp.Connection, address net.Addr) {
	// Track average sent/ACK'ed bandwidth
	avgSent := make([]float32, 0, 100)
	avgAcked := make([]float32, 0, 100)

	// Statistics printing ticker.
	statTick := time.NewTicker(time.Second)

	// Channel which accepts incoming messages.
	// This allows us to do non-blocking reads.
	recv := readLoop(c)

	// If we have an address, we are the 'client' and should
	// initiate the echo loop.
	if address != nil {
		c.Send(address, []byte("Hello"))
	}

	for {
		select {
		case <-statTick.C:
			stat(&avgSent, &avgAcked)

		default:
			// Receive next message.
			address, ok := <-recv

			if !ok {
				break
			}

			// Send a random payload back.
			err := c.Send(address, make([]byte, rand.Int31n(int32(c.PayloadSize()))))

			if err != nil {
				return
			}
		}
	}
}