Пример #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
			}
		}
	}
}
Пример #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
		}
	}
}
Пример #3
0
// 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
}