batch.go 3.3 KB

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