本文整理汇总了Golang中net.UDPConn.SetReadBuffer方法的典型用法代码示例。如果您正苦于以下问题:Golang UDPConn.SetReadBuffer方法的具体用法?Golang UDPConn.SetReadBuffer怎么用?Golang UDPConn.SetReadBuffer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.UDPConn
的用法示例。
在下文中一共展示了UDPConn.SetReadBuffer方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: setUDPRecvBuf
// setUDPRecvBuf is used to resize the UDP receive window. The function
// attempts to set the read buffer to `udpRecvBuf` but backs off until
// the read buffer can be set.
func setUDPRecvBuf(c *net.UDPConn) {
size := udpRecvBuf
for {
if err := c.SetReadBuffer(size); err == nil {
break
}
size = size / 2
}
}
示例2: main
func main() {
listen := flag.String("addr", ":2055", "Listen address")
flag.Parse()
var addr *net.UDPAddr
var err error
if addr, err = net.ResolveUDPAddr("udp", *listen); err != nil {
log.Fatal(err)
}
var server *net.UDPConn
if server, err = net.ListenUDP("udp", addr); err != nil {
log.Fatal(err)
}
if err = server.SetReadBuffer(readSize); err != nil {
log.Fatal(err)
}
decoders := make(map[string]*netflow.Decoder)
for {
buf := make([]byte, 8192)
var remote *net.UDPAddr
var octets int
if octets, remote, err = server.ReadFromUDP(buf); err != nil {
log.Printf("error reading from %s: %v\n", remote, err)
continue
}
log.Printf("received %d bytes from %s\n", octets, remote)
d, found := decoders[remote.String()]
if !found {
s := session.New()
d = netflow.NewDecoder(s)
decoders[remote.String()] = d
}
m, err := d.Read(bytes.NewBuffer(buf[:octets]))
if err != nil {
log.Println("decoder error:", err)
continue
}
switch p := m.(type) {
case *netflow1.Packet:
netflow1.Dump(p)
case *netflow5.Packet:
netflow5.Dump(p)
case *netflow6.Packet:
netflow6.Dump(p)
case *netflow7.Packet:
netflow7.Dump(p)
case *netflow9.Packet:
netflow9.Dump(p)
case *ipfix.Message:
ipfix.Dump(p)
}
}
}