github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/net/internal/socket/rawconn_msg.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 aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || windows || zos
     6  // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris windows zos
     7  
     8  package socket
     9  
    10  import (
    11  	"os"
    12  )
    13  
    14  func (c *Conn) recvMsg(m *Message, flags int) error {
    15  	m.raceWrite()
    16  	var h msghdr
    17  	vs := make([]iovec, len(m.Buffers))
    18  	var sa []byte
    19  	if c.network != "tcp" {
    20  		sa = make([]byte, sizeofSockaddrInet6)
    21  	}
    22  	h.pack(vs, m.Buffers, m.OOB, sa)
    23  	var operr error
    24  	var n int
    25  	fn := func(s uintptr) bool {
    26  		n, operr = recvmsg(s, &h, flags)
    27  		return ioComplete(flags, operr)
    28  	}
    29  	if err := c.c.Read(fn); err != nil {
    30  		return err
    31  	}
    32  	if operr != nil {
    33  		return os.NewSyscallError("recvmsg", operr)
    34  	}
    35  	if c.network != "tcp" {
    36  		var err error
    37  		m.Addr, err = parseInetAddr(sa[:], c.network)
    38  		if err != nil {
    39  			return err
    40  		}
    41  	}
    42  	m.N = n
    43  	m.NN = h.controllen()
    44  	m.Flags = h.flags()
    45  	return nil
    46  }
    47  
    48  func (c *Conn) sendMsg(m *Message, flags int) error {
    49  	m.raceRead()
    50  	var h msghdr
    51  	vs := make([]iovec, len(m.Buffers))
    52  	var sa []byte
    53  	if m.Addr != nil {
    54  		var a [sizeofSockaddrInet6]byte
    55  		n := marshalInetAddr(m.Addr, a[:])
    56  		sa = a[:n]
    57  	}
    58  	h.pack(vs, m.Buffers, m.OOB, sa)
    59  	var operr error
    60  	var n int
    61  	fn := func(s uintptr) bool {
    62  		n, operr = sendmsg(s, &h, flags)
    63  		return ioComplete(flags, operr)
    64  	}
    65  	if err := c.c.Write(fn); err != nil {
    66  		return err
    67  	}
    68  	if operr != nil {
    69  		return os.NewSyscallError("sendmsg", operr)
    70  	}
    71  	m.N = n
    72  	m.NN = len(m.OOB)
    73  	return nil
    74  }