batch.go 3.3 KB

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