rawconn_mmsg.go 1.5 KB

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