Ejemplo n.º 1
0
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
			}
		}
	}
}
Ejemplo n.º 2
0
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
		}
	}
}
Ejemplo n.º 3
0
Archivo: main.go Proyecto: postfix/xudp
// readLoop reads data from the connection and yields it through the
// returned channel. This allows us to make the read a non-blocking operation.
//
// In this particular program, we do not care about the actual payload.
// Just the sender's address.
func readLoop(c *xudp.Connection) <-chan net.Addr {
	ch := make(chan net.Addr)

	go func() {
		defer close(ch)

		for {
			address, _, err := c.Recv()

			if err != nil {
				return
			}

			ch <- address
		}
	}()

	return ch
}
Ejemplo n.º 4
0
Archivo: main.go Proyecto: 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
			}
		}
	}
}