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

View as plain text