golang.org/x/net@v0.25.1-0.20240516223405-c87a5b62e243/internal/socket/rawconn_mmsg.go (about)

     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  
     5  //go:build linux
     6  
     7  package socket
     8  
     9  import (
    10  	"net"
    11  )
    12  
    13  func (c *Conn) recvMsgs(ms []Message, flags int) (int, error) {
    14  	for i := range ms {
    15  		ms[i].raceWrite()
    16  	}
    17  	tmps := defaultMmsgTmpsPool.Get()
    18  	defer defaultMmsgTmpsPool.Put(tmps)
    19  	var parseFn func([]byte, string) (net.Addr, error)
    20  	if c.network != "tcp" {
    21  		parseFn = parseInetAddr
    22  	}
    23  	hs := tmps.packer.pack(ms, parseFn, nil)
    24  	n, err := tmps.syscaller.recvmmsg(c.c, hs, flags)
    25  	if err != nil {
    26  		return n, err
    27  	}
    28  	if err := hs[:n].unpack(ms[:n], parseFn, c.network); err != nil {
    29  		return n, err
    30  	}
    31  	return n, nil
    32  }
    33  
    34  func (c *Conn) sendMsgs(ms []Message, flags int) (int, error) {
    35  	for i := range ms {
    36  		ms[i].raceRead()
    37  	}
    38  	tmps := defaultMmsgTmpsPool.Get()
    39  	defer defaultMmsgTmpsPool.Put(tmps)
    40  	var marshalFn func(net.Addr, []byte) int
    41  	if c.network != "tcp" {
    42  		marshalFn = marshalInetAddr
    43  	}
    44  	hs := tmps.packer.pack(ms, nil, marshalFn)
    45  	n, err := tmps.syscaller.sendmmsg(c.c, hs, flags)
    46  	if err != nil {
    47  		return n, err
    48  	}
    49  	if err := hs[:n].unpack(ms[:n], nil, ""); err != nil {
    50  		return n, err
    51  	}
    52  	return n, nil
    53  }