本文整理匯總了Golang中net.IPConn.SetReadDeadline方法的典型用法代碼示例。如果您正苦於以下問題:Golang IPConn.SetReadDeadline方法的具體用法?Golang IPConn.SetReadDeadline怎麽用?Golang IPConn.SetReadDeadline使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類net.IPConn
的用法示例。
在下文中一共展示了IPConn.SetReadDeadline方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: recvICMP
func (p *Pinger) recvICMP(conn *net.IPConn, recv chan<- *packet, ctx *context, wg *sync.WaitGroup) {
p.debugln("recvICMP(): Start")
for {
select {
case <-ctx.stop:
p.debugln("recvICMP(): <-ctx.stop")
wg.Done()
p.debugln("recvICMP(): wg.Done()")
return
default:
}
bytes := make([]byte, 512)
conn.SetReadDeadline(time.Now().Add(time.Millisecond * 100))
p.debugln("recvICMP(): ReadMsgIP Start")
_, _, _, ra, err := conn.ReadMsgIP(bytes, nil)
p.debugln("recvICMP(): ReadMsgIP End")
if err != nil {
if neterr, ok := err.(*net.OpError); ok {
if neterr.Timeout() {
p.debugln("recvICMP(): Read Timeout")
continue
} else {
p.debugln("recvICMP(): OpError happen", err)
p.mu.Lock()
ctx.err = err
p.mu.Unlock()
p.debugln("recvICMP(): close(ctx.done)")
close(ctx.done)
p.debugln("recvICMP(): wg.Done()")
wg.Done()
return
}
}
}
p.debugln("recvICMP(): p.recv <- packet")
select {
case recv <- &packet{bytes: bytes, addr: ra}:
case <-ctx.stop:
p.debugln("recvICMP(): <-ctx.stop")
wg.Done()
p.debugln("recvICMP(): wg.Done()")
return
}
}
}
示例2: readOneICMP4Packet
func readOneICMP4Packet(conn *net.IPConn, deadline time.Time) (
*icmpPacket, error,
) {
for {
conn.SetReadDeadline(deadline)
bytes := make([]byte, 512)
_, _, _, ra, err := conn.ReadMsgIP(bytes, nil)
if err != nil {
if neterr, ok := err.(*net.OpError); ok {
if neterr.Timeout() {
return nil, err
} else {
fmt.Println("readOneICMP4Packet(): OpError happen", err)
return nil, err
}
}
}
fmt.Printf("ICMP: Got packet from %s.\n", ra.String())
return &icmpPacket{bytes: bytes, addr: ra}, nil
}
}