Esempio n. 1
0
// parseAndAck parses the UDP payload and sends anwACK to the client receiver.
// TODO(jvesuna): Keep parseAndAck routine live with a connection. Have the ackServer channel data
// to this routine for faster processing.
func parseAndAck(buf []byte, ServerConn *net.UDPConn, clientUDPAddr *net.UDPAddr) {
	messageAck := &ptpb.PingtestMessage{}
	if err := proto.Unmarshal(buf, messageAck); err != nil {
		//TODO(jvesuna): Fix this error handling.
		log.Println("error: failed to unmarshal packet in parseAndAck:", err)
	}

	if *tinyAck {
		// Don't include padding.
		// Here, messageAck should always be about the same size.
		messageAck = &ptpb.PingtestMessage{
			PingtestParams: messageAck.PingtestParams,
			Type:           messageAck.Type,
		}
	}

	wireBytesAck, err := proto.Marshal(messageAck)
	if err != nil {
		fmt.Println("error marshalling ACK:", err)
	}

	// Send ACK.
	ServerConn.SetWriteBuffer(proto.Size(messageAck))
	_, err = ServerConn.WriteToUDP(wireBytesAck, clientUDPAddr)
	if err != nil {
		log.Println("Failed to send ACK:", err)
	}
	log.Println("Sent Ack")
}
Esempio n. 2
0
func (uc *udpclient) Write(bp BatchPoints) error {
	var addr *net.UDPAddr
	var con *net.UDPConn
	addr, err := net.ResolveUDPAddr("udp", uc.addr)
	if err != nil {
		return err
	}

	con, err = net.DialUDP("udp", nil, addr)
	if err != nil {
		return err
	}
	defer con.Close()
	if err = con.SetWriteBuffer(UDPBufferSize); err != nil {
		return err
	}

	var b bytes.Buffer
	var d time.Duration
	d, _ = time.ParseDuration("1" + bp.Precision())

	for _, p := range bp.Points() {
		pointstring := p.pt.RoundedString(d) + "\n"

		// Write and reset the buffer if we reach the max size
		if b.Len()+len(pointstring) >= UDPBufferSize {
			if _, err = con.Write(b.Bytes()); err != nil {
				return err
			}
			b.Reset()
		}

		if _, err := b.WriteString(pointstring); err != nil {
			return err
		}
	}

	_, err = con.Write(b.Bytes())
	return err
}