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.
10 "internal/syscall/windows"
25 // CancelIo Windows API cancels all outstanding IO for a particular
26 // socket on current thread. To overcome that limitation, we run
27 // special goroutine, locked to OS single thread, that both starts
28 // and cancels IO. It means, there are 2 unavoidable thread switches
30 // Some newer versions of Windows has new CancelIoEx API, that does
31 // not have that limitation and can be used from any thread. This
32 // package uses CancelIoEx API, if present, otherwise it fallback
35 var canCancelIO
bool // determines if CancelIoEx API is present
37 // This package uses SetFileCompletionNotificationModes Windows API
38 // to skip calling GetQueuedCompletionStatus if an IO operation completes
39 // synchronously. Unfortuently SetFileCompletionNotificationModes is not
40 // available on Windows XP. Also there is a known bug where
41 // SetFileCompletionNotificationModes crashes on some systems
42 // (see http://support.microsoft.com/kb/2568167 for details).
44 var useSetFileCompletionNotificationModes
bool // determines is SetFileCompletionNotificationModes is present and safe to use
46 // checkSetFileCompletionNotificationModes verifies that
47 // SetFileCompletionNotificationModes Windows API is present
48 // on the system and is safe to use.
49 // See http://support.microsoft.com/kb/2568167 for details.
50 func checkSetFileCompletionNotificationModes() {
51 err
:= syscall
.LoadSetFileCompletionNotificationModes()
55 protos
:= [2]int32{syscall
.IPPROTO_TCP
, 0}
56 var buf
[32]syscall
.WSAProtocolInfo
57 len := uint32(unsafe
.Sizeof(buf
))
58 n
, err
:= syscall
.WSAEnumProtocols(&protos
[0], &buf
[0], &len)
62 for i
:= int32(0); i
< n
; i
++ {
63 if buf
[i
].ServiceFlags1
&syscall
.XP1_IFS_HANDLES
== 0 {
67 useSetFileCompletionNotificationModes
= true
72 e
:= syscall
.WSAStartup(uint32(0x202), &d
)
76 canCancelIO
= syscall
.LoadCancelIoEx() == nil
77 checkSetFileCompletionNotificationModes()
80 // operation contains superset of data necessary to perform all async IO.
81 type operation
struct {
82 // Used by IOCP interface, it must be first field
83 // of the struct, as our code rely on it.
86 // fields used by runtime.netpoll
92 // fields used only by net package
98 rsa
*syscall
.RawSockaddrAny
100 handle syscall
.Handle
102 bufs
[]syscall
.WSABuf
105 func (o
*operation
) InitBuf(buf
[]byte) {
106 o
.buf
.Len
= uint32(len(buf
))
113 func (o
*operation
) InitBufs(buf
*[][]byte) {
115 o
.bufs
= make([]syscall
.WSABuf
, 0, len(*buf
))
119 for _
, b
:= range *buf
{
124 o
.bufs
= append(o
.bufs
, syscall
.WSABuf
{Len
: uint32(len(b
)), Buf
: p
})
128 // ClearBufs clears all pointers to Buffers parameter captured
129 // by InitBufs, so it can be released by garbage collector.
130 func (o
*operation
) ClearBufs() {
131 for i
:= range o
.bufs
{
137 func (o
*operation
) InitMsg(p
[]byte, oob
[]byte) {
139 o
.msg
.Buffers
= &o
.buf
140 o
.msg
.BufferCount
= 1
146 o
.msg
.Control
.Len
= uint32(len(oob
))
147 o
.msg
.Control
.Buf
= nil
149 o
.msg
.Control
.Buf
= &oob
[0]
153 // ioSrv executes net IO requests.
158 type ioSrvReq
struct {
160 submit
func(o
*operation
) error
// if nil, cancel the operation
163 // ProcessRemoteIO will execute submit IO requests on behalf
164 // of other goroutines, all on a single os thread, so it can
165 // cancel them later. Results of all operations will be sent
166 // back to their requesters via channel supplied in request.
167 // It is used only when the CancelIoEx API is unavailable.
168 func (s
*ioSrv
) ProcessRemoteIO() {
169 runtime
.LockOSThread()
170 defer runtime
.UnlockOSThread()
171 for r
:= range s
.req
{
173 r
.o
.errc
<- r
.submit(r
.o
)
175 r
.o
.errc
<- syscall
.CancelIo(r
.o
.fd
.Sysfd
)
180 // ExecIO executes a single IO operation o. It submits and cancels
181 // IO in the current thread for systems where Windows CancelIoEx API
182 // is available. Alternatively, it passes the request onto
183 // runtime netpoll and waits for completion or cancels request.
184 func (s
*ioSrv
) ExecIO(o
*operation
, submit
func(o
*operation
) error
) (int, error
) {
185 if o
.fd
.pd
.runtimeCtx
== 0 {
186 return 0, errors
.New("internal error: polling on unsupported descriptor type")
190 onceStartServer
.Do(startServer
)
194 // Notify runtime netpoll about starting IO.
195 err
:= fd
.pd
.prepare(int(o
.mode
), fd
.isFile
)
203 // Send request to a special dedicated thread,
204 // so it can stop the IO with CancelIO later.
205 s
.req
<- ioSrvReq
{o
, submit
}
210 // IO completed immediately
211 if o
.fd
.skipSyncNotif
{
212 // No completion message will follow, so return immediately.
213 return int(o
.qty
), nil
215 // Need to get our completion message anyway.
216 case syscall
.ERROR_IO_PENDING
:
217 // IO started, and we have to wait for its completion.
222 // Wait for our request to complete.
223 err
= fd
.pd
.wait(int(o
.mode
), fd
.isFile
)
225 // All is good. Extract our IO results and return.
227 err
= syscall
.Errno(o
.errno
)
230 return int(o
.qty
), nil
232 // IO is interrupted by "close" or "timeout"
235 case ErrNetClosing
, ErrFileClosing
, ErrTimeout
:
236 // will deal with those.
238 panic("unexpected runtime.netpoll error: " + netpollErr
.Error())
240 // Cancel our request.
242 err
:= syscall
.CancelIoEx(fd
.Sysfd
, &o
.o
)
243 // Assuming ERROR_NOT_FOUND is returned, if IO is completed.
244 if err
!= nil && err
!= syscall
.ERROR_NOT_FOUND
{
245 // TODO(brainman): maybe do something else, but panic.
249 s
.req
<- ioSrvReq
{o
, nil}
252 // Wait for cancelation to complete.
253 fd
.pd
.waitCanceled(int(o
.mode
))
255 err
= syscall
.Errno(o
.errno
)
256 if err
== syscall
.ERROR_OPERATION_ABORTED
{ // IO Canceled
261 // We issued a cancelation request. But, it seems, IO operation succeeded
262 // before the cancelation request run. We need to treat the IO operation as
263 // succeeded (the bytes are actually sent/recv from network).
264 return int(o
.qty
), nil
267 // Start helper goroutines.
269 var onceStartServer sync
.Once
272 // This is called, once, when only the CancelIo API is available.
273 // Start two special goroutines, both locked to an OS thread,
274 // that start and cancel IO requests.
275 // One will process read requests, while the other will do writes.
276 rsrv
.req
= make(chan ioSrvReq
)
277 go rsrv
.ProcessRemoteIO()
278 wsrv
.req
= make(chan ioSrvReq
)
279 go wsrv
.ProcessRemoteIO()
282 // FD is a file descriptor. The net and os packages embed this type in
283 // a larger type representing a network connection or OS file.
285 // Lock sysfd and serialize access to Read and Write methods.
288 // System file descriptor. Immutable until Close.
299 // Used to implement pread/pwrite.
304 lastbits
[]byte // first few bytes of the last incomplete rune in last write
305 readuint16
[]uint16 // buffer to hold uint16s obtained with ReadConsole
306 readbyte
[]byte // buffer to hold decoding of readuint16 from utf16 to utf8
307 readbyteOffset
int // readbyte[readOffset:] is yet to be consumed with file.Read
309 // Semaphore signaled when file is closed.
314 // Whether this is a streaming descriptor, as opposed to a
315 // packet-based descriptor like a UDP socket.
318 // Whether a zero byte read indicates EOF. This is false for a
319 // message based socket connection.
322 // Whether this is a normal file.
325 // Whether this is a directory.
329 // logInitFD is set by tests to enable file descriptor initialization logging.
330 var logInitFD
func(net
string, fd
*FD
, err error
)
332 // Init initializes the FD. The Sysfd field should already be set.
333 // This can be called multiple times on a single FD.
334 // The net argument is a network name from the net package (e.g., "tcp"),
335 // or "file" or "console" or "dir".
336 // Set pollable to true if fd should be managed by runtime netpoll.
337 func (fd
*FD
) Init(net
string, pollable
bool) (string, error
) {
349 case "tcp", "tcp4", "tcp6":
350 case "udp", "udp4", "udp6":
351 case "ip", "ip4", "ip6":
352 case "unix", "unixgram", "unixpacket":
354 return "", errors
.New("internal error: unknown network type " + net
)
359 // Only call init for a network socket.
360 // This means that we don't add files to the runtime poller.
361 // Adding files to the runtime poller can confuse matters
362 // if the user is doing their own overlapped I/O.
365 // In general the code below avoids calling the ExecIO
366 // method for non-network sockets. If some method does
367 // somehow call ExecIO, then ExecIO, and therefore the
368 // calling method, will return an error, because
369 // fd.pd.runtimeCtx will be 0.
372 if logInitFD
!= nil {
373 logInitFD(net
, fd
, err
)
378 if pollable
&& useSetFileCompletionNotificationModes
{
379 // We do not use events, so we can skip them always.
380 flags
:= uint8(syscall
.FILE_SKIP_SET_EVENT_ON_HANDLE
)
381 // It's not safe to skip completion notifications for UDP:
382 // http://blogs.technet.com/b/winserverperformance/archive/2008/06/26/designing-applications-for-high-performance-part-iii.aspx
384 flags |
= syscall
.FILE_SKIP_COMPLETION_PORT_ON_SUCCESS
386 err
:= syscall
.SetFileCompletionNotificationModes(fd
.Sysfd
, flags
)
387 if err
== nil && flags
&syscall
.FILE_SKIP_COMPLETION_PORT_ON_SUCCESS
!= 0 {
388 fd
.skipSyncNotif
= true
391 // Disable SIO_UDP_CONNRESET behavior.
392 // http://support.microsoft.com/kb/263823
394 case "udp", "udp4", "udp6":
397 size
:= uint32(unsafe
.Sizeof(flag
))
398 err
:= syscall
.WSAIoctl(fd
.Sysfd
, syscall
.SIO_UDP_CONNRESET
, (*byte)(unsafe
.Pointer(&flag
)), size
, nil, 0, &ret
, nil, 0)
400 return "wsaioctl", err
407 fd
.rop
.runtimeCtx
= fd
.pd
.runtimeCtx
408 fd
.wop
.runtimeCtx
= fd
.pd
.runtimeCtx
410 fd
.rop
.errc
= make(chan error
)
411 fd
.wop
.errc
= make(chan error
)
416 func (fd
*FD
) destroy() error
{
417 if fd
.Sysfd
== syscall
.InvalidHandle
{
418 return syscall
.EINVAL
420 // Poller may want to unregister fd in readiness notification mechanism,
421 // so this must be executed before fd.CloseFunc.
424 if fd
.isFile || fd
.isConsole
{
425 err
= syscall
.CloseHandle(fd
.Sysfd
)
427 err
= syscall
.FindClose(fd
.Sysfd
)
429 // The net package uses the CloseFunc variable for testing.
430 err
= CloseFunc(fd
.Sysfd
)
432 fd
.Sysfd
= syscall
.InvalidHandle
433 runtime_Semrelease(&fd
.csema
)
437 // Close closes the FD. The underlying file descriptor is closed by
438 // the destroy method when there are no remaining references.
439 func (fd
*FD
) Close() error
{
440 if !fd
.fdmu
.increfAndClose() {
441 return errClosing(fd
.isFile
)
443 // unblock pending reader and writer
446 // Wait until the descriptor is closed. If this was the only
447 // reference, it is already closed.
448 runtime_Semacquire(&fd
.csema
)
452 // Shutdown wraps the shutdown network call.
453 func (fd
*FD
) Shutdown(how
int) error
{
454 if err
:= fd
.incref(); err
!= nil {
458 return syscall
.Shutdown(fd
.Sysfd
, how
)
461 // Read implements io.Reader.
462 func (fd
*FD
) Read(buf
[]byte) (int, error
) {
463 if err
:= fd
.readLock(); err
!= nil {
466 defer fd
.readUnlock()
470 if fd
.isFile || fd
.isDir || fd
.isConsole
{
474 n
, err
= fd
.readConsole(buf
)
476 n
, err
= syscall
.Read(fd
.Sysfd
, buf
)
484 n
, err
= rsrv
.ExecIO(o
, func(o
*operation
) error
{
485 return syscall
.WSARecv(o
.fd
.Sysfd
, &o
.buf
, 1, &o
.qty
, &o
.flags
, &o
.o
, nil)
488 race
.Acquire(unsafe
.Pointer(&ioSync
))
492 err
= fd
.eofError(n
, err
)
497 var ReadConsole
= syscall
.ReadConsole
// changed for testing
499 // readConsole reads utf16 characters from console File,
500 // encodes them into utf8 and stores them in buffer b.
501 // It returns the number of utf8 bytes read and an error, if any.
502 func (fd
*FD
) readConsole(b
[]byte) (int, error
) {
507 if fd
.readuint16
== nil {
508 // Note: syscall.ReadConsole fails for very large buffers.
509 // The limit is somewhere around (but not exactly) 16384.
511 fd
.readuint16
= make([]uint16, 0, 10000)
512 fd
.readbyte
= make([]byte, 0, 4*cap(fd
.readuint16
))
515 for fd
.readbyteOffset
>= len(fd
.readbyte
) {
516 n
:= cap(fd
.readuint16
) - len(fd
.readuint16
)
521 err
:= ReadConsole(fd
.Sysfd
, &fd
.readuint16
[:len(fd
.readuint16
)+1][len(fd
.readuint16
)], uint32(n
), &nw
, nil)
525 uint16s
:= fd
.readuint16
[:len(fd
.readuint16
)+int(nw
)]
526 fd
.readuint16
= fd
.readuint16
[:0]
527 buf
:= fd
.readbyte
[:0]
528 for i
:= 0; i
< len(uint16s
); i
++ {
529 r
:= rune(uint16s
[i
])
530 if utf16
.IsSurrogate(r
) {
531 if i
+1 == len(uint16s
) {
533 // Save half surrogate pair for next time.
534 fd
.readuint16
= fd
.readuint16
[:1]
535 fd
.readuint16
[0] = uint16(r
)
540 r
= utf16
.DecodeRune(r
, rune(uint16s
[i
+1]))
541 if r
!= utf8
.RuneError
{
546 n
:= utf8
.EncodeRune(buf
[len(buf
):cap(buf
)], r
)
547 buf
= buf
[:len(buf
)+n
]
550 fd
.readbyteOffset
= 0
556 src
:= fd
.readbyte
[fd
.readbyteOffset
:]
558 for i
= 0; i
< len(src
) && i
< len(b
); i
++ {
560 if x
== 0x1A { // Ctrl-Z
568 fd
.readbyteOffset
+= i
572 // Pread emulates the Unix pread system call.
573 func (fd
*FD
) Pread(b
[]byte, off
int64) (int, error
) {
574 // Call incref, not readLock, because since pread specifies the
575 // offset it is independent from other reads.
576 if err
:= fd
.incref(); err
!= nil {
583 curoffset
, e
:= syscall
.Seek(fd
.Sysfd
, 0, io
.SeekCurrent
)
587 defer syscall
.Seek(fd
.Sysfd
, curoffset
, io
.SeekStart
)
588 o
:= syscall
.Overlapped
{
589 OffsetHigh
: uint32(off
>> 32),
593 e
= syscall
.ReadFile(fd
.Sysfd
, b
, &done
, &o
)
596 if e
== syscall
.ERROR_HANDLE_EOF
{
601 e
= fd
.eofError(int(done
), e
)
606 // ReadFrom wraps the recvfrom network call.
607 func (fd
*FD
) ReadFrom(buf
[]byte) (int, syscall
.Sockaddr
, error
) {
611 if err
:= fd
.readLock(); err
!= nil {
614 defer fd
.readUnlock()
617 n
, err
:= rsrv
.ExecIO(o
, func(o
*operation
) error
{
619 o
.rsa
= new(syscall
.RawSockaddrAny
)
621 o
.rsan
= int32(unsafe
.Sizeof(*o
.rsa
))
622 return syscall
.WSARecvFrom(o
.fd
.Sysfd
, &o
.buf
, 1, &o
.qty
, &o
.flags
, o
.rsa
, &o
.rsan
, &o
.o
, nil)
624 err
= fd
.eofError(n
, err
)
628 sa
, _
:= o
.rsa
.Sockaddr()
632 // Write implements io.Writer.
633 func (fd
*FD
) Write(buf
[]byte) (int, error
) {
634 if err
:= fd
.writeLock(); err
!= nil {
637 defer fd
.writeUnlock()
641 if fd
.isFile || fd
.isDir || fd
.isConsole
{
645 n
, err
= fd
.writeConsole(buf
)
647 n
, err
= syscall
.Write(fd
.Sysfd
, buf
)
654 race
.ReleaseMerge(unsafe
.Pointer(&ioSync
))
658 n
, err
= wsrv
.ExecIO(o
, func(o
*operation
) error
{
659 return syscall
.WSASend(o
.fd
.Sysfd
, &o
.buf
, 1, &o
.qty
, 0, &o
.o
, nil)
665 // writeConsole writes len(b) bytes to the console File.
666 // It returns the number of bytes written and an error, if any.
667 func (fd
*FD
) writeConsole(b
[]byte) (int, error
) {
669 runes
:= make([]rune
, 0, 256)
670 if len(fd
.lastbits
) > 0 {
671 b
= append(fd
.lastbits
, b
...)
675 for len(b
) >= utf8
.UTFMax || utf8
.FullRune(b
) {
676 r
, l
:= utf8
.DecodeRune(b
)
677 runes
= append(runes
, r
)
681 fd
.lastbits
= make([]byte, len(b
))
684 // syscall.WriteConsole seems to fail, if given large buffer.
685 // So limit the buffer to 16000 characters. This number was
686 // discovered by experimenting with syscall.WriteConsole.
687 const maxWrite
= 16000
695 uint16s
:= utf16
.Encode(chunk
)
696 for len(uint16s
) > 0 {
698 err
:= syscall
.WriteConsole(fd
.Sysfd
, &uint16s
[0], uint32(len(uint16s
)), &written
, nil)
702 uint16s
= uint16s
[written
:]
708 // Pwrite emulates the Unix pwrite system call.
709 func (fd
*FD
) Pwrite(b
[]byte, off
int64) (int, error
) {
710 // Call incref, not writeLock, because since pwrite specifies the
711 // offset it is independent from other writes.
712 if err
:= fd
.incref(); err
!= nil {
719 curoffset
, e
:= syscall
.Seek(fd
.Sysfd
, 0, io
.SeekCurrent
)
723 defer syscall
.Seek(fd
.Sysfd
, curoffset
, io
.SeekStart
)
724 o
:= syscall
.Overlapped
{
725 OffsetHigh
: uint32(off
>> 32),
729 e
= syscall
.WriteFile(fd
.Sysfd
, b
, &done
, &o
)
733 return int(done
), nil
736 // Writev emulates the Unix writev system call.
737 func (fd
*FD
) Writev(buf
*[][]byte) (int64, error
) {
741 if err
:= fd
.writeLock(); err
!= nil {
744 defer fd
.writeUnlock()
746 race
.ReleaseMerge(unsafe
.Pointer(&ioSync
))
750 n
, err
:= wsrv
.ExecIO(o
, func(o
*operation
) error
{
751 return syscall
.WSASend(o
.fd
.Sysfd
, &o
.bufs
[0], uint32(len(o
.bufs
)), &o
.qty
, 0, &o
.o
, nil)
755 consume(buf
, int64(n
))
759 // WriteTo wraps the sendto network call.
760 func (fd
*FD
) WriteTo(buf
[]byte, sa syscall
.Sockaddr
) (int, error
) {
764 if err
:= fd
.writeLock(); err
!= nil {
767 defer fd
.writeUnlock()
771 n
, err
:= wsrv
.ExecIO(o
, func(o
*operation
) error
{
772 return syscall
.WSASendto(o
.fd
.Sysfd
, &o
.buf
, 1, &o
.qty
, 0, o
.sa
, &o
.o
, nil)
777 // Call ConnectEx. This doesn't need any locking, since it is only
778 // called when the descriptor is first created. This is here rather
779 // than in the net package so that it can use fd.wop.
780 func (fd
*FD
) ConnectEx(ra syscall
.Sockaddr
) error
{
783 _
, err
:= wsrv
.ExecIO(o
, func(o
*operation
) error
{
784 return ConnectExFunc(o
.fd
.Sysfd
, o
.sa
, nil, 0, nil, &o
.o
)
789 func (fd
*FD
) acceptOne(s syscall
.Handle
, rawsa
[]syscall
.RawSockaddrAny
, o
*operation
) (string, error
) {
790 // Submit accept request.
792 o
.rsan
= int32(unsafe
.Sizeof(rawsa
[0]))
793 _
, err
:= rsrv
.ExecIO(o
, func(o
*operation
) error
{
794 return AcceptFunc(o
.fd
.Sysfd
, o
.handle
, (*byte)(unsafe
.Pointer(&rawsa
[0])), 0, uint32(o
.rsan
), uint32(o
.rsan
), &o
.qty
, &o
.o
)
798 return "acceptex", err
801 // Inherit properties of the listening socket.
802 err
= syscall
.Setsockopt(s
, syscall
.SOL_SOCKET
, syscall
.SO_UPDATE_ACCEPT_CONTEXT
, (*byte)(unsafe
.Pointer(&fd
.Sysfd
)), int32(unsafe
.Sizeof(fd
.Sysfd
)))
805 return "setsockopt", err
811 // Accept handles accepting a socket. The sysSocket parameter is used
812 // to allocate the net socket.
813 func (fd
*FD
) Accept(sysSocket
func() (syscall
.Handle
, error
)) (syscall
.Handle
, []syscall
.RawSockaddrAny
, uint32, string, error
) {
814 if err
:= fd
.readLock(); err
!= nil {
815 return syscall
.InvalidHandle
, nil, 0, "", err
817 defer fd
.readUnlock()
820 var rawsa
[2]syscall
.RawSockaddrAny
822 s
, err
:= sysSocket()
824 return syscall
.InvalidHandle
, nil, 0, "", err
827 errcall
, err
:= fd
.acceptOne(s
, rawsa
[:], o
)
829 return s
, rawsa
[:], uint32(o
.rsan
), "", nil
832 // Sometimes we see WSAECONNRESET and ERROR_NETNAME_DELETED is
833 // returned here. These happen if connection reset is received
834 // before AcceptEx could complete. These errors relate to new
835 // connection, not to AcceptEx, so ignore broken connection and
836 // try AcceptEx again for more connections.
837 errno
, ok
:= err
.(syscall
.Errno
)
839 return syscall
.InvalidHandle
, nil, 0, errcall
, err
842 case syscall
.ERROR_NETNAME_DELETED
, syscall
.WSAECONNRESET
:
843 // ignore these and try again
845 return syscall
.InvalidHandle
, nil, 0, errcall
, err
850 // Seek wraps syscall.Seek.
851 func (fd
*FD
) Seek(offset
int64, whence
int) (int64, error
) {
852 if err
:= fd
.incref(); err
!= nil {
860 return syscall
.Seek(fd
.Sysfd
, offset
, whence
)
863 // FindNextFile wraps syscall.FindNextFile.
864 func (fd
*FD
) FindNextFile(data
*syscall
.Win32finddata
) error
{
865 if err
:= fd
.incref(); err
!= nil {
869 return syscall
.FindNextFile(fd
.Sysfd
, data
)
872 // Fchdir wraps syscall.Fchdir.
873 func (fd
*FD
) Fchdir() error
{
874 if err
:= fd
.incref(); err
!= nil {
878 return syscall
.Fchdir(fd
.Sysfd
)
881 // GetFileType wraps syscall.GetFileType.
882 func (fd
*FD
) GetFileType() (uint32, error
) {
883 if err
:= fd
.incref(); err
!= nil {
887 return syscall
.GetFileType(fd
.Sysfd
)
890 // GetFileInformationByHandle wraps GetFileInformationByHandle.
891 func (fd
*FD
) GetFileInformationByHandle(data
*syscall
.ByHandleFileInformation
) error
{
892 if err
:= fd
.incref(); err
!= nil {
896 return syscall
.GetFileInformationByHandle(fd
.Sysfd
, data
)
899 // RawControl invokes the user-defined function f for a non-IO
901 func (fd
*FD
) RawControl(f
func(uintptr)) error
{
902 if err
:= fd
.incref(); err
!= nil {
910 // RawRead invokes the user-defined function f for a read operation.
911 func (fd
*FD
) RawRead(f
func(uintptr) bool) error
{
912 return errors
.New("not implemented")
915 // RawWrite invokes the user-defined function f for a write operation.
916 func (fd
*FD
) RawWrite(f
func(uintptr) bool) error
{
917 return errors
.New("not implemented")
920 func sockaddrToRaw(sa syscall
.Sockaddr
) (unsafe
.Pointer
, int32, error
) {
921 switch sa
:= sa
.(type) {
922 case *syscall
.SockaddrInet4
:
923 var raw syscall
.RawSockaddrInet4
924 raw
.Family
= syscall
.AF_INET
925 p
:= (*[2]byte)(unsafe
.Pointer(&raw
.Port
))
926 p
[0] = byte(sa
.Port
>> 8)
928 for i
:= 0; i
< len(sa
.Addr
); i
++ {
929 raw
.Addr
[i
] = sa
.Addr
[i
]
931 return unsafe
.Pointer(&raw
), int32(unsafe
.Sizeof(raw
)), nil
932 case *syscall
.SockaddrInet6
:
933 var raw syscall
.RawSockaddrInet6
934 raw
.Family
= syscall
.AF_INET6
935 p
:= (*[2]byte)(unsafe
.Pointer(&raw
.Port
))
936 p
[0] = byte(sa
.Port
>> 8)
938 raw
.Scope_id
= sa
.ZoneId
939 for i
:= 0; i
< len(sa
.Addr
); i
++ {
940 raw
.Addr
[i
] = sa
.Addr
[i
]
942 return unsafe
.Pointer(&raw
), int32(unsafe
.Sizeof(raw
)), nil
944 return nil, 0, syscall
.EWINDOWS
948 // ReadMsg wraps the WSARecvMsg network call.
949 func (fd
*FD
) ReadMsg(p
[]byte, oob
[]byte) (int, int, int, syscall
.Sockaddr
, error
) {
950 if err
:= fd
.readLock(); err
!= nil {
951 return 0, 0, 0, nil, err
953 defer fd
.readUnlock()
957 o
.rsa
= new(syscall
.RawSockaddrAny
)
959 o
.msg
.Namelen
= int32(unsafe
.Sizeof(*o
.rsa
))
960 n
, err
:= rsrv
.ExecIO(o
, func(o
*operation
) error
{
961 return windows
.WSARecvMsg(o
.fd
.Sysfd
, &o
.msg
, &o
.qty
, &o
.o
, nil)
963 err
= fd
.eofError(n
, err
)
964 var sa syscall
.Sockaddr
966 sa
, err
= o
.rsa
.Sockaddr()
968 return n
, int(o
.msg
.Control
.Len
), int(o
.msg
.Flags
), sa
, err
971 // WriteMsg wraps the WSASendMsg network call.
972 func (fd
*FD
) WriteMsg(p
[]byte, oob
[]byte, sa syscall
.Sockaddr
) (int, int, error
) {
973 if err
:= fd
.writeLock(); err
!= nil {
976 defer fd
.writeUnlock()
981 rsa
, len, err
:= sockaddrToRaw(sa
)
985 o
.msg
.Name
= (*syscall
.RawSockaddrAny
)(rsa
)
988 n
, err
:= wsrv
.ExecIO(o
, func(o
*operation
) error
{
989 return windows
.WSASendMsg(o
.fd
.Sysfd
, &o
.msg
, 0, &o
.qty
, &o
.o
, nil)
991 return n
, int(o
.msg
.Control
.Len
), err