Black Lives Matter. Support the Equal Justice Initiative.

Source file src/net/fd_posix.go

Documentation: net

     1  // Copyright 2020 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
     6  // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris windows
     7  
     8  package net
     9  
    10  import (
    11  	"internal/poll"
    12  	"runtime"
    13  	"syscall"
    14  	"time"
    15  )
    16  
    17  // Network file descriptor.
    18  type netFD struct {
    19  	pfd poll.FD
    20  
    21  	// immutable until Close
    22  	family      int
    23  	sotype      int
    24  	isConnected bool // handshake completed or use of association with peer
    25  	net         string
    26  	laddr       Addr
    27  	raddr       Addr
    28  }
    29  
    30  func (fd *netFD) setAddr(laddr, raddr Addr) {
    31  	fd.laddr = laddr
    32  	fd.raddr = raddr
    33  	runtime.SetFinalizer(fd, (*netFD).Close)
    34  }
    35  
    36  func (fd *netFD) Close() error {
    37  	runtime.SetFinalizer(fd, nil)
    38  	return fd.pfd.Close()
    39  }
    40  
    41  func (fd *netFD) shutdown(how int) error {
    42  	err := fd.pfd.Shutdown(how)
    43  	runtime.KeepAlive(fd)
    44  	return wrapSyscallError("shutdown", err)
    45  }
    46  
    47  func (fd *netFD) closeRead() error {
    48  	return fd.shutdown(syscall.SHUT_RD)
    49  }
    50  
    51  func (fd *netFD) closeWrite() error {
    52  	return fd.shutdown(syscall.SHUT_WR)
    53  }
    54  
    55  func (fd *netFD) Read(p []byte) (n int, err error) {
    56  	n, err = fd.pfd.Read(p)
    57  	runtime.KeepAlive(fd)
    58  	return n, wrapSyscallError(readSyscallName, err)
    59  }
    60  
    61  func (fd *netFD) readFrom(p []byte) (n int, sa syscall.Sockaddr, err error) {
    62  	n, sa, err = fd.pfd.ReadFrom(p)
    63  	runtime.KeepAlive(fd)
    64  	return n, sa, wrapSyscallError(readFromSyscallName, err)
    65  }
    66  
    67  func (fd *netFD) readMsg(p []byte, oob []byte, flags int) (n, oobn, retflags int, sa syscall.Sockaddr, err error) {
    68  	n, oobn, retflags, sa, err = fd.pfd.ReadMsg(p, oob, flags)
    69  	runtime.KeepAlive(fd)
    70  	return n, oobn, retflags, sa, wrapSyscallError(readMsgSyscallName, err)
    71  }
    72  
    73  func (fd *netFD) Write(p []byte) (nn int, err error) {
    74  	nn, err = fd.pfd.Write(p)
    75  	runtime.KeepAlive(fd)
    76  	return nn, wrapSyscallError(writeSyscallName, err)
    77  }
    78  
    79  func (fd *netFD) writeTo(p []byte, sa syscall.Sockaddr) (n int, err error) {
    80  	n, err = fd.pfd.WriteTo(p, sa)
    81  	runtime.KeepAlive(fd)
    82  	return n, wrapSyscallError(writeToSyscallName, err)
    83  }
    84  
    85  func (fd *netFD) writeMsg(p []byte, oob []byte, sa syscall.Sockaddr) (n int, oobn int, err error) {
    86  	n, oobn, err = fd.pfd.WriteMsg(p, oob, sa)
    87  	runtime.KeepAlive(fd)
    88  	return n, oobn, wrapSyscallError(writeMsgSyscallName, err)
    89  }
    90  
    91  func (fd *netFD) SetDeadline(t time.Time) error {
    92  	return fd.pfd.SetDeadline(t)
    93  }
    94  
    95  func (fd *netFD) SetReadDeadline(t time.Time) error {
    96  	return fd.pfd.SetReadDeadline(t)
    97  }
    98  
    99  func (fd *netFD) SetWriteDeadline(t time.Time) error {
   100  	return fd.pfd.SetWriteDeadline(t)
   101  }
   102  

View as plain text