当前位置: 首页>>代码示例>>Golang>>正文


Golang UDPConn.SetWriteBuffer方法代码示例

本文整理汇总了Golang中net.UDPConn.SetWriteBuffer方法的典型用法代码示例。如果您正苦于以下问题:Golang UDPConn.SetWriteBuffer方法的具体用法?Golang UDPConn.SetWriteBuffer怎么用?Golang UDPConn.SetWriteBuffer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在net.UDPConn的用法示例。


在下文中一共展示了UDPConn.SetWriteBuffer方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: parseAndAck

// 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")
}
开发者ID:JamshedVesuna,项目名称:pingtest,代码行数:32,代码来源:server.go

示例2: Write

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
}
开发者ID:MohamedFAhmed,项目名称:heapster,代码行数:40,代码来源:client.go


注:本文中的net.UDPConn.SetWriteBuffer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。