source sync 20190409
[platform/core/system/edge-orchestration.git] / vendor / github.com / miekg / dns / vendor / golang.org / x / net / ipv6 / batch.go
1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // +build go1.9
6
7 package ipv6
8
9 import (
10         "net"
11         "runtime"
12
13         "golang.org/x/net/internal/socket"
14 )
15
16 // BUG(mikio): On Windows, the ReadBatch and WriteBatch methods of
17 // PacketConn are not implemented.
18
19 // A Message represents an IO message.
20 //
21 //      type Message struct {
22 //              Buffers [][]byte
23 //              OOB     []byte
24 //              Addr    net.Addr
25 //              N       int
26 //              NN      int
27 //              Flags   int
28 //      }
29 //
30 // The Buffers fields represents a list of contiguous buffers, which
31 // can be used for vectored IO, for example, putting a header and a
32 // payload in each slice.
33 // When writing, the Buffers field must contain at least one byte to
34 // write.
35 // When reading, the Buffers field will always contain a byte to read.
36 //
37 // The OOB field contains protocol-specific control or miscellaneous
38 // ancillary data known as out-of-band data.
39 // It can be nil when not required.
40 //
41 // The Addr field specifies a destination address when writing.
42 // It can be nil when the underlying protocol of the endpoint uses
43 // connection-oriented communication.
44 // After a successful read, it may contain the source address on the
45 // received packet.
46 //
47 // The N field indicates the number of bytes read or written from/to
48 // Buffers.
49 //
50 // The NN field indicates the number of bytes read or written from/to
51 // OOB.
52 //
53 // The Flags field contains protocol-specific information on the
54 // received message.
55 type Message = socket.Message
56
57 // ReadBatch reads a batch of messages.
58 //
59 // The provided flags is a set of platform-dependent flags, such as
60 // syscall.MSG_PEEK.
61 //
62 // On a successful read it returns the number of messages received, up
63 // to len(ms).
64 //
65 // On Linux, a batch read will be optimized.
66 // On other platforms, this method will read only a single message.
67 func (c *payloadHandler) ReadBatch(ms []Message, flags int) (int, error) {
68         if !c.ok() {
69                 return 0, errInvalidConn
70         }
71         switch runtime.GOOS {
72         case "linux":
73                 n, err := c.RecvMsgs([]socket.Message(ms), flags)
74                 if err != nil {
75                         err = &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
76                 }
77                 return n, err
78         default:
79                 n := 1
80                 err := c.RecvMsg(&ms[0], flags)
81                 if err != nil {
82                         n = 0
83                         err = &net.OpError{Op: "read", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
84                 }
85                 return n, err
86         }
87 }
88
89 // WriteBatch writes a batch of messages.
90 //
91 // The provided flags is a set of platform-dependent flags, such as
92 // syscall.MSG_DONTROUTE.
93 //
94 // It returns the number of messages written on a successful write.
95 //
96 // On Linux, a batch write will be optimized.
97 // On other platforms, this method will write only a single message.
98 func (c *payloadHandler) WriteBatch(ms []Message, flags int) (int, error) {
99         if !c.ok() {
100                 return 0, errInvalidConn
101         }
102         switch runtime.GOOS {
103         case "linux":
104                 n, err := c.SendMsgs([]socket.Message(ms), flags)
105                 if err != nil {
106                         err = &net.OpError{Op: "write", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
107                 }
108                 return n, err
109         default:
110                 n := 1
111                 err := c.SendMsg(&ms[0], flags)
112                 if err != nil {
113                         n = 0
114                         err = &net.OpError{Op: "write", Net: c.PacketConn.LocalAddr().Network(), Source: c.PacketConn.LocalAddr(), Err: err}
115                 }
116                 return n, err
117         }
118 }