batch.go 3.3 KB

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