Fix writev on Windows.

This commit is contained in:
levlam 2021-04-04 00:40:37 +03:00
parent 84194768ae
commit 6285d98479
3 changed files with 16 additions and 3 deletions

View File

@ -36,6 +36,8 @@
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#else
#include <limits>
#endif
#if TD_PORT_WINDOWS && defined(WIN32_LEAN_AND_MEAN)
@ -271,9 +273,16 @@ Result<size_t> FileFd::writev(Span<IoSlice> slices) {
return OS_ERROR(PSLICE() << "Writev to " << get_native_fd() << " has failed");
#else
size_t res = 0;
for (auto slice : slices) {
for (const auto &slice : slices) {
if (slice.size() > std::numeric_limits<size_t>::max() - res) {
break;
}
TRY_RESULT(size, write(slice));
res += size;
if (size != slice.size()) {
CHECK(size < slice.size());
break;
}
}
return res;
#endif

View File

@ -25,7 +25,7 @@ inline IoSlice as_io_slice(Slice slice) {
return res;
}
inline Slice as_slice(const IoSlice io_slice) {
inline Slice as_slice(const IoSlice &io_slice) {
return Slice(static_cast<const char *>(io_slice.iov_base), io_slice.iov_len);
}

View File

@ -30,6 +30,8 @@
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#else
#include <limits>
#endif
#include <atomic>
@ -98,7 +100,9 @@ class SocketFdImpl : private Iocp::Callback {
Result<size_t> writev(Span<IoSlice> slices) {
size_t total_size = 0;
for (auto io_slice : slices) {
total_size += as_slice(io_slice).size();
auto size = as_slice(io_slice).size();
CHECK(size <= std::numeric_limits<size_t>::max() - total_size);
total_size += size;
}
auto left_size = total_size;