github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/internal/poll/fd_windows.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  package poll
     6  
     7  import (
     8  	"github.com/shogo82148/std/sync"
     9  	"github.com/shogo82148/std/syscall"
    10  )
    11  
    12  // InitWSA initiates the use of the Winsock DLL by the current process.
    13  // It is called from the net package at init time to avoid
    14  // loading ws2_32.dll when net is not used.
    15  var InitWSA = sync.OnceFunc(func() {
    16  	var d syscall.WSAData
    17  	e := syscall.WSAStartup(uint32(0x202), &d)
    18  	if e != nil {
    19  		initErr = e
    20  	}
    21  	checkSetFileCompletionNotificationModes()
    22  })
    23  
    24  // FD is a file descriptor. The net and os packages embed this type in
    25  // a larger type representing a network connection or OS file.
    26  type FD struct {
    27  	// Lock sysfd and serialize access to Read and Write methods.
    28  	fdmu fdMutex
    29  
    30  	// System file descriptor. Immutable until Close.
    31  	Sysfd syscall.Handle
    32  
    33  	// Read operation.
    34  	rop operation
    35  	// Write operation.
    36  	wop operation
    37  
    38  	// I/O poller.
    39  	pd pollDesc
    40  
    41  	// Used to implement pread/pwrite.
    42  	l sync.Mutex
    43  
    44  	// For console I/O.
    45  	lastbits       []byte
    46  	readuint16     []uint16
    47  	readbyte       []byte
    48  	readbyteOffset int
    49  
    50  	// Semaphore signaled when file is closed.
    51  	csema uint32
    52  
    53  	skipSyncNotif bool
    54  
    55  	// Whether this is a streaming descriptor, as opposed to a
    56  	// packet-based descriptor like a UDP socket.
    57  	IsStream bool
    58  
    59  	// Whether a zero byte read indicates EOF. This is false for a
    60  	// message based socket connection.
    61  	ZeroReadIsEOF bool
    62  
    63  	// Whether this is a file rather than a network socket.
    64  	isFile bool
    65  
    66  	// The kind of this file.
    67  	kind fileKind
    68  }
    69  
    70  // Init initializes the FD. The Sysfd field should already be set.
    71  // This can be called multiple times on a single FD.
    72  // The net argument is a network name from the net package (e.g., "tcp"),
    73  // or "file" or "console" or "dir".
    74  // Set pollable to true if fd should be managed by runtime netpoll.
    75  func (fd *FD) Init(net string, pollable bool) (string, error)
    76  
    77  // Close closes the FD. The underlying file descriptor is closed by
    78  // the destroy method when there are no remaining references.
    79  func (fd *FD) Close() error
    80  
    81  // Read implements io.Reader.
    82  func (fd *FD) Read(buf []byte) (int, error)
    83  
    84  var ReadConsole = syscall.ReadConsole
    85  
    86  // Pread emulates the Unix pread system call.
    87  func (fd *FD) Pread(b []byte, off int64) (int, error)
    88  
    89  // ReadFrom wraps the recvfrom network call.
    90  func (fd *FD) ReadFrom(buf []byte) (int, syscall.Sockaddr, error)
    91  
    92  // ReadFromInet4 wraps the recvfrom network call for IPv4.
    93  func (fd *FD) ReadFromInet4(buf []byte, sa4 *syscall.SockaddrInet4) (int, error)
    94  
    95  // ReadFromInet6 wraps the recvfrom network call for IPv6.
    96  func (fd *FD) ReadFromInet6(buf []byte, sa6 *syscall.SockaddrInet6) (int, error)
    97  
    98  // Write implements io.Writer.
    99  func (fd *FD) Write(buf []byte) (int, error)
   100  
   101  // Pwrite emulates the Unix pwrite system call.
   102  func (fd *FD) Pwrite(buf []byte, off int64) (int, error)
   103  
   104  // Writev emulates the Unix writev system call.
   105  func (fd *FD) Writev(buf *[][]byte) (int64, error)
   106  
   107  // WriteTo wraps the sendto network call.
   108  func (fd *FD) WriteTo(buf []byte, sa syscall.Sockaddr) (int, error)
   109  
   110  // WriteToInet4 is WriteTo, specialized for syscall.SockaddrInet4.
   111  func (fd *FD) WriteToInet4(buf []byte, sa4 *syscall.SockaddrInet4) (int, error)
   112  
   113  // WriteToInet6 is WriteTo, specialized for syscall.SockaddrInet6.
   114  func (fd *FD) WriteToInet6(buf []byte, sa6 *syscall.SockaddrInet6) (int, error)
   115  
   116  // Call ConnectEx. This doesn't need any locking, since it is only
   117  // called when the descriptor is first created. This is here rather
   118  // than in the net package so that it can use fd.wop.
   119  func (fd *FD) ConnectEx(ra syscall.Sockaddr) error
   120  
   121  // Accept handles accepting a socket. The sysSocket parameter is used
   122  // to allocate the net socket.
   123  func (fd *FD) Accept(sysSocket func() (syscall.Handle, error)) (syscall.Handle, []syscall.RawSockaddrAny, uint32, string, error)
   124  
   125  // Seek wraps syscall.Seek.
   126  func (fd *FD) Seek(offset int64, whence int) (int64, error)
   127  
   128  // Fchmod updates syscall.ByHandleFileInformation.Fileattributes when needed.
   129  func (fd *FD) Fchmod(mode uint32) error
   130  
   131  // Fchdir wraps syscall.Fchdir.
   132  func (fd *FD) Fchdir() error
   133  
   134  // GetFileType wraps syscall.GetFileType.
   135  func (fd *FD) GetFileType() (uint32, error)
   136  
   137  // GetFileInformationByHandle wraps GetFileInformationByHandle.
   138  func (fd *FD) GetFileInformationByHandle(data *syscall.ByHandleFileInformation) error
   139  
   140  // RawRead invokes the user-defined function f for a read operation.
   141  func (fd *FD) RawRead(f func(uintptr) bool) error
   142  
   143  // RawWrite invokes the user-defined function f for a write operation.
   144  func (fd *FD) RawWrite(f func(uintptr) bool) error
   145  
   146  // ReadMsg wraps the WSARecvMsg network call.
   147  func (fd *FD) ReadMsg(p []byte, oob []byte, flags int) (int, int, int, syscall.Sockaddr, error)
   148  
   149  // ReadMsgInet4 is ReadMsg, but specialized to return a syscall.SockaddrInet4.
   150  func (fd *FD) ReadMsgInet4(p []byte, oob []byte, flags int, sa4 *syscall.SockaddrInet4) (int, int, int, error)
   151  
   152  // ReadMsgInet6 is ReadMsg, but specialized to return a syscall.SockaddrInet6.
   153  func (fd *FD) ReadMsgInet6(p []byte, oob []byte, flags int, sa6 *syscall.SockaddrInet6) (int, int, int, error)
   154  
   155  // WriteMsg wraps the WSASendMsg network call.
   156  func (fd *FD) WriteMsg(p []byte, oob []byte, sa syscall.Sockaddr) (int, int, error)
   157  
   158  // WriteMsgInet4 is WriteMsg specialized for syscall.SockaddrInet4.
   159  func (fd *FD) WriteMsgInet4(p []byte, oob []byte, sa *syscall.SockaddrInet4) (int, int, error)
   160  
   161  // WriteMsgInet6 is WriteMsg specialized for syscall.SockaddrInet6.
   162  func (fd *FD) WriteMsgInet6(p []byte, oob []byte, sa *syscall.SockaddrInet6) (int, int, error)
   163  
   164  func DupCloseOnExec(fd int) (int, string, error)