| ... | ... | @@ -183,11 +183,8 @@ pub fn poll( |
| 183 | 183 | .count = 0, |
| 184 | 184 | }; |
| 185 | 185 | result.poll_fds[i] = .{ |
| 186 | | .fd = @field(files, enum_fields[i].name).file.handle, |
| 187 | | .events = switch (@field(files, enum_fields[i].name).direction) { |
| 188 | | .in => os.POLL.IN, |
| 189 | | .out => os.POLL.OUT, |
| 190 | | }, |
| 186 | .fd = @field(files, enum_fields[i].name).handle, |
| 187 | .events = os.POLL.IN, |
| 191 | 188 | .revents = undefined, |
| 192 | 189 | }; |
| 193 | 190 | } |
| ... | ... | @@ -200,12 +197,15 @@ pub fn Poller(comptime StreamEnum: type) type { |
| 200 | 197 | const Fifo = std.fifo.LinearFifo(u8, .Dynamic); |
| 201 | 198 | |
| 202 | 199 | fifos: [enum_fields.len]Fifo, |
| 203 | | //directions: [enum_fields.len]PollFile.Direction, |
| 204 | | //handles: [enum_fields.len]std.fs.File.Handle, |
| 205 | 200 | poll_fds: [enum_fields.len]std.os.pollfd, |
| 206 | 201 | |
| 207 | 202 | const Self = @This(); |
| 208 | 203 | |
| 204 | pub fn deinit(self: *Self) void { |
| 205 | inline for (&self.fifos) |*q| q.deinit(); |
| 206 | self.* = undefined; |
| 207 | } |
| 208 | |
| 209 | 209 | pub fn poll(self: *Self) !void { |
| 210 | 210 | if (builtin.os.tag == .windows) { |
| 211 | 211 | return pollWindows(self); |
| ... | ... | @@ -241,31 +241,22 @@ pub fn Poller(comptime StreamEnum: type) type { |
| 241 | 241 | const events_len = try os.poll(&self.poll_fds, std.math.maxInt(i32)); |
| 242 | 242 | if (events_len == 0) return; |
| 243 | 243 | |
| 244 | | inline for (0..enum_fields.len) |i| { |
| 244 | inline for (&self.poll_fds, &self.fifos) |*poll_fd, *q| { |
| 245 | 245 | // Try reading whatever is available before checking the error |
| 246 | 246 | // conditions. |
| 247 | 247 | // It's still possible to read after a POLL.HUP is received, |
| 248 | 248 | // always check if there's some data waiting to be read first. |
| 249 | | if (self.poll_fds[i].revents & os.POLL.IN != 0) { |
| 250 | | const q = &self.fifos[i]; |
| 249 | if (poll_fd.revents & os.POLL.IN != 0) { |
| 251 | 250 | const buf = try q.writableWithSize(bump_amt); |
| 252 | | const amt = try os.read(self.poll_fds[i].fd, buf); |
| 251 | const amt = try os.read(poll_fd.fd, buf); |
| 253 | 252 | q.update(amt); |
| 254 | | std.debug.print("read {d} bytes\n", .{amt}); |
| 255 | 253 | if (amt == 0) { |
| 256 | 254 | // Remove the fd when the EOF condition is met. |
| 257 | | self.poll_fds[i].fd = -1; |
| 255 | poll_fd.fd = -1; |
| 258 | 256 | } |
| 259 | | } else if (self.poll_fds[i].revents & err_mask != 0) { |
| 257 | } else if (poll_fd.revents & err_mask != 0) { |
| 260 | 258 | // Exclude the fds that signaled an error. |
| 261 | | self.poll_fds[i].fd = -1; |
| 262 | | } else if (self.poll_fds[i].revents & os.POLL.OUT != 0) { |
| 263 | | const q = &self.fifos[i]; |
| 264 | | const amt = try os.write(self.poll_fds[i].fd, q.readableSlice(0)); |
| 265 | | q.discard(amt); |
| 266 | | if (amt == 0) { |
| 267 | | self.poll_fds[i].fd = -1; |
| 268 | | } |
| 259 | poll_fd.fd = -1; |
| 269 | 260 | } |
| 270 | 261 | } |
| 271 | 262 | } |
| ... | ... | @@ -280,10 +271,10 @@ pub fn PollFiles(comptime StreamEnum: type) type { |
| 280 | 271 | for (&struct_fields, enum_fields) |*struct_field, enum_field| { |
| 281 | 272 | struct_field.* = .{ |
| 282 | 273 | .name = enum_field.name, |
| 283 | | .type = PollFile, |
| 274 | .type = fs.File, |
| 284 | 275 | .default_value = null, |
| 285 | 276 | .is_comptime = false, |
| 286 | | .alignment = @alignOf(PollFile), |
| 277 | .alignment = @alignOf(fs.File), |
| 287 | 278 | }; |
| 288 | 279 | } |
| 289 | 280 | return @Type(.{ .Struct = .{ |
| ... | ... | @@ -294,13 +285,6 @@ pub fn PollFiles(comptime StreamEnum: type) type { |
| 294 | 285 | } }); |
| 295 | 286 | } |
| 296 | 287 | |
| 297 | | pub const PollFile = struct { |
| 298 | | file: File, |
| 299 | | direction: Direction, |
| 300 | | |
| 301 | | pub const Direction = enum { in, out }; |
| 302 | | }; |
| 303 | | |
| 304 | 288 | test { |
| 305 | 289 | _ = @import("io/bit_reader.zig"); |
| 306 | 290 | _ = @import("io/bit_writer.zig"); |