本文整理汇总了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")
}
示例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
}