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


Golang PacketConn.SetReadDeadline方法代码示例

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


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

示例1: ReceivePacket

// ReceivePacket listens for incoming OSC packets and returns the packet and
// client address if one is received.
func (s *Server) ReceivePacket(ctx context.Context, c net.PacketConn) (Packet, net.Addr, error) {
	if deadline, ok := ctx.Deadline(); ok {
		if err := c.SetReadDeadline(deadline); err != nil {
			return nil, nil, err
		}
	}

	go func() {
		select {
		// case <-time.After(200 * time.Millisecond):
		// 	log.Println("Overslept.")
		case <-ctx.Done():
			log.Println(ctx.Err())
		}
	}()

	data := make([]byte, 65535)
	var n, start int
	n, addr, err := c.ReadFrom(data)
	if err != nil {
		return nil, nil, err
	}
	pkt, err := readPacket(bufio.NewReader(bytes.NewBuffer(data)), &start, n)
	return pkt, addr, err
}
开发者ID:kward,项目名称:go-osc,代码行数:27,代码来源:osc.go

示例2: send

// RFC 3489: Clients SHOULD retransmit the request starting with an interval
// of 100ms, doubling every retransmit until the interval reaches 1.6s.
// Retransmissions continue with intervals of 1.6s until a response is
// received, or a total of 9 requests have been sent.
func (v *packet) send(conn net.PacketConn, addr net.Addr) (*packet, error) {
	timeout := 100
	for i := 0; i < 9; i++ {
		length, err := conn.WriteTo(v.bytes(), addr)
		if err != nil {
			return nil, err
		}
		if length != len(v.bytes()) {
			return nil, errors.New("Error in sending data.")
		}
		conn.SetReadDeadline(time.Now().Add(time.Duration(timeout) * time.Millisecond))
		if timeout < 1600 {
			timeout *= 2
		}
		packetBytes := make([]byte, 1024)
		length, _, err = conn.ReadFrom(packetBytes)
		if err == nil {
			return newPacketFromBytes(packetBytes[0:length])
		} else {
			if !err.(net.Error).Timeout() {
				return nil, err
			}
		}
	}
	return nil, nil
}
开发者ID:skycoin,项目名称:skycoin,代码行数:30,代码来源:packet.go

示例3: runSyslog

func runSyslog(c net.PacketConn, done chan<- string) {
	var buf [4096]byte
	var rcvd string
	for {
		c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
		n, _, err := c.ReadFrom(buf[:])
		rcvd += string(buf[:n])
		if err != nil {
			break
		}
	}
	done <- rcvd
}
开发者ID:redcatmiss,项目名称:gcc,代码行数:13,代码来源:syslog_test.go

示例4: readFromConnection

// readFromConnection retrieves OSC packets.
func (s *Server) readFromConnection(c net.PacketConn) (packet Packet, err error) {
	if s.ReadTimeout != 0 {
		err = c.SetReadDeadline(time.Now().Add(s.ReadTimeout))
		if err != nil {
			return nil, err
		}
	}
	data := make([]byte, 65535)
	var n, start int
	n, _, err = c.ReadFrom(data)
	if err != nil {
		return nil, err
	}
	return s.readPacket(bufio.NewReader(bytes.NewBuffer(data)), &start, n)
}
开发者ID:WnP,项目名称:go-osc,代码行数:16,代码来源:osc.go

示例5: read

func read(conn net.PacketConn) (SwApi_FindSwAck, net.Addr, error) {
	var pkt SwApi_FindSwAck
	conn.SetReadDeadline(time.Now().Add(3 * time.Second))
	buf := make([]byte, 4096)
	len, peer, err := conn.ReadFrom(buf)
	if err != nil {
		if opErr, ok := err.(*net.OpError); ok {
			if opErr.Timeout() {
				return pkt, peer, err
			}
		}
		log.Fatal(err)
	}
	buffer := bytes.NewBuffer(buf[:len])
	err = binary.Read(buffer, _LE, &pkt)
	if err != nil {
		log.Fatal(err)
	}
	return pkt, peer, nil
}
开发者ID:cerevo,项目名称:LiveWedge_API,代码行数:20,代码来源:find.go

示例6: runPktSyslog

func runPktSyslog(c net.PacketConn, done chan<- string) {
	var buf [4096]byte
	var rcvd string
	ct := 0
	for {
		var n int
		var err error

		c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
		n, _, err = c.ReadFrom(buf[:])
		rcvd += string(buf[:n])
		if err != nil {
			if oe, ok := err.(*net.OpError); ok {
				if ct < 3 && oe.Temporary() {
					ct++
					continue
				}
			}
			break
		}
	}
	c.Close()
	done <- rcvd
}
开发者ID:pjump,项目名称:gcc,代码行数:24,代码来源:syslog_test.go

示例7: syslogWorker

func syslogWorker(die dieCh, conn net.PacketConn, cfg logplexc.Config, sr *serveRecord) {
	// Make world-writable so anything can connect and send logs.
	// This may be be worth locking down more, but as-is unless
	// pg_logplexcollector and the Postgres server share the same
	// running user common umasks will be useless.
	fi, err := os.Stat(sr.P)
	if err != nil {
		log.Fatalf(
			"exiting, cannot stat just created socket %q: %v",
			sr.P, err)
	}

	err = os.Chmod(sr.P, fi.Mode().Perm()|0222)
	if err != nil {
		log.Fatalf(
			"exiting, cannot make just created socket "+
				"world-writable %q: %v",
			sr.P, err)
	}

	cfg.Logplex = sr.u

	buf := make([]byte, 9*KB)
	target, err := logplexc.NewClient(&cfg)
	if err != nil {
		log.Fatalf("could not create auditing client: %v", err)
	}

	for {
		select {
		case <-die:
			return
		default:
			break
		}

		err := conn.SetReadDeadline(time.Now().Add(time.Duration(1 * time.Second)))
		if err != nil {
			log.Fatalf("could not set connection deadline: %v", err)
		}

		n, _, err := conn.ReadFrom(buf)
		if n > 0 {
			// Just send the message wholesale, which
			// leads to some weird syslog-in-syslog
			// framing, but perhaps it's good enough.
			target.BufferMessage(134, time.Now(),
				"audit", "-", append([]byte(
					"instance_type=shogun identity="+
						sr.I+" "), buf[:n]...))
		}

		if err != nil {
			if err, ok := err.(net.Error); ok {
				if err.Timeout() || err.Temporary() {
					continue
				}
			}

			log.Fatalf("got syslog datagram error %v", err)
		}
	}
}
开发者ID:heroku,项目名称:pg_logplexcollector,代码行数:63,代码来源:syslog.go


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