Function WSASocketW [src]
Microsoft requires WSAStartup to be called to initialize, or else
WSASocketW will return WSANOTINITIALISED.
Since this is a standard library, we do not have the luxury of
putting initialization code anywhere, because we would not want
to pay the cost of calling WSAStartup if there ended up being no
networking. Also, if Zig code is used as a library, Zig is not in
charge of the start code, and we couldn't put in any initialization
code even if we wanted to.
The documentation for WSAStartup mentions that there must be a
matching WSACleanup call. It is not possible for the Zig Standard
Library to honor this for the same reason - there is nowhere to put
deinitialization code.
So, API users of the zig std lib have two options:
(recommended) The simple, cross-platform way: just call WSASocketW
and don't worry about it. Zig will call WSAStartup() in a thread-safe
manner and never deinitialize networking. This is ideal for an
application which has the capability to do networking.
The getting-your-hands-dirty way: call WSAStartup() before doing
networking, so that the error handling code for WSANOTINITIALISED never
gets run, which then allows the application or library to call WSACleanup().
This could make sense for a library, which has init and deinit
functions for the whole library's lifetime.
Prototype
pub fn WSASocketW( af: i32, socket_type: i32, protocol: i32, protocolInfo: ?*ws2_32.WSAPROTOCOL_INFOW, g: ws2_32.GROUP, dwFlags: DWORD, ) !ws2_32.SOCKET
Parameters
af: i32
socket_type: i32
protocol: i32
protocolInfo: ?*ws2_32.WSAPROTOCOL_INFOW
g: ws2_32.GROUP
dwFlags: DWORD
Source
pub fn WSASocketW(
af: i32,
socket_type: i32,
protocol: i32,
protocolInfo: ?*ws2_32.WSAPROTOCOL_INFOW,
g: ws2_32.GROUP,
dwFlags: DWORD,
) !ws2_32.SOCKET {
var first = true;
while (true) {
const rc = ws2_32.WSASocketW(af, socket_type, protocol, protocolInfo, g, dwFlags);
if (rc == ws2_32.INVALID_SOCKET) {
switch (ws2_32.WSAGetLastError()) {
.WSAEAFNOSUPPORT => return error.AddressFamilyNotSupported,
.WSAEMFILE => return error.ProcessFdQuotaExceeded,
.WSAENOBUFS => return error.SystemResources,
.WSAEPROTONOSUPPORT => return error.ProtocolNotSupported,
.WSANOTINITIALISED => {
if (!first) return error.Unexpected;
first = false;
try callWSAStartup();
continue;
},
else => |err| return unexpectedWSAError(err),
}
}
return rc;
}
}