rawconn_mmsg.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 linux
  5. package socket
  6. import (
  7. "net"
  8. "os"
  9. "syscall"
  10. )
  11. func (c *Conn) recvMsgs(ms []Message, flags int) (int, error) {
  12. for i := range ms {
  13. ms[i].raceWrite()
  14. }
  15. hs := make(mmsghdrs, len(ms))
  16. var parseFn func([]byte, string) (net.Addr, error)
  17. if c.network != "tcp" {
  18. parseFn = parseInetAddr
  19. }
  20. if err := hs.pack(ms, parseFn, nil); err != nil {
  21. return 0, err
  22. }
  23. var operr error
  24. var n int
  25. fn := func(s uintptr) bool {
  26. n, operr = recvmmsg(s, hs, flags)
  27. if operr == syscall.EAGAIN {
  28. return false
  29. }
  30. return true
  31. }
  32. if err := c.c.Read(fn); err != nil {
  33. return n, err
  34. }
  35. if operr != nil {
  36. return n, os.NewSyscallError("recvmmsg", operr)
  37. }
  38. if err := hs[:n].unpack(ms[:n], parseFn, c.network); err != nil {
  39. return n, err
  40. }
  41. return n, nil
  42. }
  43. func (c *Conn) sendMsgs(ms []Message, flags int) (int, error) {
  44. for i := range ms {
  45. ms[i].raceRead()
  46. }
  47. hs := make(mmsghdrs, len(ms))
  48. var marshalFn func(net.Addr) []byte
  49. if c.network != "tcp" {
  50. marshalFn = marshalInetAddr
  51. }
  52. if err := hs.pack(ms, nil, marshalFn); err != nil {
  53. return 0, err
  54. }
  55. var operr error
  56. var n int
  57. fn := func(s uintptr) bool {
  58. n, operr = sendmmsg(s, hs, flags)
  59. if operr == syscall.EAGAIN {
  60. return false
  61. }
  62. return true
  63. }
  64. if err := c.c.Write(fn); err != nil {
  65. return n, err
  66. }
  67. if operr != nil {
  68. return n, os.NewSyscallError("sendmmsg", operr)
  69. }
  70. if err := hs[:n].unpack(ms[:n], nil, ""); err != nil {
  71. return n, err
  72. }
  73. return n, nil
  74. }