コード例 #1
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
			}
		}
	}
}