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