-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement vecnet.ReadFrom on Windows #54
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright 2018 The gVisor Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package vecnet | ||
|
||
import ( | ||
"io" | ||
"syscall" | ||
|
||
"golang.org/x/sys/windows" | ||
) | ||
|
||
var readFromBuffers = readFromBuffersWindows | ||
|
||
func readFromBuffersWindows(bufs Buffers, conn syscall.Conn) (int64, error) { | ||
rc, err := conn.SyscallConn() | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
length := int64(0) | ||
for _, buf := range bufs { | ||
length += int64(len(buf)) | ||
} | ||
for n := int64(0); n < length; { | ||
cur, err := recvmsg(bufs, rc) | ||
if err != nil && (cur == 0 || err != io.EOF) { | ||
return n, err | ||
} | ||
n += int64(cur) | ||
|
||
// Consume buffers to retry. | ||
for consumed := 0; consumed < cur; { | ||
if len(bufs[0]) <= cur-consumed { | ||
consumed += len(bufs[0]) | ||
bufs = bufs[1:] | ||
} else { | ||
bufs[0] = bufs[0][cur-consumed:] | ||
break | ||
} | ||
} | ||
} | ||
return length, nil | ||
} | ||
|
||
func buildWSABufs(bufs Buffers, WSABufs []windows.WSABuf) []windows.WSABuf { | ||
for _, buf := range bufs { | ||
if l := len(buf); l > 0 { | ||
WSABufs = append(WSABufs, windows.WSABuf{ | ||
Len: uint32(l), | ||
Buf: &buf[0], | ||
}) | ||
} | ||
} | ||
return WSABufs | ||
} | ||
|
||
func recvmsg(bufs Buffers, rc syscall.RawConn) (int, error) { | ||
var ( | ||
bytesReceived uint32 | ||
WSABufs = buildWSABufs(bufs, make([]windows.WSABuf, 0, 2)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Leaving a note that this magic If there's a better value (typical average bufcount?), it could be used here. But this is probably fine as-is. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I actually probably stole the 2 from gVisor and didn't think about it further. |
||
bufCount = len(bufs) | ||
|
||
msg = windows.WSAMsg{ | ||
Buffers: &WSABufs[0], | ||
BufferCount: uint32(bufCount), | ||
} | ||
recvmsgCallBack = func(fd uintptr) bool { | ||
winErr := windows.WSARecvMsg( | ||
windows.Handle(fd), | ||
&msg, &bytesReceived, | ||
nil, nil) // TODO: overlapped structure? | ||
if winErr != nil { | ||
// TODO: double check documentation for other temporary issues | ||
// retry if err is temporary | ||
canRetry := (winErr == windows.WSAEINTR || winErr == windows.WSAEWOULDBLOCK) | ||
return !canRetry | ||
} | ||
return true | ||
} | ||
err = rc.Read(recvmsgCallBack) | ||
) | ||
return int(bytesReceived), err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
UDP was chosen here mainly for compat and simplicity, but could easily be changed to use Unix domain sockets instead.
I could be wrong, but I don't think the message passing semantics differ between them (especially when local).
In addition, (modern) Unices and Windows typically support UDS, so instead of letting the system choose a UDP port for us, we could create (and cleanup) socket files during the test. But I don't think we have to make this change, so I won't unless requested.
It should be noted that pkg
net
(as of 1.19) implies all theMsgX
methods are not implemented on Plan9 regardless of transport.