| author | |
| committer | |
| log | 9c08a33b2226239b8e0cf08ebcef17d710a54d8a |
| tree | dd55d02ef4c45ffdeeb9b003b0bf7bd3456e7fd0 |
| parent | 05b677f0c484181bcbd7eb86b41a70b8e508644b |
| parent | 4909aa1da43d227ad85b2fe03a58ef1a8c12b769 |
| signature |
x/io, x/os: async i/o reactor, cross-platform socket syscalls and bits24 files changed, 646 insertions(+), 203 deletions(-)
lib/std/atomic.zig+3-3| ... | ... | @@ -19,7 +19,7 @@ test "std.atomic" { |
| 19 | 19 | _ = @import("atomic/Atomic.zig"); |
| 20 | 20 | } |
| 21 | 21 | |
| 22 | pub fn fence(comptime ordering: Ordering) callconv(.Inline) void { | |
| 22 | pub inline fn fence(comptime ordering: Ordering) void { | |
| 23 | 23 | switch (ordering) { |
| 24 | 24 | .Acquire, .Release, .AcqRel, .SeqCst => { |
| 25 | 25 | @fence(ordering); |
| ... | ... | @@ -30,7 +30,7 @@ pub fn fence(comptime ordering: Ordering) callconv(.Inline) void { |
| 30 | 30 | } |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | pub fn compilerFence(comptime ordering: Ordering) callconv(.Inline) void { | |
| 33 | pub inline fn compilerFence(comptime ordering: Ordering) void { | |
| 34 | 34 | switch (ordering) { |
| 35 | 35 | .Acquire, .Release, .AcqRel, .SeqCst => asm volatile ("" ::: "memory"), |
| 36 | 36 | else => @compileLog(ordering, " only applies to a given memory location"), |
| ... | ... | @@ -45,7 +45,7 @@ test "fence/compilerFence" { |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | /// Signals to the processor that the caller is inside a busy-wait spin-loop. |
| 48 | pub fn spinLoopHint() callconv(.Inline) void { | |
| 48 | pub inline fn spinLoopHint() void { | |
| 49 | 49 | const hint_instruction = switch (target.cpu.arch) { |
| 50 | 50 | // No-op instruction that can hint to save (or share with a hardware-thread) pipelining/power resources |
| 51 | 51 | // https://software.intel.com/content/www/us/en/develop/articles/benefitting-power-and-performance-sleep-loops.html |
lib/std/atomic/Atomic.zig+22-22| ... | ... | @@ -48,38 +48,38 @@ pub fn Atomic(comptime T: type) type { |
| 48 | 48 | }; |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | pub fn swap(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T { | |
| 51 | pub inline fn swap(self: *Self, value: T, comptime ordering: Ordering) T { | |
| 52 | 52 | return self.rmw(.Xchg, value, ordering); |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | pub fn compareAndSwap( | |
| 55 | pub inline fn compareAndSwap( | |
| 56 | 56 | self: *Self, |
| 57 | 57 | compare: T, |
| 58 | 58 | exchange: T, |
| 59 | 59 | comptime success: Ordering, |
| 60 | 60 | comptime failure: Ordering, |
| 61 | ) callconv(.Inline) ?T { | |
| 61 | ) ?T { | |
| 62 | 62 | return self.cmpxchg(true, compare, exchange, success, failure); |
| 63 | 63 | } |
| 64 | 64 | |
| 65 | pub fn tryCompareAndSwap( | |
| 65 | pub inline fn tryCompareAndSwap( | |
| 66 | 66 | self: *Self, |
| 67 | 67 | compare: T, |
| 68 | 68 | exchange: T, |
| 69 | 69 | comptime success: Ordering, |
| 70 | 70 | comptime failure: Ordering, |
| 71 | ) callconv(.Inline) ?T { | |
| 71 | ) ?T { | |
| 72 | 72 | return self.cmpxchg(false, compare, exchange, success, failure); |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | fn cmpxchg( | |
| 75 | inline fn cmpxchg( | |
| 76 | 76 | self: *Self, |
| 77 | 77 | comptime is_strong: bool, |
| 78 | 78 | compare: T, |
| 79 | 79 | exchange: T, |
| 80 | 80 | comptime success: Ordering, |
| 81 | 81 | comptime failure: Ordering, |
| 82 | ) callconv(.Inline) ?T { | |
| 82 | ) ?T { | |
| 83 | 83 | if (success == .Unordered or failure == .Unordered) { |
| 84 | 84 | @compileError(@tagName(Ordering.Unordered) ++ " is only allowed on atomic loads and stores"); |
| 85 | 85 | } |
| ... | ... | @@ -103,12 +103,12 @@ pub fn Atomic(comptime T: type) type { |
| 103 | 103 | }; |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | fn rmw( | |
| 106 | inline fn rmw( | |
| 107 | 107 | self: *Self, |
| 108 | 108 | comptime op: std.builtin.AtomicRmwOp, |
| 109 | 109 | value: T, |
| 110 | 110 | comptime ordering: Ordering, |
| 111 | ) callconv(.Inline) T { | |
| 111 | ) T { | |
| 112 | 112 | return @atomicRmw(T, &self.value, op, value, ordering); |
| 113 | 113 | } |
| 114 | 114 | |
| ... | ... | @@ -117,37 +117,37 @@ pub fn Atomic(comptime T: type) type { |
| 117 | 117 | } |
| 118 | 118 | |
| 119 | 119 | pub usingnamespace exportWhen(std.meta.trait.isNumber(T), struct { |
| 120 | pub fn fetchAdd(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T { | |
| 120 | pub inline fn fetchAdd(self: *Self, value: T, comptime ordering: Ordering) T { | |
| 121 | 121 | return self.rmw(.Add, value, ordering); |
| 122 | 122 | } |
| 123 | 123 | |
| 124 | pub fn fetchSub(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T { | |
| 124 | pub inline fn fetchSub(self: *Self, value: T, comptime ordering: Ordering) T { | |
| 125 | 125 | return self.rmw(.Sub, value, ordering); |
| 126 | 126 | } |
| 127 | 127 | |
| 128 | pub fn fetchMin(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T { | |
| 128 | pub inline fn fetchMin(self: *Self, value: T, comptime ordering: Ordering) T { | |
| 129 | 129 | return self.rmw(.Min, value, ordering); |
| 130 | 130 | } |
| 131 | 131 | |
| 132 | pub fn fetchMax(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T { | |
| 132 | pub inline fn fetchMax(self: *Self, value: T, comptime ordering: Ordering) T { | |
| 133 | 133 | return self.rmw(.Max, value, ordering); |
| 134 | 134 | } |
| 135 | 135 | }); |
| 136 | 136 | |
| 137 | 137 | pub usingnamespace exportWhen(std.meta.trait.isIntegral(T), struct { |
| 138 | pub fn fetchAnd(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T { | |
| 138 | pub inline fn fetchAnd(self: *Self, value: T, comptime ordering: Ordering) T { | |
| 139 | 139 | return self.rmw(.And, value, ordering); |
| 140 | 140 | } |
| 141 | 141 | |
| 142 | pub fn fetchNand(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T { | |
| 142 | pub inline fn fetchNand(self: *Self, value: T, comptime ordering: Ordering) T { | |
| 143 | 143 | return self.rmw(.Nand, value, ordering); |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | pub fn fetchOr(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T { | |
| 146 | pub inline fn fetchOr(self: *Self, value: T, comptime ordering: Ordering) T { | |
| 147 | 147 | return self.rmw(.Or, value, ordering); |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | pub fn fetchXor(self: *Self, value: T, comptime ordering: Ordering) callconv(.Inline) T { | |
| 150 | pub inline fn fetchXor(self: *Self, value: T, comptime ordering: Ordering) T { | |
| 151 | 151 | return self.rmw(.Xor, value, ordering); |
| 152 | 152 | } |
| 153 | 153 | |
| ... | ... | @@ -158,24 +158,24 @@ pub fn Atomic(comptime T: type) type { |
| 158 | 158 | Toggle, |
| 159 | 159 | }; |
| 160 | 160 | |
| 161 | pub fn bitSet(self: *Self, bit: Bit, comptime ordering: Ordering) callconv(.Inline) u1 { | |
| 161 | pub inline fn bitSet(self: *Self, bit: Bit, comptime ordering: Ordering) u1 { | |
| 162 | 162 | return bitRmw(self, .Set, bit, ordering); |
| 163 | 163 | } |
| 164 | 164 | |
| 165 | pub fn bitReset(self: *Self, bit: Bit, comptime ordering: Ordering) callconv(.Inline) u1 { | |
| 165 | pub inline fn bitReset(self: *Self, bit: Bit, comptime ordering: Ordering) u1 { | |
| 166 | 166 | return bitRmw(self, .Reset, bit, ordering); |
| 167 | 167 | } |
| 168 | 168 | |
| 169 | pub fn bitToggle(self: *Self, bit: Bit, comptime ordering: Ordering) callconv(.Inline) u1 { | |
| 169 | pub inline fn bitToggle(self: *Self, bit: Bit, comptime ordering: Ordering) u1 { | |
| 170 | 170 | return bitRmw(self, .Toggle, bit, ordering); |
| 171 | 171 | } |
| 172 | 172 | |
| 173 | fn bitRmw( | |
| 173 | inline fn bitRmw( | |
| 174 | 174 | self: *Self, |
| 175 | 175 | comptime op: BitRmwOp, |
| 176 | 176 | bit: Bit, |
| 177 | 177 | comptime ordering: Ordering, |
| 178 | ) callconv(.Inline) u1 { | |
| 178 | ) u1 { | |
| 179 | 179 | // x86 supports dedicated bitwise instructions |
| 180 | 180 | if (comptime target.cpu.arch.isX86() and @sizeOf(T) >= 2 and @sizeOf(T) <= 8) { |
| 181 | 181 | const instruction = switch (op) { |
lib/std/c.zig+4-2| ... | ... | @@ -166,9 +166,10 @@ pub extern "c" fn sendto( |
| 166 | 166 | dest_addr: ?*const sockaddr, |
| 167 | 167 | addrlen: socklen_t, |
| 168 | 168 | ) isize; |
| 169 | pub extern "c" fn sendmsg(sockfd: fd_t, msg: *const std.x.os.Socket.Message, flags: c_int) isize; | |
| 169 | 170 | |
| 170 | pub extern fn recv(sockfd: fd_t, arg1: ?*c_void, arg2: usize, arg3: c_int) isize; | |
| 171 | pub extern fn recvfrom( | |
| 171 | pub extern "c" fn recv(sockfd: fd_t, arg1: ?*c_void, arg2: usize, arg3: c_int) isize; | |
| 172 | pub extern "c" fn recvfrom( | |
| 172 | 173 | sockfd: fd_t, |
| 173 | 174 | noalias buf: *c_void, |
| 174 | 175 | len: usize, |
| ... | ... | @@ -176,6 +177,7 @@ pub extern fn recvfrom( |
| 176 | 177 | noalias src_addr: ?*sockaddr, |
| 177 | 178 | noalias addrlen: ?*socklen_t, |
| 178 | 179 | ) isize; |
| 180 | pub extern "c" fn recvmsg(sockfd: fd_t, msg: *std.x.os.Socket.Message, flags: c_int) isize; | |
| 179 | 181 | |
| 180 | 182 | pub usingnamespace switch (builtin.os.tag) { |
| 181 | 183 | .netbsd => struct { |
lib/std/json.zig+1-4| ... | ... | @@ -2111,10 +2111,7 @@ test "parse into struct with duplicate field" { |
| 2111 | 2111 | const ballast = try testing.allocator.alloc(u64, 1); |
| 2112 | 2112 | defer testing.allocator.free(ballast); |
| 2113 | 2113 | |
| 2114 | const options_first = ParseOptions{ | |
| 2115 | .allocator = testing.allocator, | |
| 2116 | .duplicate_field_behavior = .UseFirst, | |
| 2117 | }; | |
| 2114 | const options_first = ParseOptions{ .allocator = testing.allocator, .duplicate_field_behavior = .UseFirst }; | |
| 2118 | 2115 | |
| 2119 | 2116 | const options_last = ParseOptions{ |
| 2120 | 2117 | .allocator = testing.allocator, |
lib/std/mem.zig+3-3| ... | ... | @@ -1171,7 +1171,7 @@ test "mem.indexOf" { |
| 1171 | 1171 | test "mem.indexOf multibyte" { |
| 1172 | 1172 | { |
| 1173 | 1173 | // make haystack and needle long enough to trigger boyer-moore-horspool algorithm |
| 1174 | const haystack = [1]u16{0} ** 100 ++ [_]u16 { 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee, 0x00ff }; | |
| 1174 | const haystack = [1]u16{0} ** 100 ++ [_]u16{ 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee, 0x00ff }; | |
| 1175 | 1175 | const needle = [_]u16{ 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee }; |
| 1176 | 1176 | try testing.expectEqual(indexOfPos(u16, &haystack, 0, &needle), 100); |
| 1177 | 1177 | |
| ... | ... | @@ -1184,7 +1184,7 @@ test "mem.indexOf multibyte" { |
| 1184 | 1184 | |
| 1185 | 1185 | { |
| 1186 | 1186 | // make haystack and needle long enough to trigger boyer-moore-horspool algorithm |
| 1187 | const haystack = [_]u16 { 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee, 0x00ff } ++ [1]u16{0} ** 100; | |
| 1187 | const haystack = [_]u16{ 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee, 0x00ff } ++ [1]u16{0} ** 100; | |
| 1188 | 1188 | const needle = [_]u16{ 0xbbaa, 0xccbb, 0xddcc, 0xeedd, 0xffee }; |
| 1189 | 1189 | try testing.expectEqual(lastIndexOf(u16, &haystack, &needle), 0); |
| 1190 | 1190 | |
| ... | ... | @@ -2201,7 +2201,7 @@ pub fn collapseRepeatsLen(comptime T: type, slice: []T, elem: T) usize { |
| 2201 | 2201 | |
| 2202 | 2202 | /// Collapse consecutive duplicate elements into one entry. |
| 2203 | 2203 | pub fn collapseRepeats(comptime T: type, slice: []T, elem: T) []T { |
| 2204 | return slice[0 .. collapseRepeatsLen(T, slice, elem)]; | |
| 2204 | return slice[0..collapseRepeatsLen(T, slice, elem)]; | |
| 2205 | 2205 | } |
| 2206 | 2206 | |
| 2207 | 2207 | fn testCollapseRepeats(str: []const u8, elem: u8, expected: []const u8) !void { |
lib/std/os.zig+1-1| ... | ... | @@ -4998,7 +4998,7 @@ pub fn sendmsg( |
| 4998 | 4998 | flags: u32, |
| 4999 | 4999 | ) SendMsgError!usize { |
| 5000 | 5000 | while (true) { |
| 5001 | const rc = system.sendmsg(sockfd, &msg, flags); | |
| 5001 | const rc = system.sendmsg(sockfd, @ptrCast(*const std.x.os.Socket.Message, &msg), @intCast(c_int, flags)); | |
| 5002 | 5002 | if (builtin.os.tag == .windows) { |
| 5003 | 5003 | if (rc == windows.ws2_32.SOCKET_ERROR) { |
| 5004 | 5004 | switch (windows.ws2_32.WSAGetLastError()) { |
lib/std/os/bits/darwin.zig+1-7| ... | ... | @@ -23,13 +23,7 @@ pub const sockaddr = extern struct { |
| 23 | 23 | family: sa_family_t, |
| 24 | 24 | data: [14]u8, |
| 25 | 25 | }; |
| 26 | pub const sockaddr_storage = extern struct { | |
| 27 | len: u8, | |
| 28 | family: sa_family_t, | |
| 29 | __pad1: [5]u8, | |
| 30 | __align: i64, | |
| 31 | __pad2: [112]u8, | |
| 32 | }; | |
| 26 | pub const sockaddr_storage = std.x.os.Socket.Address.Native.Storage; | |
| 33 | 27 | pub const sockaddr_in = extern struct { |
| 34 | 28 | len: u8 = @sizeOf(sockaddr_in), |
| 35 | 29 | family: sa_family_t = AF_INET, |
lib/std/os/bits/dragonfly.zig+7-13| ... | ... | @@ -396,6 +396,8 @@ pub const sockaddr = extern struct { |
| 396 | 396 | data: [14]u8, |
| 397 | 397 | }; |
| 398 | 398 | |
| 399 | pub const sockaddr_storage = std.x.os.Socket.Address.Native.Storage; | |
| 400 | ||
| 399 | 401 | pub const Kevent = extern struct { |
| 400 | 402 | ident: usize, |
| 401 | 403 | filter: c_short, |
| ... | ... | @@ -694,14 +696,6 @@ pub const in_port_t = u16; |
| 694 | 696 | pub const sa_family_t = u8; |
| 695 | 697 | pub const socklen_t = u32; |
| 696 | 698 | |
| 697 | pub const sockaddr_storage = extern struct { | |
| 698 | ss_len: u8, | |
| 699 | ss_family: sa_family_t, | |
| 700 | __ss_pad1: [5]u8, | |
| 701 | __ss_align: i64, | |
| 702 | __ss_pad2: [112]u8, | |
| 703 | }; | |
| 704 | ||
| 705 | 699 | pub const sockaddr_in = extern struct { |
| 706 | 700 | len: u8 = @sizeOf(sockaddr_in), |
| 707 | 701 | family: sa_family_t = AF_INET, |
| ... | ... | @@ -768,6 +762,11 @@ pub const dl_phdr_info = extern struct { |
| 768 | 762 | dlpi_phdr: [*]std.elf.Phdr, |
| 769 | 763 | dlpi_phnum: u16, |
| 770 | 764 | }; |
| 765 | pub const cmsghdr = extern struct { | |
| 766 | cmsg_len: socklen_t, | |
| 767 | cmsg_level: c_int, | |
| 768 | cmsg_type: c_int, | |
| 769 | }; | |
| 771 | 770 | pub const msghdr = extern struct { |
| 772 | 771 | msg_name: ?*c_void, |
| 773 | 772 | msg_namelen: socklen_t, |
| ... | ... | @@ -777,11 +776,6 @@ pub const msghdr = extern struct { |
| 777 | 776 | msg_controllen: socklen_t, |
| 778 | 777 | msg_flags: c_int, |
| 779 | 778 | }; |
| 780 | pub const cmsghdr = extern struct { | |
| 781 | cmsg_len: socklen_t, | |
| 782 | cmsg_level: c_int, | |
| 783 | cmsg_type: c_int, | |
| 784 | }; | |
| 785 | 779 | pub const cmsgcred = extern struct { |
| 786 | 780 | cmcred_pid: pid_t, |
| 787 | 781 | cmcred_uid: uid_t, |
lib/std/os/bits/freebsd.zig+1-7| ... | ... | @@ -206,13 +206,7 @@ pub const sockaddr = extern struct { |
| 206 | 206 | data: [14]u8, |
| 207 | 207 | }; |
| 208 | 208 | |
| 209 | pub const sockaddr_storage = extern struct { | |
| 210 | len: u8, | |
| 211 | family: sa_family_t, | |
| 212 | __pad1: [5]u8, | |
| 213 | __align: i64, | |
| 214 | __pad2: [112]u8, | |
| 215 | }; | |
| 209 | pub const sockaddr_storage = std.x.os.Socket.Address.Native.Storage; | |
| 216 | 210 | |
| 217 | 211 | pub const sockaddr_in = extern struct { |
| 218 | 212 | len: u8 = @sizeOf(sockaddr_in), |
lib/std/os/bits/haiku.zig+1-7| ... | ... | @@ -239,13 +239,7 @@ pub const sockaddr = extern struct { |
| 239 | 239 | data: [14]u8, |
| 240 | 240 | }; |
| 241 | 241 | |
| 242 | pub const sockaddr_storage = extern struct { | |
| 243 | len: u8, | |
| 244 | family: sa_family_t, | |
| 245 | __pad1: [5]u8, | |
| 246 | __align: i64, | |
| 247 | __pad2: [112]u8, | |
| 248 | }; | |
| 242 | pub const sockaddr_storage = std.x.os.Socket.Address.Native.Storage; | |
| 249 | 243 | |
| 250 | 244 | pub const sockaddr_in = extern struct { |
| 251 | 245 | len: u8 = @sizeOf(sockaddr_in), |
lib/std/os/bits/linux.zig+1-6| ... | ... | @@ -1149,12 +1149,7 @@ pub const sockaddr = extern struct { |
| 1149 | 1149 | data: [14]u8, |
| 1150 | 1150 | }; |
| 1151 | 1151 | |
| 1152 | pub const sockaddr_storage = extern struct { | |
| 1153 | family: sa_family_t, | |
| 1154 | __pad1: [6]u8, | |
| 1155 | __align: i64, | |
| 1156 | __pad2: [112]u8, | |
| 1157 | }; | |
| 1152 | pub const sockaddr_storage = std.x.os.Socket.Address.Native.Storage; | |
| 1158 | 1153 | |
| 1159 | 1154 | /// IPv4 socket address |
| 1160 | 1155 | pub const sockaddr_in = extern struct { |
lib/std/os/bits/netbsd.zig+1-7| ... | ... | @@ -226,13 +226,7 @@ pub const sockaddr = extern struct { |
| 226 | 226 | data: [14]u8, |
| 227 | 227 | }; |
| 228 | 228 | |
| 229 | pub const sockaddr_storage = extern struct { | |
| 230 | len: u8, | |
| 231 | family: sa_family_t, | |
| 232 | __pad1: [5]u8, | |
| 233 | __align: i64, | |
| 234 | __pad2: [112]u8, | |
| 235 | }; | |
| 229 | pub const sockaddr_storage = std.x.os.Socket.Address.Native.Storage; | |
| 236 | 230 | |
| 237 | 231 | pub const sockaddr_in = extern struct { |
| 238 | 232 | len: u8 = @sizeOf(sockaddr_in), |
lib/std/os/bits/openbsd.zig+1-7| ... | ... | @@ -246,13 +246,7 @@ pub const sockaddr = extern struct { |
| 246 | 246 | data: [14]u8, |
| 247 | 247 | }; |
| 248 | 248 | |
| 249 | pub const sockaddr_storage = extern struct { | |
| 250 | len: u8, | |
| 251 | family: sa_family_t, | |
| 252 | __pad1: [5]u8, | |
| 253 | __align: i64, | |
| 254 | __pad2: [112]u8, | |
| 255 | }; | |
| 249 | pub const sockaddr_storage = std.x.os.Socket.Address.Native.Storage; | |
| 256 | 250 | |
| 257 | 251 | pub const sockaddr_in = extern struct { |
| 258 | 252 | len: u8 = @sizeOf(sockaddr_in), |
lib/std/os/linux.zig+6-6| ... | ... | @@ -1000,11 +1000,11 @@ pub fn getsockopt(fd: i32, level: u32, optname: u32, noalias optval: [*]u8, noal |
| 1000 | 1000 | return syscall5(.getsockopt, @bitCast(usize, @as(isize, fd)), level, optname, @ptrToInt(optval), @ptrToInt(optlen)); |
| 1001 | 1001 | } |
| 1002 | 1002 | |
| 1003 | pub fn sendmsg(fd: i32, msg: *const msghdr_const, flags: u32) usize { | |
| 1003 | pub fn sendmsg(fd: i32, msg: *const std.x.os.Socket.Message, flags: c_int) usize { | |
| 1004 | 1004 | if (native_arch == .i386) { |
| 1005 | return socketcall(SC_sendmsg, &[3]usize{ @bitCast(usize, @as(isize, fd)), @ptrToInt(msg), flags }); | |
| 1005 | return socketcall(SC_sendmsg, &[3]usize{ @bitCast(usize, @as(isize, fd)), @ptrToInt(msg), @bitCast(usize, @as(isize, flags)) }); | |
| 1006 | 1006 | } |
| 1007 | return syscall3(.sendmsg, @bitCast(usize, @as(isize, fd)), @ptrToInt(msg), flags); | |
| 1007 | return syscall3(.sendmsg, @bitCast(usize, @as(isize, fd)), @ptrToInt(msg), @bitCast(usize, @as(isize, flags))); | |
| 1008 | 1008 | } |
| 1009 | 1009 | |
| 1010 | 1010 | pub fn sendmmsg(fd: i32, msgvec: [*]mmsghdr_const, vlen: u32, flags: u32) usize { |
| ... | ... | @@ -1054,11 +1054,11 @@ pub fn connect(fd: i32, addr: *const c_void, len: socklen_t) usize { |
| 1054 | 1054 | return syscall3(.connect, @bitCast(usize, @as(isize, fd)), @ptrToInt(addr), len); |
| 1055 | 1055 | } |
| 1056 | 1056 | |
| 1057 | pub fn recvmsg(fd: i32, msg: *msghdr, flags: u32) usize { | |
| 1057 | pub fn recvmsg(fd: i32, msg: *std.x.os.Socket.Message, flags: c_int) usize { | |
| 1058 | 1058 | if (native_arch == .i386) { |
| 1059 | return socketcall(SC_recvmsg, &[3]usize{ @bitCast(usize, @as(isize, fd)), @ptrToInt(msg), flags }); | |
| 1059 | return socketcall(SC_recvmsg, &[3]usize{ @bitCast(usize, @as(isize, fd)), @ptrToInt(msg), @bitCast(usize, @as(isize, flags)) }); | |
| 1060 | 1060 | } |
| 1061 | return syscall3(.recvmsg, @bitCast(usize, @as(isize, fd)), @ptrToInt(msg), flags); | |
| 1061 | return syscall3(.recvmsg, @bitCast(usize, @as(isize, fd)), @ptrToInt(msg), @bitCast(usize, @as(isize, flags))); | |
| 1062 | 1062 | } |
| 1063 | 1063 | |
| 1064 | 1064 | pub fn recvfrom(fd: i32, noalias buf: [*]u8, len: usize, flags: u32, noalias addr: ?*sockaddr, noalias alen: ?*socklen_t) usize { |
lib/std/os/windows.zig+1-1| ... | ... | @@ -1844,7 +1844,7 @@ pub fn sliceToPrefixedFileW(s: []const u8) !PathSpace { |
| 1844 | 1844 | } |
| 1845 | 1845 | |
| 1846 | 1846 | fn getFullPathNameW(path: [*:0]const u16, out: []u16) !usize { |
| 1847 | const result= kernel32.GetFullPathNameW(path, @intCast(u32, out.len), std.meta.assumeSentinel(out.ptr, 0), null); | |
| 1847 | const result = kernel32.GetFullPathNameW(path, @intCast(u32, out.len), std.meta.assumeSentinel(out.ptr, 0), null); | |
| 1848 | 1848 | if (result == 0) { |
| 1849 | 1849 | switch (kernel32.GetLastError()) { |
| 1850 | 1850 | else => |err| return unexpectedError(err), |
lib/std/os/windows/ws2_32.zig+6-10| ... | ... | @@ -3,6 +3,7 @@ |
| 3 | 3 | // This file is part of [zig](https://ziglang.org/), which is MIT licensed. |
| 4 | 4 | // The MIT license requires this copyright notice to be included in all copies |
| 5 | 5 | // and substantial portions of the software. |
| 6 | const std = @import("../../std.zig"); | |
| 6 | 7 | usingnamespace @import("bits.zig"); |
| 7 | 8 | |
| 8 | 9 | pub const SOCKET = *opaque {}; |
| ... | ... | @@ -1058,12 +1059,7 @@ pub const sockaddr = extern struct { |
| 1058 | 1059 | data: [14]u8, |
| 1059 | 1060 | }; |
| 1060 | 1061 | |
| 1061 | pub const sockaddr_storage = extern struct { | |
| 1062 | family: ADDRESS_FAMILY, | |
| 1063 | __pad1: [6]u8, | |
| 1064 | __align: i64, | |
| 1065 | __pad2: [112]u8, | |
| 1066 | }; | |
| 1062 | pub const sockaddr_storage = std.x.os.Socket.Address.Native.Storage; | |
| 1067 | 1063 | |
| 1068 | 1064 | /// IPv4 socket address |
| 1069 | 1065 | pub const sockaddr_in = extern struct { |
| ... | ... | @@ -1163,7 +1159,7 @@ pub const LPFN_GETACCEPTEXSOCKADDRS = fn ( |
| 1163 | 1159 | |
| 1164 | 1160 | pub const LPFN_WSASENDMSG = fn ( |
| 1165 | 1161 | s: SOCKET, |
| 1166 | lpMsg: *const WSAMSG_const, | |
| 1162 | lpMsg: *const std.x.os.Socket.Message, | |
| 1167 | 1163 | dwFlags: u32, |
| 1168 | 1164 | lpNumberOfBytesSent: ?*u32, |
| 1169 | 1165 | lpOverlapped: ?*OVERLAPPED, |
| ... | ... | @@ -1172,7 +1168,7 @@ pub const LPFN_WSASENDMSG = fn ( |
| 1172 | 1168 | |
| 1173 | 1169 | pub const LPFN_WSARECVMSG = fn ( |
| 1174 | 1170 | s: SOCKET, |
| 1175 | lpMsg: *WSAMSG, | |
| 1171 | lpMsg: *std.x.os.Socket.Message, | |
| 1176 | 1172 | lpdwNumberOfBytesRecv: ?*u32, |
| 1177 | 1173 | lpOverlapped: ?*OVERLAPPED, |
| 1178 | 1174 | lpCompletionRoutine: ?LPWSAOVERLAPPED_COMPLETION_ROUTINE, |
| ... | ... | @@ -2046,7 +2042,7 @@ pub extern "ws2_32" fn WSASend( |
| 2046 | 2042 | |
| 2047 | 2043 | pub extern "ws2_32" fn WSASendMsg( |
| 2048 | 2044 | s: SOCKET, |
| 2049 | lpMsg: *const WSAMSG_const, | |
| 2045 | lpMsg: *const std.x.os.Socket.Message, | |
| 2050 | 2046 | dwFlags: u32, |
| 2051 | 2047 | lpNumberOfBytesSent: ?*u32, |
| 2052 | 2048 | lpOverlapped: ?*OVERLAPPED, |
| ... | ... | @@ -2055,7 +2051,7 @@ pub extern "ws2_32" fn WSASendMsg( |
| 2055 | 2051 | |
| 2056 | 2052 | pub extern "ws2_32" fn WSARecvMsg( |
| 2057 | 2053 | s: SOCKET, |
| 2058 | lpMsg: *WSAMSG, | |
| 2054 | lpMsg: *std.x.os.Socket.Message, | |
| 2059 | 2055 | lpdwNumberOfBytesRecv: ?*u32, |
| 2060 | 2056 | lpOverlapped: ?*OVERLAPPED, |
| 2061 | 2057 | lpCompletionRoutine: ?LPWSAOVERLAPPED_COMPLETION_ROUTINE, |
lib/std/target.zig+1-2| ... | ... | @@ -500,8 +500,7 @@ pub const Target = struct { |
| 500 | 500 | .haiku, |
| 501 | 501 | .windows, |
| 502 | 502 | => return .gnu, |
| 503 | .uefi, | |
| 504 | => return .msvc, | |
| 503 | .uefi => return .msvc, | |
| 505 | 504 | .linux, |
| 506 | 505 | .wasi, |
| 507 | 506 | .emscripten, |
lib/std/x.zig+1| ... | ... | @@ -8,6 +8,7 @@ const std = @import("std.zig"); |
| 8 | 8 | |
| 9 | 9 | pub const os = struct { |
| 10 | 10 | pub const Socket = @import("x/os/socket.zig").Socket; |
| 11 | pub usingnamespace @import("x/os/io.zig"); | |
| 11 | 12 | pub usingnamespace @import("x/os/net.zig"); |
| 12 | 13 | }; |
| 13 | 14 |
lib/std/x/net/tcp.zig+65-23| ... | ... | @@ -12,12 +12,13 @@ const ip = std.x.net.ip; |
| 12 | 12 | |
| 13 | 13 | const fmt = std.fmt; |
| 14 | 14 | const mem = std.mem; |
| 15 | const builtin = std.builtin; | |
| 16 | 15 | const testing = std.testing; |
| 16 | const native_os = std.Target.current.os; | |
| 17 | 17 | |
| 18 | 18 | const IPv4 = std.x.os.IPv4; |
| 19 | 19 | const IPv6 = std.x.os.IPv6; |
| 20 | 20 | const Socket = std.x.os.Socket; |
| 21 | const Buffer = std.x.os.Buffer; | |
| 21 | 22 | |
| 22 | 23 | /// A generic TCP socket abstraction. |
| 23 | 24 | const tcp = @This(); |
| ... | ... | @@ -82,12 +83,13 @@ pub const Client = struct { |
| 82 | 83 | }; |
| 83 | 84 | |
| 84 | 85 | /// Opens a new client. |
| 85 | pub fn init(domain: tcp.Domain, flags: u32) !Client { | |
| 86 | pub fn init(domain: tcp.Domain, flags: std.enums.EnumFieldStruct(Socket.InitFlags, bool, false)) !Client { | |
| 86 | 87 | return Client{ |
| 87 | 88 | .socket = try Socket.init( |
| 88 | 89 | @enumToInt(domain), |
| 89 | os.SOCK_STREAM | flags, | |
| 90 | os.SOCK_STREAM, | |
| 90 | 91 | os.IPPROTO_TCP, |
| 92 | flags, | |
| 91 | 93 | ), |
| 92 | 94 | }; |
| 93 | 95 | } |
| ... | ... | @@ -143,15 +145,15 @@ pub const Client = struct { |
| 143 | 145 | /// Writes multiple I/O vectors with a prepended message header to the socket |
| 144 | 146 | /// with a set of flags specified. It returns the number of bytes that are |
| 145 | 147 | /// written to the socket. |
| 146 | pub fn writeVectorized(self: Client, msg: os.msghdr_const, flags: u32) !usize { | |
| 147 | return self.socket.writeVectorized(msg, flags); | |
| 148 | pub fn writeMessage(self: Client, msg: Socket.Message, flags: u32) !usize { | |
| 149 | return self.socket.writeMessage(msg, flags); | |
| 148 | 150 | } |
| 149 | 151 | |
| 150 | 152 | /// Read multiple I/O vectors with a prepended message header from the socket |
| 151 | 153 | /// with a set of flags specified. It returns the number of bytes that were |
| 152 | 154 | /// read into the buffer provided. |
| 153 | pub fn readVectorized(self: Client, msg: *os.msghdr, flags: u32) !usize { | |
| 154 | return self.socket.readVectorized(msg, flags); | |
| 155 | pub fn readMessage(self: Client, msg: *Socket.Message, flags: u32) !usize { | |
| 156 | return self.socket.readMessage(msg, flags); | |
| 155 | 157 | } |
| 156 | 158 | |
| 157 | 159 | /// Query and return the latest cached error on the client's underlying socket. |
| ... | ... | @@ -244,12 +246,13 @@ pub const Listener = struct { |
| 244 | 246 | socket: Socket, |
| 245 | 247 | |
| 246 | 248 | /// Opens a new listener. |
| 247 | pub fn init(domain: tcp.Domain, flags: u32) !Listener { | |
| 249 | pub fn init(domain: tcp.Domain, flags: std.enums.EnumFieldStruct(Socket.InitFlags, bool, false)) !Listener { | |
| 248 | 250 | return Listener{ |
| 249 | 251 | .socket = try Socket.init( |
| 250 | 252 | @enumToInt(domain), |
| 251 | os.SOCK_STREAM | flags, | |
| 253 | os.SOCK_STREAM, | |
| 252 | 254 | os.IPPROTO_TCP, |
| 255 | flags, | |
| 253 | 256 | ), |
| 254 | 257 | }; |
| 255 | 258 | } |
| ... | ... | @@ -278,7 +281,7 @@ pub const Listener = struct { |
| 278 | 281 | |
| 279 | 282 | /// Accept a pending incoming connection queued to the kernel backlog |
| 280 | 283 | /// of the listener's socket. |
| 281 | pub fn accept(self: Listener, flags: u32) !tcp.Connection { | |
| 284 | pub fn accept(self: Listener, flags: std.enums.EnumFieldStruct(Socket.InitFlags, bool, false)) !tcp.Connection { | |
| 282 | 285 | return tcp.Connection.from(try self.socket.accept(flags)); |
| 283 | 286 | } |
| 284 | 287 | |
| ... | ... | @@ -322,9 +325,9 @@ pub const Listener = struct { |
| 322 | 325 | }; |
| 323 | 326 | |
| 324 | 327 | test "tcp: create client/listener pair" { |
| 325 | if (builtin.os.tag == .wasi) return error.SkipZigTest; | |
| 328 | if (native_os.tag == .wasi) return error.SkipZigTest; | |
| 326 | 329 | |
| 327 | const listener = try tcp.Listener.init(.ip, os.SOCK_CLOEXEC); | |
| 330 | const listener = try tcp.Listener.init(.ip, .{ .close_on_exec = true }); | |
| 328 | 331 | defer listener.deinit(); |
| 329 | 332 | |
| 330 | 333 | try listener.bind(ip.Address.initIPv4(IPv4.unspecified, 0)); |
| ... | ... | @@ -336,19 +339,19 @@ test "tcp: create client/listener pair" { |
| 336 | 339 | .ipv6 => |*ipv6| ipv6.host = IPv6.localhost, |
| 337 | 340 | } |
| 338 | 341 | |
| 339 | const client = try tcp.Client.init(.ip, os.SOCK_CLOEXEC); | |
| 342 | const client = try tcp.Client.init(.ip, .{ .close_on_exec = true }); | |
| 340 | 343 | defer client.deinit(); |
| 341 | 344 | |
| 342 | 345 | try client.connect(binded_address); |
| 343 | 346 | |
| 344 | const conn = try listener.accept(os.SOCK_CLOEXEC); | |
| 347 | const conn = try listener.accept(.{ .close_on_exec = true }); | |
| 345 | 348 | defer conn.deinit(); |
| 346 | 349 | } |
| 347 | 350 | |
| 348 | test "tcp/client: set read timeout of 1 millisecond on blocking client" { | |
| 349 | if (builtin.os.tag == .wasi) return error.SkipZigTest; | |
| 351 | test "tcp/client: 1ms read timeout" { | |
| 352 | if (native_os.tag == .wasi) return error.SkipZigTest; | |
| 350 | 353 | |
| 351 | const listener = try tcp.Listener.init(.ip, os.SOCK_CLOEXEC); | |
| 354 | const listener = try tcp.Listener.init(.ip, .{ .close_on_exec = true }); | |
| 352 | 355 | defer listener.deinit(); |
| 353 | 356 | |
| 354 | 357 | try listener.bind(ip.Address.initIPv4(IPv4.unspecified, 0)); |
| ... | ... | @@ -360,23 +363,62 @@ test "tcp/client: set read timeout of 1 millisecond on blocking client" { |
| 360 | 363 | .ipv6 => |*ipv6| ipv6.host = IPv6.localhost, |
| 361 | 364 | } |
| 362 | 365 | |
| 363 | const client = try tcp.Client.init(.ip, os.SOCK_CLOEXEC); | |
| 366 | const client = try tcp.Client.init(.ip, .{ .close_on_exec = true }); | |
| 364 | 367 | defer client.deinit(); |
| 365 | 368 | |
| 366 | 369 | try client.connect(binded_address); |
| 367 | 370 | try client.setReadTimeout(1); |
| 368 | 371 | |
| 369 | const conn = try listener.accept(os.SOCK_CLOEXEC); | |
| 372 | const conn = try listener.accept(.{ .close_on_exec = true }); | |
| 370 | 373 | defer conn.deinit(); |
| 371 | 374 | |
| 372 | 375 | var buf: [1]u8 = undefined; |
| 373 | 376 | try testing.expectError(error.WouldBlock, client.reader(0).read(&buf)); |
| 374 | 377 | } |
| 375 | 378 | |
| 379 | test "tcp/client: read and write multiple vectors" { | |
| 380 | if (native_os.tag == .wasi) return error.SkipZigTest; | |
| 381 | ||
| 382 | const listener = try tcp.Listener.init(.ip, .{ .close_on_exec = true }); | |
| 383 | defer listener.deinit(); | |
| 384 | ||
| 385 | try listener.bind(ip.Address.initIPv4(IPv4.unspecified, 0)); | |
| 386 | try listener.listen(128); | |
| 387 | ||
| 388 | var binded_address = try listener.getLocalAddress(); | |
| 389 | switch (binded_address) { | |
| 390 | .ipv4 => |*ipv4| ipv4.host = IPv4.localhost, | |
| 391 | .ipv6 => |*ipv6| ipv6.host = IPv6.localhost, | |
| 392 | } | |
| 393 | ||
| 394 | const client = try tcp.Client.init(.ip, .{ .close_on_exec = true }); | |
| 395 | defer client.deinit(); | |
| 396 | ||
| 397 | try client.connect(binded_address); | |
| 398 | ||
| 399 | const conn = try listener.accept(.{ .close_on_exec = true }); | |
| 400 | defer conn.deinit(); | |
| 401 | ||
| 402 | const message = "hello world"; | |
| 403 | _ = try conn.client.writeMessage(Socket.Message.fromBuffers(&[_]Buffer{ | |
| 404 | Buffer.from(message[0 .. message.len / 2]), | |
| 405 | Buffer.from(message[message.len / 2 ..]), | |
| 406 | }), 0); | |
| 407 | ||
| 408 | var buf: [message.len + 1]u8 = undefined; | |
| 409 | var msg = Socket.Message.fromBuffers(&[_]Buffer{ | |
| 410 | Buffer.from(buf[0 .. message.len / 2]), | |
| 411 | Buffer.from(buf[message.len / 2 ..]), | |
| 412 | }); | |
| 413 | _ = try client.readMessage(&msg, 0); | |
| 414 | ||
| 415 | try testing.expectEqualStrings(message, buf[0..message.len]); | |
| 416 | } | |
| 417 | ||
| 376 | 418 | test "tcp/listener: bind to unspecified ipv4 address" { |
| 377 | if (builtin.os.tag == .wasi) return error.SkipZigTest; | |
| 419 | if (native_os.tag == .wasi) return error.SkipZigTest; | |
| 378 | 420 | |
| 379 | const listener = try tcp.Listener.init(.ip, os.SOCK_CLOEXEC); | |
| 421 | const listener = try tcp.Listener.init(.ip, .{ .close_on_exec = true }); | |
| 380 | 422 | defer listener.deinit(); |
| 381 | 423 | |
| 382 | 424 | try listener.bind(ip.Address.initIPv4(IPv4.unspecified, 0)); |
| ... | ... | @@ -387,9 +429,9 @@ test "tcp/listener: bind to unspecified ipv4 address" { |
| 387 | 429 | } |
| 388 | 430 | |
| 389 | 431 | test "tcp/listener: bind to unspecified ipv6 address" { |
| 390 | if (builtin.os.tag == .wasi) return error.SkipZigTest; | |
| 432 | if (native_os.tag == .wasi) return error.SkipZigTest; | |
| 391 | 433 | |
| 392 | const listener = try tcp.Listener.init(.ipv6, os.SOCK_CLOEXEC); | |
| 434 | const listener = try tcp.Listener.init(.ipv6, .{ .close_on_exec = true }); | |
| 393 | 435 | defer listener.deinit(); |
| 394 | 436 | |
| 395 | 437 | try listener.bind(ip.Address.initIPv6(IPv6.unspecified, 0)); |
lib/std/x/os/io.zig created+205| ... | ... | @@ -0,0 +1,205 @@ |
| 1 | const std = @import("../../std.zig"); | |
| 2 | ||
| 3 | const os = std.os; | |
| 4 | const mem = std.mem; | |
| 5 | const testing = std.testing; | |
| 6 | const native_os = std.Target.current.os; | |
| 7 | ||
| 8 | /// POSIX `iovec`, or Windows `WSABUF`. The difference between the two are the ordering | |
| 9 | /// of fields, alongside the length being represented as either a ULONG or a size_t. | |
| 10 | pub const Buffer = if (native_os.tag == .windows) | |
| 11 | extern struct { | |
| 12 | len: c_ulong, | |
| 13 | ptr: usize, | |
| 14 | ||
| 15 | pub fn from(slice: []const u8) Buffer { | |
| 16 | return .{ .len = @intCast(c_ulong, slice.len), .ptr = @ptrToInt(slice.ptr) }; | |
| 17 | } | |
| 18 | ||
| 19 | pub fn into(self: Buffer) []const u8 { | |
| 20 | return @intToPtr([*]const u8, self.ptr)[0..self.len]; | |
| 21 | } | |
| 22 | ||
| 23 | pub fn intoMutable(self: Buffer) []u8 { | |
| 24 | return @intToPtr([*]u8, self.ptr)[0..self.len]; | |
| 25 | } | |
| 26 | } | |
| 27 | else | |
| 28 | extern struct { | |
| 29 | ptr: usize, | |
| 30 | len: usize, | |
| 31 | ||
| 32 | pub fn from(slice: []const u8) Buffer { | |
| 33 | return .{ .ptr = @ptrToInt(slice.ptr), .len = slice.len }; | |
| 34 | } | |
| 35 | ||
| 36 | pub fn into(self: Buffer) []const u8 { | |
| 37 | return @intToPtr([*]const u8, self.ptr)[0..self.len]; | |
| 38 | } | |
| 39 | ||
| 40 | pub fn intoMutable(self: Buffer) []u8 { | |
| 41 | return @intToPtr([*]u8, self.ptr)[0..self.len]; | |
| 42 | } | |
| 43 | }; | |
| 44 | ||
| 45 | pub const Reactor = struct { | |
| 46 | pub const InitFlags = enum { | |
| 47 | close_on_exec, | |
| 48 | }; | |
| 49 | ||
| 50 | pub const Event = struct { | |
| 51 | data: usize, | |
| 52 | is_error: bool, | |
| 53 | is_hup: bool, | |
| 54 | is_readable: bool, | |
| 55 | is_writable: bool, | |
| 56 | }; | |
| 57 | ||
| 58 | pub const Interest = struct { | |
| 59 | hup: bool = false, | |
| 60 | oneshot: bool = false, | |
| 61 | readable: bool = false, | |
| 62 | writable: bool = false, | |
| 63 | }; | |
| 64 | ||
| 65 | fd: os.fd_t, | |
| 66 | ||
| 67 | pub fn init(flags: std.enums.EnumFieldStruct(Reactor.InitFlags, bool, false)) !Reactor { | |
| 68 | var raw_flags: u32 = 0; | |
| 69 | const set = std.EnumSet(Reactor.InitFlags).init(flags); | |
| 70 | if (set.contains(.close_on_exec)) raw_flags |= os.EPOLL_CLOEXEC; | |
| 71 | return Reactor{ .fd = try os.epoll_create1(raw_flags) }; | |
| 72 | } | |
| 73 | ||
| 74 | pub fn deinit(self: Reactor) void { | |
| 75 | os.close(self.fd); | |
| 76 | } | |
| 77 | ||
| 78 | pub fn update(self: Reactor, fd: os.fd_t, identifier: usize, interest: Reactor.Interest) !void { | |
| 79 | var flags: u32 = 0; | |
| 80 | flags |= if (interest.oneshot) os.EPOLLONESHOT else os.EPOLLET; | |
| 81 | if (interest.hup) flags |= os.EPOLLRDHUP; | |
| 82 | if (interest.readable) flags |= os.EPOLLIN; | |
| 83 | if (interest.writable) flags |= os.EPOLLOUT; | |
| 84 | ||
| 85 | const event = &os.epoll_event{ | |
| 86 | .events = flags, | |
| 87 | .data = .{ .ptr = identifier }, | |
| 88 | }; | |
| 89 | ||
| 90 | os.epoll_ctl(self.fd, os.EPOLL_CTL_MOD, fd, event) catch |err| switch (err) { | |
| 91 | error.FileDescriptorNotRegistered => try os.epoll_ctl(self.fd, os.EPOLL_CTL_ADD, fd, event), | |
| 92 | else => return err, | |
| 93 | }; | |
| 94 | } | |
| 95 | ||
| 96 | pub fn poll(self: Reactor, comptime max_num_events: comptime_int, closure: anytype, timeout_milliseconds: ?u64) !void { | |
| 97 | var events: [max_num_events]os.epoll_event = undefined; | |
| 98 | ||
| 99 | const num_events = os.epoll_wait(self.fd, &events, if (timeout_milliseconds) |ms| @intCast(i32, ms) else -1); | |
| 100 | for (events[0..num_events]) |ev| { | |
| 101 | const is_error = ev.events & os.EPOLLERR != 0; | |
| 102 | const is_hup = ev.events & (os.EPOLLHUP | os.EPOLLRDHUP) != 0; | |
| 103 | const is_readable = ev.events & os.EPOLLIN != 0; | |
| 104 | const is_writable = ev.events & os.EPOLLOUT != 0; | |
| 105 | ||
| 106 | try closure.call(Reactor.Event{ | |
| 107 | .data = ev.data.ptr, | |
| 108 | .is_error = is_error, | |
| 109 | .is_hup = is_hup, | |
| 110 | .is_readable = is_readable, | |
| 111 | .is_writable = is_writable, | |
| 112 | }); | |
| 113 | } | |
| 114 | } | |
| 115 | }; | |
| 116 | ||
| 117 | test "reactor/linux: drive async tcp client/listener pair" { | |
| 118 | if (native_os.tag != .linux) return error.SkipZigTest; | |
| 119 | ||
| 120 | const ip = std.x.net.ip; | |
| 121 | const tcp = std.x.net.tcp; | |
| 122 | ||
| 123 | const IPv4 = std.x.os.IPv4; | |
| 124 | const IPv6 = std.x.os.IPv6; | |
| 125 | const Socket = std.x.os.Socket; | |
| 126 | ||
| 127 | const reactor = try Reactor.init(.{ .close_on_exec = true }); | |
| 128 | defer reactor.deinit(); | |
| 129 | ||
| 130 | const listener = try tcp.Listener.init(.ip, .{ | |
| 131 | .close_on_exec = true, | |
| 132 | .nonblocking = true, | |
| 133 | }); | |
| 134 | defer listener.deinit(); | |
| 135 | ||
| 136 | try reactor.update(listener.socket.fd, 0, .{ .readable = true }); | |
| 137 | try reactor.poll(1, struct { | |
| 138 | fn call(event: Reactor.Event) !void { | |
| 139 | try testing.expectEqual(Reactor.Event{ | |
| 140 | .data = 0, | |
| 141 | .is_error = false, | |
| 142 | .is_hup = true, | |
| 143 | .is_readable = false, | |
| 144 | .is_writable = false, | |
| 145 | }, event); | |
| 146 | } | |
| 147 | }, null); | |
| 148 | ||
| 149 | try listener.bind(ip.Address.initIPv4(IPv4.unspecified, 0)); | |
| 150 | try listener.listen(128); | |
| 151 | ||
| 152 | var binded_address = try listener.getLocalAddress(); | |
| 153 | switch (binded_address) { | |
| 154 | .ipv4 => |*ipv4| ipv4.host = IPv4.localhost, | |
| 155 | .ipv6 => |*ipv6| ipv6.host = IPv6.localhost, | |
| 156 | } | |
| 157 | ||
| 158 | const client = try tcp.Client.init(.ip, .{ | |
| 159 | .close_on_exec = true, | |
| 160 | .nonblocking = true, | |
| 161 | }); | |
| 162 | defer client.deinit(); | |
| 163 | ||
| 164 | try reactor.update(client.socket.fd, 1, .{ .readable = true, .writable = true }); | |
| 165 | try reactor.poll(1, struct { | |
| 166 | fn call(event: Reactor.Event) !void { | |
| 167 | try testing.expectEqual(Reactor.Event{ | |
| 168 | .data = 1, | |
| 169 | .is_error = false, | |
| 170 | .is_hup = true, | |
| 171 | .is_readable = false, | |
| 172 | .is_writable = true, | |
| 173 | }, event); | |
| 174 | } | |
| 175 | }, null); | |
| 176 | ||
| 177 | client.connect(binded_address) catch |err| switch (err) { | |
| 178 | error.WouldBlock => {}, | |
| 179 | else => return err, | |
| 180 | }; | |
| 181 | ||
| 182 | try reactor.poll(1, struct { | |
| 183 | fn call(event: Reactor.Event) !void { | |
| 184 | try testing.expectEqual(Reactor.Event{ | |
| 185 | .data = 1, | |
| 186 | .is_error = false, | |
| 187 | .is_hup = false, | |
| 188 | .is_readable = false, | |
| 189 | .is_writable = true, | |
| 190 | }, event); | |
| 191 | } | |
| 192 | }, null); | |
| 193 | ||
| 194 | try reactor.poll(1, struct { | |
| 195 | fn call(event: Reactor.Event) !void { | |
| 196 | try testing.expectEqual(Reactor.Event{ | |
| 197 | .data = 0, | |
| 198 | .is_error = false, | |
| 199 | .is_hup = false, | |
| 200 | .is_readable = true, | |
| 201 | .is_writable = false, | |
| 202 | }, event); | |
| 203 | } | |
| 204 | }, null); | |
| 205 | } |
lib/std/x/os/net.zig+3-3| ... | ... | @@ -10,17 +10,17 @@ const os = std.os; |
| 10 | 10 | const fmt = std.fmt; |
| 11 | 11 | const mem = std.mem; |
| 12 | 12 | const math = std.math; |
| 13 | const builtin = std.builtin; | |
| 14 | 13 | const testing = std.testing; |
| 14 | const native_os = std.Target.current.os; | |
| 15 | 15 | |
| 16 | 16 | /// Resolves a network interface name into a scope/zone ID. It returns |
| 17 | 17 | /// an error if either resolution fails, or if the interface name is |
| 18 | 18 | /// too long. |
| 19 | 19 | pub fn resolveScopeID(name: []const u8) !u32 { |
| 20 | if (comptime @hasDecl(os, "IFNAMESIZE")) { | |
| 20 | if (@hasDecl(os, "IFNAMESIZE")) { | |
| 21 | 21 | if (name.len >= os.IFNAMESIZE - 1) return error.NameTooLong; |
| 22 | 22 | |
| 23 | if (comptime builtin.os.tag == .windows) { | |
| 23 | if (native_os.tag == .windows) { | |
| 24 | 24 | var interface_name: [os.IFNAMESIZE]u8 = undefined; |
| 25 | 25 | mem.copy(u8, &interface_name, name); |
| 26 | 26 | interface_name[name.len] = 0; |
lib/std/x/os/socket.zig+202-2| ... | ... | @@ -11,7 +11,13 @@ const os = std.os; |
| 11 | 11 | const fmt = std.fmt; |
| 12 | 12 | const mem = std.mem; |
| 13 | 13 | const time = std.time; |
| 14 | const builtin = std.builtin; | |
| 14 | const meta = std.meta; | |
| 15 | const native_os = std.Target.current.os; | |
| 16 | const native_endian = std.Target.current.cpu.arch.endian(); | |
| 17 | ||
| 18 | const Buffer = std.x.os.Buffer; | |
| 19 | ||
| 20 | const assert = std.debug.assert; | |
| 15 | 21 | |
| 16 | 22 | /// A generic, cross-platform socket abstraction. |
| 17 | 23 | pub const Socket = struct { |
| ... | ... | @@ -29,6 +35,32 @@ pub const Socket = struct { |
| 29 | 35 | /// A generic socket address abstraction. It is safe to directly access and modify |
| 30 | 36 | /// the fields of a `Socket.Address`. |
| 31 | 37 | pub const Address = union(enum) { |
| 38 | pub const Native = struct { | |
| 39 | pub const requires_prepended_length = native_os.getVersionRange() == .semver; | |
| 40 | pub const Length = if (requires_prepended_length) u8 else [0]u8; | |
| 41 | ||
| 42 | pub const Family = if (requires_prepended_length) u8 else c_ushort; | |
| 43 | ||
| 44 | /// POSIX `sockaddr_storage`. The expected size and alignment is specified in IETF RFC 2553. | |
| 45 | pub const Storage = extern struct { | |
| 46 | pub const expected_size = 128; | |
| 47 | pub const expected_alignment = 8; | |
| 48 | ||
| 49 | pub const padding_size = expected_size - | |
| 50 | mem.alignForward(@sizeOf(Address.Native.Length), expected_alignment) - | |
| 51 | mem.alignForward(@sizeOf(Address.Native.Family), expected_alignment); | |
| 52 | ||
| 53 | len: Address.Native.Length align(expected_alignment) = undefined, | |
| 54 | family: Address.Native.Family align(expected_alignment) = undefined, | |
| 55 | padding: [padding_size]u8 align(expected_alignment) = undefined, | |
| 56 | ||
| 57 | comptime { | |
| 58 | assert(@sizeOf(Storage) == Storage.expected_size); | |
| 59 | assert(@alignOf(Storage) == Storage.expected_alignment); | |
| 60 | } | |
| 61 | }; | |
| 62 | }; | |
| 63 | ||
| 32 | 64 | ipv4: net.IPv4.Address, |
| 33 | 65 | ipv6: net.IPv6.Address, |
| 34 | 66 | |
| ... | ... | @@ -107,6 +139,174 @@ pub const Socket = struct { |
| 107 | 139 | } |
| 108 | 140 | }; |
| 109 | 141 | |
| 142 | /// POSIX `msghdr`. Denotes a destination address, set of buffers, control data, and flags. Ported | |
| 143 | /// directly from musl. | |
| 144 | pub const Message = if (native_os.isAtLeast(.windows, .vista) != null and native_os.isAtLeast(.windows, .vista).?) | |
| 145 | extern struct { | |
| 146 | name: usize = @ptrToInt(@as(?[*]u8, null)), | |
| 147 | name_len: c_int = 0, | |
| 148 | ||
| 149 | buffers: usize = undefined, | |
| 150 | buffers_len: c_ulong = undefined, | |
| 151 | ||
| 152 | control: Buffer = .{ | |
| 153 | .ptr = @ptrToInt(@as(?[*]u8, null)), | |
| 154 | .len = 0, | |
| 155 | }, | |
| 156 | flags: c_ulong = 0, | |
| 157 | ||
| 158 | pub usingnamespace MessageMixin(Message); | |
| 159 | } | |
| 160 | else if (native_os.tag == .windows) | |
| 161 | extern struct { | |
| 162 | name: usize = @ptrToInt(@as(?[*]u8, null)), | |
| 163 | name_len: c_int = 0, | |
| 164 | ||
| 165 | buffers: usize = undefined, | |
| 166 | buffers_len: u32 = undefined, | |
| 167 | ||
| 168 | control: Buffer = .{ | |
| 169 | .ptr = @ptrToInt(@as(?[*]u8, null)), | |
| 170 | .len = 0, | |
| 171 | }, | |
| 172 | flags: u32 = 0, | |
| 173 | ||
| 174 | pub usingnamespace MessageMixin(Message); | |
| 175 | } | |
| 176 | else if (@sizeOf(usize) > 4 and native_endian == .Big) | |
| 177 | extern struct { | |
| 178 | name: usize = @ptrToInt(@as(?[*]u8, null)), | |
| 179 | name_len: c_uint = 0, | |
| 180 | ||
| 181 | buffers: usize = undefined, | |
| 182 | _pad_1: c_int = 0, | |
| 183 | buffers_len: c_int = undefined, | |
| 184 | ||
| 185 | control: usize = @ptrToInt(@as(?[*]u8, null)), | |
| 186 | _pad_2: c_int = 0, | |
| 187 | control_len: c_uint = 0, | |
| 188 | ||
| 189 | flags: c_int = 0, | |
| 190 | ||
| 191 | pub usingnamespace MessageMixin(Message); | |
| 192 | } | |
| 193 | else if (@sizeOf(usize) > 4 and native_endian == .Little) | |
| 194 | extern struct { | |
| 195 | name: usize = @ptrToInt(@as(?[*]u8, null)), | |
| 196 | name_len: c_uint = 0, | |
| 197 | ||
| 198 | buffers: usize = undefined, | |
| 199 | buffers_len: c_int = undefined, | |
| 200 | _pad_1: c_int = 0, | |
| 201 | ||
| 202 | control: usize = @ptrToInt(@as(?[*]u8, null)), | |
| 203 | control_len: c_uint = 0, | |
| 204 | _pad_2: c_int = 0, | |
| 205 | ||
| 206 | flags: c_int = 0, | |
| 207 | ||
| 208 | pub usingnamespace MessageMixin(Message); | |
| 209 | } | |
| 210 | else | |
| 211 | extern struct { | |
| 212 | name: usize = @ptrToInt(@as(?[*]u8, null)), | |
| 213 | name_len: c_uint = 0, | |
| 214 | ||
| 215 | buffers: usize = undefined, | |
| 216 | buffers_len: c_int = undefined, | |
| 217 | ||
| 218 | control: usize = @ptrToInt(@as(?[*]u8, null)), | |
| 219 | control_len: c_uint = 0, | |
| 220 | ||
| 221 | flags: c_int = 0, | |
| 222 | ||
| 223 | pub usingnamespace MessageMixin(Message); | |
| 224 | }; | |
| 225 | ||
| 226 | fn MessageMixin(comptime Self: type) type { | |
| 227 | return struct { | |
| 228 | pub fn fromBuffers(buffers: []const Buffer) Self { | |
| 229 | var self: Self = .{}; | |
| 230 | self.setBuffers(buffers); | |
| 231 | return self; | |
| 232 | } | |
| 233 | ||
| 234 | pub fn setName(self: *Self, name: []const u8) void { | |
| 235 | self.name = @ptrToInt(name.ptr); | |
| 236 | self.name_len = @intCast(meta.fieldInfo(Self, .name_len).field_type, name.len); | |
| 237 | } | |
| 238 | ||
| 239 | pub fn setBuffers(self: *Self, buffers: []const Buffer) void { | |
| 240 | self.buffers = @ptrToInt(buffers.ptr); | |
| 241 | self.buffers_len = @intCast(meta.fieldInfo(Self, .buffers_len).field_type, buffers.len); | |
| 242 | } | |
| 243 | ||
| 244 | pub fn setControl(self: *Self, control: []const u8) void { | |
| 245 | if (native_os.tag == .windows) { | |
| 246 | self.control = Buffer.from(control); | |
| 247 | } else { | |
| 248 | self.control = @ptrToInt(control.ptr); | |
| 249 | self.control_len = @intCast(meta.fieldInfo(Self, .control_len).field_type, control.len); | |
| 250 | } | |
| 251 | } | |
| 252 | ||
| 253 | pub fn setFlags(self: *Self, flags: u32) void { | |
| 254 | self.flags = @intCast(meta.fieldInfo(Self, .flags).field_type, flags); | |
| 255 | } | |
| 256 | ||
| 257 | pub fn getName(self: Self) []const u8 { | |
| 258 | return @intToPtr([*]const u8, self.name)[0..@intCast(usize, self.name_len)]; | |
| 259 | } | |
| 260 | ||
| 261 | pub fn getBuffers(self: Self) []const Buffer { | |
| 262 | return @intToPtr([*]const Buffer, self.buffers)[0..@intCast(usize, self.buffers_len)]; | |
| 263 | } | |
| 264 | ||
| 265 | pub fn getControl(self: Self) []const u8 { | |
| 266 | if (native_os.tag == .windows) { | |
| 267 | return self.control.into(); | |
| 268 | } else { | |
| 269 | return @intToPtr([*]const u8, self.control)[0..@intCast(usize, self.control_len)]; | |
| 270 | } | |
| 271 | } | |
| 272 | ||
| 273 | pub fn getFlags(self: Self) u32 { | |
| 274 | return @intCast(u32, self.flags); | |
| 275 | } | |
| 276 | }; | |
| 277 | } | |
| 278 | ||
| 279 | /// POSIX `linger`, denoting the linger settings of a socket. | |
| 280 | /// | |
| 281 | /// Microsoft's documentation and glibc denote the fields to be unsigned | |
| 282 | /// short's on Windows, whereas glibc and musl denote the fields to be | |
| 283 | /// int's on every other platform. | |
| 284 | pub const Linger = extern struct { | |
| 285 | pub const Field = switch (native_os.tag) { | |
| 286 | .windows => c_ushort, | |
| 287 | else => c_int, | |
| 288 | }; | |
| 289 | ||
| 290 | enabled: Field, | |
| 291 | timeout_seconds: Field, | |
| 292 | ||
| 293 | pub fn init(timeout_seconds: ?u16) Socket.Linger { | |
| 294 | return .{ | |
| 295 | .enabled = @intCast(Socket.Linger.Field, @boolToInt(timeout_seconds != null)), | |
| 296 | .timeout_seconds = if (timeout_seconds) |seconds| @intCast(Socket.Linger.Field, seconds) else 0, | |
| 297 | }; | |
| 298 | } | |
| 299 | }; | |
| 300 | ||
| 301 | /// Possible set of flags to initialize a socket with. | |
| 302 | pub const InitFlags = enum { | |
| 303 | // Initialize a socket to be non-blocking. | |
| 304 | nonblocking, | |
| 305 | ||
| 306 | // Have a socket close itself on exec syscalls. | |
| 307 | close_on_exec, | |
| 308 | }; | |
| 309 | ||
| 110 | 310 | /// The underlying handle of a socket. |
| 111 | 311 | fd: os.socket_t, |
| 112 | 312 | |
| ... | ... | @@ -116,7 +316,7 @@ pub const Socket = struct { |
| 116 | 316 | } |
| 117 | 317 | |
| 118 | 318 | /// Mix in socket syscalls depending on the platform we are compiling against. |
| 119 | pub usingnamespace switch (builtin.os.tag) { | |
| 319 | pub usingnamespace switch (native_os.tag) { | |
| 120 | 320 | .windows => @import("socket_windows.zig"), |
| 121 | 321 | else => @import("socket_posix.zig"), |
| 122 | 322 | }.Mixin(Socket); |
lib/std/x/os/socket_posix.zig+70-40| ... | ... | @@ -13,8 +13,12 @@ const time = std.time; |
| 13 | 13 | pub fn Mixin(comptime Socket: type) type { |
| 14 | 14 | return struct { |
| 15 | 15 | /// Open a new socket. |
| 16 | pub fn init(domain: u32, socket_type: u32, protocol: u32) !Socket { | |
| 17 | return Socket{ .fd = try os.socket(domain, socket_type, protocol) }; | |
| 16 | pub fn init(domain: u32, socket_type: u32, protocol: u32, flags: std.enums.EnumFieldStruct(Socket.InitFlags, bool, false)) !Socket { | |
| 17 | var raw_flags: u32 = socket_type; | |
| 18 | const set = std.EnumSet(Socket.InitFlags).init(flags); | |
| 19 | if (set.contains(.close_on_exec)) raw_flags |= os.SOCK_CLOEXEC; | |
| 20 | if (set.contains(.nonblocking)) raw_flags |= os.SOCK_NONBLOCK; | |
| 21 | return Socket{ .fd = try os.socket(domain, raw_flags, protocol) }; | |
| 18 | 22 | } |
| 19 | 23 | |
| 20 | 24 | /// Closes the socket. |
| ... | ... | @@ -44,11 +48,16 @@ pub fn Mixin(comptime Socket: type) type { |
| 44 | 48 | |
| 45 | 49 | /// Accept a pending incoming connection queued to the kernel backlog |
| 46 | 50 | /// of the socket. |
| 47 | pub fn accept(self: Socket, flags: u32) !Socket.Connection { | |
| 48 | var address: os.sockaddr_storage = undefined; | |
| 49 | var address_len: u32 = @sizeOf(os.sockaddr_storage); | |
| 51 | pub fn accept(self: Socket, flags: std.enums.EnumFieldStruct(Socket.InitFlags, bool, false)) !Socket.Connection { | |
| 52 | var address: Socket.Address.Native.Storage = undefined; | |
| 53 | var address_len: u32 = @sizeOf(Socket.Address.Native.Storage); | |
| 50 | 54 | |
| 51 | const socket = Socket{ .fd = try os.accept(self.fd, @ptrCast(*os.sockaddr, &address), &address_len, flags) }; | |
| 55 | var raw_flags: u32 = 0; | |
| 56 | const set = std.EnumSet(Socket.InitFlags).init(flags); | |
| 57 | if (set.contains(.close_on_exec)) raw_flags |= os.SOCK_CLOEXEC; | |
| 58 | if (set.contains(.nonblocking)) raw_flags |= os.SOCK_NONBLOCK; | |
| 59 | ||
| 60 | const socket = Socket{ .fd = try os.accept(self.fd, @ptrCast(*os.sockaddr, &address), &address_len, raw_flags) }; | |
| 52 | 61 | const socket_address = Socket.Address.fromNative(@ptrCast(*os.sockaddr, &address)); |
| 53 | 62 | |
| 54 | 63 | return Socket.Connection.from(socket, socket_address); |
| ... | ... | @@ -69,48 +78,76 @@ pub fn Mixin(comptime Socket: type) type { |
| 69 | 78 | /// Writes multiple I/O vectors with a prepended message header to the socket |
| 70 | 79 | /// with a set of flags specified. It returns the number of bytes that are |
| 71 | 80 | /// written to the socket. |
| 72 | pub fn writeVectorized(self: Socket, msg: os.msghdr_const, flags: u32) !usize { | |
| 73 | return os.sendmsg(self.fd, msg, flags); | |
| 81 | pub fn writeMessage(self: Socket, msg: Socket.Message, flags: u32) !usize { | |
| 82 | while (true) { | |
| 83 | const rc = os.system.sendmsg(self.fd, &msg, @intCast(c_int, flags)); | |
| 84 | return switch (os.errno(rc)) { | |
| 85 | 0 => return @intCast(usize, rc), | |
| 86 | os.EACCES => error.AccessDenied, | |
| 87 | os.EAGAIN => error.WouldBlock, | |
| 88 | os.EALREADY => error.FastOpenAlreadyInProgress, | |
| 89 | os.EBADF => unreachable, // always a race condition | |
| 90 | os.ECONNRESET => error.ConnectionResetByPeer, | |
| 91 | os.EDESTADDRREQ => unreachable, // The socket is not connection-mode, and no peer address is set. | |
| 92 | os.EFAULT => unreachable, // An invalid user space address was specified for an argument. | |
| 93 | os.EINTR => continue, | |
| 94 | os.EINVAL => unreachable, // Invalid argument passed. | |
| 95 | os.EISCONN => unreachable, // connection-mode socket was connected already but a recipient was specified | |
| 96 | os.EMSGSIZE => error.MessageTooBig, | |
| 97 | os.ENOBUFS => error.SystemResources, | |
| 98 | os.ENOMEM => error.SystemResources, | |
| 99 | os.ENOTSOCK => unreachable, // The file descriptor sockfd does not refer to a socket. | |
| 100 | os.EOPNOTSUPP => unreachable, // Some bit in the flags argument is inappropriate for the socket type. | |
| 101 | os.EPIPE => error.BrokenPipe, | |
| 102 | os.EAFNOSUPPORT => error.AddressFamilyNotSupported, | |
| 103 | os.ELOOP => error.SymLinkLoop, | |
| 104 | os.ENAMETOOLONG => error.NameTooLong, | |
| 105 | os.ENOENT => error.FileNotFound, | |
| 106 | os.ENOTDIR => error.NotDir, | |
| 107 | os.EHOSTUNREACH => error.NetworkUnreachable, | |
| 108 | os.ENETUNREACH => error.NetworkUnreachable, | |
| 109 | os.ENOTCONN => error.SocketNotConnected, | |
| 110 | os.ENETDOWN => error.NetworkSubsystemFailed, | |
| 111 | else => |err| os.unexpectedErrno(err), | |
| 112 | }; | |
| 113 | } | |
| 74 | 114 | } |
| 75 | 115 | |
| 76 | 116 | /// Read multiple I/O vectors with a prepended message header from the socket |
| 77 | 117 | /// with a set of flags specified. It returns the number of bytes that were |
| 78 | 118 | /// read into the buffer provided. |
| 79 | pub fn readVectorized(self: Socket, msg: *os.msghdr, flags: u32) !usize { | |
| 80 | if (comptime @hasDecl(os.system, "recvmsg")) { | |
| 81 | while (true) { | |
| 82 | const rc = os.system.recvmsg(self.fd, msg, flags); | |
| 83 | return switch (os.errno(rc)) { | |
| 84 | 0 => @intCast(usize, rc), | |
| 85 | os.EBADF => unreachable, // always a race condition | |
| 86 | os.EFAULT => unreachable, | |
| 87 | os.EINVAL => unreachable, | |
| 88 | os.ENOTCONN => unreachable, | |
| 89 | os.ENOTSOCK => unreachable, | |
| 90 | os.EINTR => continue, | |
| 91 | os.EAGAIN => error.WouldBlock, | |
| 92 | os.ENOMEM => error.SystemResources, | |
| 93 | os.ECONNREFUSED => error.ConnectionRefused, | |
| 94 | os.ECONNRESET => error.ConnectionResetByPeer, | |
| 95 | else => |err| os.unexpectedErrno(err), | |
| 96 | }; | |
| 97 | } | |
| 119 | pub fn readMessage(self: Socket, msg: *Socket.Message, flags: u32) !usize { | |
| 120 | while (true) { | |
| 121 | const rc = os.system.recvmsg(self.fd, msg, @intCast(c_int, flags)); | |
| 122 | return switch (os.errno(rc)) { | |
| 123 | 0 => @intCast(usize, rc), | |
| 124 | os.EBADF => unreachable, // always a race condition | |
| 125 | os.EFAULT => unreachable, | |
| 126 | os.EINVAL => unreachable, | |
| 127 | os.ENOTCONN => unreachable, | |
| 128 | os.ENOTSOCK => unreachable, | |
| 129 | os.EINTR => continue, | |
| 130 | os.EAGAIN => error.WouldBlock, | |
| 131 | os.ENOMEM => error.SystemResources, | |
| 132 | os.ECONNREFUSED => error.ConnectionRefused, | |
| 133 | os.ECONNRESET => error.ConnectionResetByPeer, | |
| 134 | else => |err| os.unexpectedErrno(err), | |
| 135 | }; | |
| 98 | 136 | } |
| 99 | return error.NotSupported; | |
| 100 | 137 | } |
| 101 | 138 | |
| 102 | 139 | /// Query the address that the socket is locally bounded to. |
| 103 | 140 | pub fn getLocalAddress(self: Socket) !Socket.Address { |
| 104 | var address: os.sockaddr_storage = undefined; | |
| 105 | var address_len: u32 = @sizeOf(os.sockaddr_storage); | |
| 141 | var address: Socket.Address.Native.Storage = undefined; | |
| 142 | var address_len: u32 = @sizeOf(Socket.Address.Native.Storage); | |
| 106 | 143 | try os.getsockname(self.fd, @ptrCast(*os.sockaddr, &address), &address_len); |
| 107 | 144 | return Socket.Address.fromNative(@ptrCast(*os.sockaddr, &address)); |
| 108 | 145 | } |
| 109 | 146 | |
| 110 | 147 | /// Query the address that the socket is connected to. |
| 111 | 148 | pub fn getRemoteAddress(self: Socket) !Socket.Address { |
| 112 | var address: os.sockaddr_storage = undefined; | |
| 113 | var address_len: u32 = @sizeOf(os.sockaddr_storage); | |
| 149 | var address: Socket.Address.Native.Storage = undefined; | |
| 150 | var address_len: u32 = @sizeOf(Socket.Address.Native.Storage); | |
| 114 | 151 | try os.getpeername(self.fd, @ptrCast(*os.sockaddr, &address), &address_len); |
| 115 | 152 | return Socket.Address.fromNative(@ptrCast(*os.sockaddr, &address)); |
| 116 | 153 | } |
| ... | ... | @@ -165,14 +202,7 @@ pub fn Mixin(comptime Socket: type) type { |
| 165 | 202 | /// seconds. |
| 166 | 203 | pub fn setLinger(self: Socket, timeout_seconds: ?u16) !void { |
| 167 | 204 | if (comptime @hasDecl(os, "SO_LINGER")) { |
| 168 | const settings = extern struct { | |
| 169 | l_onoff: c_int, | |
| 170 | l_linger: c_int, | |
| 171 | }{ | |
| 172 | .l_onoff = @intCast(c_int, @boolToInt(timeout_seconds != null)), | |
| 173 | .l_linger = if (timeout_seconds) |seconds| @intCast(c_int, seconds) else 0, | |
| 174 | }; | |
| 175 | ||
| 205 | const settings = Socket.Linger.init(timeout_seconds); | |
| 176 | 206 | return self.setOption(os.SOL_SOCKET, os.SO_LINGER, mem.asBytes(&settings)); |
| 177 | 207 | } |
| 178 | 208 |
lib/std/x/os/socket_windows.zig+39-27| ... | ... | @@ -16,27 +16,24 @@ const ws2_32 = windows.ws2_32; |
| 16 | 16 | pub fn Mixin(comptime Socket: type) type { |
| 17 | 17 | return struct { |
| 18 | 18 | /// Open a new socket. |
| 19 | pub fn init(domain: u32, socket_type: u32, protocol: u32) !Socket { | |
| 20 | var filtered_socket_type = socket_type & ~@as(u32, os.SOCK_CLOEXEC); | |
| 21 | ||
| 22 | var filtered_flags: u32 = ws2_32.WSA_FLAG_OVERLAPPED; | |
| 23 | if (socket_type & os.SOCK_CLOEXEC != 0) { | |
| 24 | filtered_flags |= ws2_32.WSA_FLAG_NO_HANDLE_INHERIT; | |
| 25 | } | |
| 19 | pub fn init(domain: u32, socket_type: u32, protocol: u32, flags: std.enums.EnumFieldStruct(Socket.InitFlags, bool, false)) !Socket { | |
| 20 | var raw_flags: u32 = 0; | |
| 21 | const set = std.EnumSet(Socket.InitFlags).init(flags); | |
| 22 | if (set.contains(.close_on_exec)) raw_flags |= ws2_32.WSA_FLAG_NO_HANDLE_INHERIT; | |
| 26 | 23 | |
| 27 | 24 | const fd = ws2_32.WSASocketW( |
| 28 | 25 | @intCast(i32, domain), |
| 29 | @intCast(i32, filtered_socket_type), | |
| 26 | @intCast(i32, socket_type), | |
| 30 | 27 | @intCast(i32, protocol), |
| 31 | 28 | null, |
| 32 | 29 | 0, |
| 33 | filtered_flags, | |
| 30 | raw_flags, | |
| 34 | 31 | ); |
| 35 | 32 | if (fd == ws2_32.INVALID_SOCKET) { |
| 36 | 33 | return switch (ws2_32.WSAGetLastError()) { |
| 37 | 34 | .WSANOTINITIALISED => { |
| 38 | 35 | _ = try windows.WSAStartup(2, 2); |
| 39 | return Socket.init(domain, socket_type, protocol); | |
| 36 | return Socket.init(domain, socket_type, protocol, flags); | |
| 40 | 37 | }, |
| 41 | 38 | .WSAEAFNOSUPPORT => error.AddressFamilyNotSupported, |
| 42 | 39 | .WSAEMFILE => error.ProcessFdQuotaExceeded, |
| ... | ... | @@ -46,6 +43,14 @@ pub fn Mixin(comptime Socket: type) type { |
| 46 | 43 | }; |
| 47 | 44 | } |
| 48 | 45 | |
| 46 | if (set.contains(.nonblocking)) { | |
| 47 | var enabled: c_ulong = 1; | |
| 48 | const rc = ws2_32.ioctlsocket(fd, ws2_32.FIONBIO, &enabled); | |
| 49 | if (rc == ws2_32.SOCKET_ERROR) { | |
| 50 | return windows.unexpectedWSAError(ws2_32.WSAGetLastError()); | |
| 51 | } | |
| 52 | } | |
| 53 | ||
| 49 | 54 | return Socket{ .fd = fd }; |
| 50 | 55 | } |
| 51 | 56 | |
| ... | ... | @@ -138,12 +143,12 @@ pub fn Mixin(comptime Socket: type) type { |
| 138 | 143 | |
| 139 | 144 | /// Accept a pending incoming connection queued to the kernel backlog |
| 140 | 145 | /// of the socket. |
| 141 | pub fn accept(self: Socket, flags: u32) !Socket.Connection { | |
| 142 | var address: ws2_32.sockaddr_storage = undefined; | |
| 143 | var address_len: c_int = @sizeOf(ws2_32.sockaddr_storage); | |
| 146 | pub fn accept(self: Socket, flags: std.enums.EnumFieldStruct(Socket.InitFlags, bool, false)) !Socket.Connection { | |
| 147 | var address: Socket.Address.Native.Storage = undefined; | |
| 148 | var address_len: c_int = @sizeOf(Socket.Address.Native.Storage); | |
| 144 | 149 | |
| 145 | const rc = ws2_32.accept(self.fd, @ptrCast(*ws2_32.sockaddr, &address), &address_len); | |
| 146 | if (rc == ws2_32.INVALID_SOCKET) { | |
| 150 | const fd = ws2_32.accept(self.fd, @ptrCast(*ws2_32.sockaddr, &address), &address_len); | |
| 151 | if (fd == ws2_32.INVALID_SOCKET) { | |
| 147 | 152 | return switch (ws2_32.WSAGetLastError()) { |
| 148 | 153 | .WSANOTINITIALISED => unreachable, |
| 149 | 154 | .WSAECONNRESET => error.ConnectionResetByPeer, |
| ... | ... | @@ -158,9 +163,20 @@ pub fn Mixin(comptime Socket: type) type { |
| 158 | 163 | }; |
| 159 | 164 | } |
| 160 | 165 | |
| 161 | const socket = Socket.from(rc); | |
| 166 | const socket = Socket.from(fd); | |
| 167 | errdefer socket.deinit(); | |
| 168 | ||
| 162 | 169 | const socket_address = Socket.Address.fromNative(@ptrCast(*ws2_32.sockaddr, &address)); |
| 163 | 170 | |
| 171 | const set = std.EnumSet(Socket.InitFlags).init(flags); | |
| 172 | if (set.contains(.nonblocking)) { | |
| 173 | var enabled: c_ulong = 1; | |
| 174 | const rc = ws2_32.ioctlsocket(fd, ws2_32.FIONBIO, &enabled); | |
| 175 | if (rc == ws2_32.SOCKET_ERROR) { | |
| 176 | return windows.unexpectedWSAError(ws2_32.WSAGetLastError()); | |
| 177 | } | |
| 178 | } | |
| 179 | ||
| 164 | 180 | return Socket.Connection.from(socket, socket_address); |
| 165 | 181 | } |
| 166 | 182 | |
| ... | ... | @@ -238,7 +254,7 @@ pub fn Mixin(comptime Socket: type) type { |
| 238 | 254 | /// Writes multiple I/O vectors with a prepended message header to the socket |
| 239 | 255 | /// with a set of flags specified. It returns the number of bytes that are |
| 240 | 256 | /// written to the socket. |
| 241 | pub fn writeVectorized(self: Socket, msg: ws2_32.msghdr_const, flags: u32) !usize { | |
| 257 | pub fn writeMessage(self: Socket, msg: Socket.Message, flags: u32) !usize { | |
| 242 | 258 | const call = try windows.loadWinsockExtensionFunction(ws2_32.LPFN_WSASENDMSG, self.fd, ws2_32.WSAID_WSASENDMSG); |
| 243 | 259 | |
| 244 | 260 | var num_bytes: u32 = undefined; |
| ... | ... | @@ -275,7 +291,7 @@ pub fn Mixin(comptime Socket: type) type { |
| 275 | 291 | /// Read multiple I/O vectors with a prepended message header from the socket |
| 276 | 292 | /// with a set of flags specified. It returns the number of bytes that were |
| 277 | 293 | /// read into the buffer provided. |
| 278 | pub fn readVectorized(self: Socket, msg: *ws2_32.msghdr, flags: u32) !usize { | |
| 294 | pub fn readMessage(self: Socket, msg: *Socket.Message, flags: u32) !usize { | |
| 279 | 295 | const call = try windows.loadWinsockExtensionFunction(ws2_32.LPFN_WSARECVMSG, self.fd, ws2_32.WSAID_WSARECVMSG); |
| 280 | 296 | |
| 281 | 297 | var num_bytes: u32 = undefined; |
| ... | ... | @@ -311,8 +327,8 @@ pub fn Mixin(comptime Socket: type) type { |
| 311 | 327 | |
| 312 | 328 | /// Query the address that the socket is locally bounded to. |
| 313 | 329 | pub fn getLocalAddress(self: Socket) !Socket.Address { |
| 314 | var address: ws2_32.sockaddr_storage = undefined; | |
| 315 | var address_len: c_int = @sizeOf(ws2_32.sockaddr_storage); | |
| 330 | var address: Socket.Address.Native.Storage = undefined; | |
| 331 | var address_len: c_int = @sizeOf(Socket.Address.Native.Storage); | |
| 316 | 332 | |
| 317 | 333 | const rc = ws2_32.getsockname(self.fd, @ptrCast(*ws2_32.sockaddr, &address), &address_len); |
| 318 | 334 | if (rc == ws2_32.SOCKET_ERROR) { |
| ... | ... | @@ -331,8 +347,8 @@ pub fn Mixin(comptime Socket: type) type { |
| 331 | 347 | |
| 332 | 348 | /// Query the address that the socket is connected to. |
| 333 | 349 | pub fn getRemoteAddress(self: Socket) !Socket.Address { |
| 334 | var address: ws2_32.sockaddr_storage = undefined; | |
| 335 | var address_len: c_int = @sizeOf(ws2_32.sockaddr_storage); | |
| 350 | var address: Socket.Address.Native.Storage = undefined; | |
| 351 | var address_len: c_int = @sizeOf(Socket.Address.Native.Storage); | |
| 336 | 352 | |
| 337 | 353 | const rc = ws2_32.getpeername(self.fd, @ptrCast(*ws2_32.sockaddr, &address), &address_len); |
| 338 | 354 | if (rc == ws2_32.SOCKET_ERROR) { |
| ... | ... | @@ -384,11 +400,7 @@ pub fn Mixin(comptime Socket: type) type { |
| 384 | 400 | /// if the host does not support the option for a socket to linger around up until a timeout specified in |
| 385 | 401 | /// seconds. |
| 386 | 402 | pub fn setLinger(self: Socket, timeout_seconds: ?u16) !void { |
| 387 | const settings = ws2_32.linger{ | |
| 388 | .l_onoff = @as(u16, @boolToInt(timeout_seconds != null)), | |
| 389 | .l_linger = if (timeout_seconds) |seconds| seconds else 0, | |
| 390 | }; | |
| 391 | ||
| 403 | const settings = Socket.Linger.init(timeout_seconds); | |
| 392 | 404 | return self.setOption(ws2_32.SOL_SOCKET, ws2_32.SO_LINGER, mem.asBytes(&settings)); |
| 393 | 405 | } |
| 394 | 406 |