| ... | @@ -1621,6 +1621,9 @@ pub const Stream = struct { | ... | @@ -1621,6 +1621,9 @@ pub const Stream = struct { |
| 1621 | } | 1621 | } |
| 1622 | } | 1622 | } |
| 1623 | | 1623 | |
| | 1624 | /// TODO in evented I/O mode, this implementation incorrectly uses the event loop's |
| | 1625 | /// file system thread instead of non-blocking. It needs to be reworked to properly |
| | 1626 | /// use non-blocking I/O. |
| 1624 | pub fn write(self: Stream, buffer: []const u8) WriteError!usize { | 1627 | pub fn write(self: Stream, buffer: []const u8) WriteError!usize { |
| 1625 | if (std.Target.current.os.tag == .windows) { | 1628 | if (std.Target.current.os.tag == .windows) { |
| 1626 | return os.windows.WriteFile(self.handle, buffer, null, io.default_mode); | 1629 | return os.windows.WriteFile(self.handle, buffer, null, io.default_mode); |
| ... | @@ -1632,6 +1635,40 @@ pub const Stream = struct { | ... | @@ -1632,6 +1635,40 @@ pub const Stream = struct { |
| 1632 | return os.write(self.handle, buffer); | 1635 | return os.write(self.handle, buffer); |
| 1633 | } | 1636 | } |
| 1634 | } | 1637 | } |
| | 1638 | |
| | 1639 | /// See https://github.com/ziglang/zig/issues/7699 |
| | 1640 | /// See equivalent function: `std.fs.File.writev`. |
| | 1641 | pub fn writev(self: Stream, iovecs: []const os.iovec_const) WriteError!usize { |
| | 1642 | if (std.io.is_async) { |
| | 1643 | // TODO improve to actually take advantage of writev syscall, if available. |
| | 1644 | if (iovecs.len == 0) return 0; |
| | 1645 | const first_buffer = iovecs[0].iov_base[0..iovecs[0].iov_len]; |
| | 1646 | try self.write(first_buffer); |
| | 1647 | return first_buffer.len; |
| | 1648 | } else { |
| | 1649 | return os.writev(self.handle, iovecs); |
| | 1650 | } |
| | 1651 | } |
| | 1652 | |
| | 1653 | /// The `iovecs` parameter is mutable because this function needs to mutate the fields in |
| | 1654 | /// order to handle partial writes from the underlying OS layer. |
| | 1655 | /// See https://github.com/ziglang/zig/issues/7699 |
| | 1656 | /// See equivalent function: `std.fs.File.writevAll`. |
| | 1657 | pub fn writevAll(self: Stream, iovecs: []os.iovec_const) WriteError!void { |
| | 1658 | if (iovecs.len == 0) return; |
| | 1659 | |
| | 1660 | var i: usize = 0; |
| | 1661 | while (true) { |
| | 1662 | var amt = try self.writev(iovecs[i..]); |
| | 1663 | while (amt >= iovecs[i].iov_len) { |
| | 1664 | amt -= iovecs[i].iov_len; |
| | 1665 | i += 1; |
| | 1666 | if (i >= iovecs.len) return; |
| | 1667 | } |
| | 1668 | iovecs[i].iov_base += amt; |
| | 1669 | iovecs[i].iov_len -= amt; |
| | 1670 | } |
| | 1671 | } |
| 1635 | }; | 1672 | }; |
| 1636 | | 1673 | |
| 1637 | pub const StreamServer = struct { | 1674 | pub const StreamServer = struct { |