Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions lib/std/net.zig
Original file line number Diff line number Diff line change
Expand Up @@ -857,8 +857,8 @@ pub const TcpConnectToAddressError = posix.SocketError || posix.ConnectError;

pub fn tcpConnectToAddress(address: Address) TcpConnectToAddressError!Stream {
const nonblock = 0;
const sock_flags = posix.SOCK.STREAM | nonblock |
(if (native_os == .windows) 0 else posix.SOCK.CLOEXEC);
const winFlag = (if (native_os == .windows) 0 else posix.SOCK.CLOEXEC);
const sock_flags = posix.SOCK.DGRAM | nonblock | winFlag;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zig prefers snake_case for variable names (win_flag, not winFlag).

winFlag is also not a great name, considering its a flag that is applied to non-windows systems.

SOCK.STREAM shouldn't have been changed to SOCK.DGRAM; this is TCP.

const sockfd = try posix.socket(address.any.family, sock_flags, posix.IPPROTO.TCP);
errdefer Stream.close(.{ .handle = sockfd });

Expand All @@ -867,6 +867,22 @@ pub fn tcpConnectToAddress(address: Address) TcpConnectToAddressError!Stream {
return Stream{ .handle = sockfd };
}


pub const UdpConnectToAddressError = posix.SocketError || posix.ConnectError;

pub fn udpConnectToAddress(address: Address) UdpConnectToAddressError!Stream {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UDP is connectionless so it doesn't make sense to connect via UDP. Returning a stream also doesn't make sense, as UDP transmits individual datagrams and not a continuous stream of data.

const nonblock = 0;
const winFlag = (if (native_os == .windows) 0 else posix.SOCK.CLOEXEC);
const sock_flags = posix.SOCK.DGRAM | nonblock | winFlag;
const sockfd = try posix.socket(address.any.family, sock_flags, posix.IPPROTO.UDP);
errdefer Stream.close(.{ .handle = sockfd });

try posix.connect(sockfd, &address.any, address.getOsSockLen());

return Stream{ .handle = sockfd };
}


// TODO: Instead of having a massive error set, make the error set have categories, and then
// store the sub-error as a diagnostic value.
const GetAddressListError = Allocator.Error || File.OpenError || File.ReadError || posix.SocketError || posix.BindError || posix.SetSockOptError || error{
Expand Down