本文整理汇总了Golang中net.UDPConn.SetReadDeadline方法的典型用法代码示例。如果您正苦于以下问题:Golang UDPConn.SetReadDeadline方法的具体用法?Golang UDPConn.SetReadDeadline怎么用?Golang UDPConn.SetReadDeadline使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net.UDPConn
的用法示例。
在下文中一共展示了UDPConn.SetReadDeadline方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: replyLoop
func (proxy *UDPProxy) replyLoop(proxyConn *net.UDPConn, clientAddr *net.UDPAddr, clientKey *connTrackKey) {
defer func() {
proxy.connTrackLock.Lock()
delete(proxy.connTrackTable, *clientKey)
proxy.connTrackLock.Unlock()
proxyConn.Close()
}()
readBuf := make([]byte, UDPBufSize)
for {
proxyConn.SetReadDeadline(time.Now().Add(UDPConnTrackTimeout))
again:
read, err := proxyConn.Read(readBuf)
if err != nil {
if err, ok := err.(*net.OpError); ok && err.Err == syscall.ECONNREFUSED {
// This will happen if the last write failed
// (e.g: nothing is actually listening on the
// proxied port on the container), ignore it
// and continue until UDPConnTrackTimeout
// expires:
goto again
}
return
}
for i := 0; i != read; {
written, err := proxy.listener.WriteToUDP(readBuf[i:read], clientAddr)
if err != nil {
return
}
i += written
}
}
}
示例2: receiveUDP
func receiveUDP(udpConn *net.UDPConn, rAddr *net.UDPAddr) {
b := make([]byte, 1024)
udpConn.SetReadDeadline(time.Now().Add(5 * time.Second))
n, Addr, err := udpConn.ReadFromUDP(b)
// n,err:=udpConn.Read(b)
if err != nil {
fmt.Println(err.Error())
return
}
if n > 1024 {
fmt.Println("Buff out!")
return
}
fmt.Println(udpConn.RemoteAddr().Network())
if !Addr.IP.Equal(rAddr.IP) {
fmt.Println("IP diff:%s-%s", Addr.IP.String(), rAddr.IP.String())
return
} else if Addr.Port != rAddr.Port {
fmt.Println("Port diff:%d-%d", Addr.Port, rAddr.Port)
return
}
fmt.Printf("Receive from %s:%s", udpConn.RemoteAddr().String(), b[:n])
}
示例3: ServeUDP
// ServeUDP starts a UDP listener for the server.
// Each request is handled in a seperate goroutine,
// with the Handler set in ....
func (srv *Server) ServeUDP(l *net.UDPConn) error {
defer l.Close()
handler := srv.Handler
if handler == nil {
handler = DefaultServeMux
}
if srv.UDPSize == 0 {
srv.UDPSize = UDPMsgSize
}
for {
m := make([]byte, srv.UDPSize)
n, a, e := l.ReadFromUDP(m)
if e != nil {
return e
}
m = m[:n]
if srv.ReadTimeout != 0 {
l.SetReadDeadline(time.Now().Add(srv.ReadTimeout))
}
if srv.WriteTimeout != 0 {
l.SetWriteDeadline(time.Now().Add(srv.WriteTimeout))
}
d, err := newConn(nil, l, a, m, handler, srv.TsigSecret)
if err != nil {
continue
}
go d.serve()
}
panic("not reached")
}
示例4: serveUDP
// serveUDP starts a UDP listener for the server.
// Each request is handled in a seperate goroutine.
func (srv *Server) serveUDP(l *net.UDPConn) error {
defer l.Close()
handler := srv.Handler
if handler == nil {
handler = DefaultServeMux
}
if srv.UDPSize == 0 {
srv.UDPSize = udpMsgSize
}
for {
if srv.ReadTimeout != 0 {
l.SetReadDeadline(time.Now().Add(srv.ReadTimeout))
}
if srv.WriteTimeout != 0 {
l.SetWriteDeadline(time.Now().Add(srv.WriteTimeout))
}
m := make([]byte, srv.UDPSize)
n, a, e := l.ReadFromUDP(m)
if e != nil || n == 0 {
// don't bail out, but wait for a new request
continue
}
m = m[:n]
go serve(a, handler, m, l, nil, srv.TsigSecret)
}
panic("dns: not reached")
}
示例5: udpConnectionReader
func udpConnectionReader(conn *net.UDPConn, rcvCh chan UdpMessage, TimeoutCh chan bool) {
for {
buf := make([]byte, MSGsize)
conn.SetReadDeadline(time.Now().Add(200 * time.Millisecond))
n, rAddr, err := conn.ReadFromUDP(buf)
buf = buf[:n]
if err != nil || n < 0 {
switch err := err.(type) {
case net.Error:
if err.Timeout() {
//fmt.Println("Before rcvCh1")
TimeoutCh <- true
//fmt.Println("After rcvCh1")
continue
} else {
fmt.Println("Error in connectionReader")
panic(err)
}
}
}
var TempData elevatorOperation.Elevator
DecodeMessage(&TempData, buf)
//fmt.Println(TempData)
rcvCh <- UdpMessage{Raddr: rAddr.String(), Data: TempData, Length: n}
//fmt.Println("After rcvCh2")
time.Sleep(10 * time.Millisecond)
}
}
示例6: detect_precense
func detect_precense(connection *net.UDPConn, masterChan chan bool) {
buffer := make([]byte, 2048)
for {
t := time.Now()
connection.SetReadDeadline(t.Add(3 * time.Second))
_, _, err := connection.ReadFromUDP(buffer)
if err != nil {
fmt.Println("UDP timeout: ", err)
masterChan <- true
break
}
/*
fmt.Println("I'm getting into this for")
select {
case <-time.After(time.Second * 3):
fmt.Println("Master dead")
masterChan <- true
break L
/*
default:
_, _, err := connection.ReadFromUDP(buffer)
if err != nil {
fmt.Println("You messed up in detect_precense.")
panic(err)
}
}(*/
}
}
示例7: setReadDeadline
// Sets the read deadline on the given connection using the given timeout (which should be a duration). The timeout is added to time.Now().
func setReadDeadline(connection *net.UDPConn, timeout string) error {
duration, error := time.ParseDuration(timeout)
if error != nil {
return error
}
connection.SetReadDeadline(time.Now().Add(duration))
return nil
}
示例8: Receive
// Receive a message.
func Receive(l *net.UDPConn, buf []byte) (Message, error) {
l.SetReadDeadline(time.Now().Add(ResponseTimeout))
nr, _, err := l.ReadFromUDP(buf)
if err != nil {
return Message{}, err
}
return ParseMessage(buf[:nr])
}
示例9: udpTest
func udpTest(Conn *net.UDPConn) bool {
buf := make([]byte, 1024)
Conn.SetReadDeadline(time.Now().Add(1 * time.Second))
Conn.Write([]byte("TEST"))
n, _, _ := Conn.ReadFromUDP(buf)
Conn.Close()
return string(buf[:n]) == "OK\n"
}
示例10: Receive
// Receive a message.
func Receive(l *net.UDPConn, buf []byte) (Message, error) {
l.SetReadDeadline(time.Now().Add(RESPONSE_TIMEOUT))
nr, _, err := l.ReadFromUDP(buf)
if err != nil {
return Message{}, err
}
return parseMessage(buf[:nr])
}
示例11: readUDP
func (srv *Server) readUDP(conn *net.UDPConn, timeout time.Duration) ([]byte, net.Addr, error) {
conn.SetReadDeadline(time.Now().Add(timeout))
m := make([]byte, srv.UDPSize)
n, a, e := conn.ReadFromUDP(m)
if e != nil || n == 0 {
return nil, nil, ErrConn
}
m = m[:n]
return m, a, nil
}
示例12: asyncSflowListen
func asyncSflowListen(t *testing.T, wg *sync.WaitGroup, conn *net.UDPConn, trace *packetsTraceInfo) {
defer wg.Done()
var buf [maxDgramSize]byte
t.Log("listen...")
nbPackets := 0
conn.SetReadDeadline(time.Now().Add(10 * time.Second))
for {
_, _, err := conn.ReadFromUDP(buf[:])
if err != nil {
neterr := err.(*net.OpError)
if neterr.Timeout() == false {
t.Error(err.Error())
}
break
}
p := gopacket.NewPacket(buf[:], layers.LayerTypeSFlow, gopacket.Default)
sflowLayer := p.Layer(layers.LayerTypeSFlow)
sflowPacket, ok := sflowLayer.(*layers.SFlowDatagram)
if !ok {
t.Fatal("not SFlowDatagram")
break
}
if sflowPacket.SampleCount > 0 {
for _, sample := range sflowPacket.FlowSamples {
for _, rec := range sample.Records {
record, ok := rec.(layers.SFlowRawPacketFlowRecord)
if !ok {
t.Fatal("1st layer is not SFlowRawPacketFlowRecord type")
break
}
packet := record.Header
nbPackets++
packetSize := len(packet.Data())
if nbPackets > len(trace.bytes) {
t.Fatalf("Too much Packets, reference have only %d", len(trace.bytes))
}
if trace.bytes[nbPackets-1] != packetSize {
t.Fatalf("Packet size don't match %d %d", trace.bytes[nbPackets-1], packetSize)
}
}
}
}
}
if trace.packets != nbPackets {
t.Fatalf("NB Packets don't match %d %d", trace.packets, nbPackets)
}
}
示例13: dong
func dong(udpConn *net.UDPConn, rAddr *net.UDPAddr, chap []byte, sn uint32, timeout time.Duration) (state *State, err error) {
b := make([]byte, 1024)
udpConn.SetReadDeadline(time.Now().Add(timeout))
n, Addr, err := udpConn.ReadFromUDP(b)
if err != nil {
return nil, err
}
if (udpAddrEqual(Addr, rAddr) == false) || (checkReply(b[:n], chap, sn)) {
return nil, errNotReplyPackage
}
return getStateFromBytes(b)
}
示例14: writeReadUDP
// Sends to_write via udp_conn to remote_addr, then reads from udp_conn into read_buf
// and returns the number of bytes read and the UDP address from which they were received.
// Errors are reported in the 3rd return value.
// If timeout is non-0 the function will return (with an error if necessary) after no more
// than that duration. Before that time, if an error occurs during sending or reading, the
// function will retry the whole operation (beginning with the write).
// For each individual retry, a random timeout between min_wait_retry and max_wait_retry is
// used (but no more than the remaining time from timeout).
func writeReadUDP(udp_conn *net.UDPConn, remote_addr *net.UDPAddr, to_write, read_buf []byte, min_wait_retry, max_wait_retry, timeout time.Duration) (int, *net.UDPAddr, error) {
var special_err error
var err error
if timeout == 0 {
timeout = 365 * 86400 * time.Second
}
endtime := time.Now().Add(timeout)
if min_wait_retry <= 0 {
min_wait_retry++
}
if max_wait_retry <= min_wait_retry {
max_wait_retry = min_wait_retry + 1
}
for {
_, err = udp_conn.WriteToUDP(to_write, remote_addr)
if err == nil {
timo := time.Duration(rand.Int63n(int64(max_wait_retry-min_wait_retry))) + min_wait_retry
endtime2 := time.Now().Add(timo)
if endtime2.After(endtime) {
endtime2 = endtime
}
udp_conn.SetReadDeadline(endtime2)
var n int
var raddr *net.UDPAddr
n, raddr, err = udp_conn.ReadFromUDP(read_buf)
if err == nil {
if n < 4 {
err = too_short
} else {
return n, raddr, err
}
}
}
if e, ok := err.(*net.OpError); !ok || !e.Timeout() {
special_err = err
}
if time.Now().After(endtime) {
break
}
}
if special_err != nil {
return 0, nil, special_err
}
return 0, nil, err
}
示例15: readUDP
func (srv *Server) readUDP(conn *net.UDPConn, timeout time.Duration) ([]byte, *SessionUDP, error) {
conn.SetReadDeadline(time.Now().Add(timeout))
m := make([]byte, srv.UDPSize)
n, s, e := ReadFromSessionUDP(conn, m)
if e != nil || n == 0 {
if e != nil {
return nil, nil, e
}
return nil, nil, ErrShortRead
}
m = m[:n]
return m, s, nil
}