|
29 | 29 | #include <cstring> |
30 | 30 | #include <fstream> |
31 | 31 | #include <filesystem> |
| 32 | +#include <algorithm> |
32 | 33 |
|
33 | 34 | namespace fs = std::filesystem; |
34 | 35 |
|
| 36 | +static constexpr size_t MAX_CHUNK_SIZE = 1024ull * 1024ull * 1024ull; // 1 GiB |
| 37 | + |
35 | 38 | #ifdef _WIN32 |
36 | 39 | typedef SOCKET sockfd_t; |
37 | 40 | using ssize_t = __int64; |
@@ -323,23 +326,33 @@ static std::shared_ptr<socket_t> create_server_socket(const char * host, int por |
323 | 326 | static bool send_data(sockfd_t sockfd, const void * data, size_t size) { |
324 | 327 | size_t bytes_sent = 0; |
325 | 328 | while (bytes_sent < size) { |
326 | | - ssize_t n = send(sockfd, (const char *)data + bytes_sent, size - bytes_sent, 0); |
| 329 | + size_t size_to_send = std::min(size - bytes_sent, MAX_CHUNK_SIZE); |
| 330 | + ssize_t n = send(sockfd, (const char *)data + bytes_sent, size_to_send, 0); |
327 | 331 | if (n < 0) { |
| 332 | + GGML_LOG_ERROR("send failed (bytes_sent=%zu, size_to_send=%zu)\n", |
| 333 | + bytes_sent, size_to_send); |
328 | 334 | return false; |
329 | 335 | } |
330 | | - bytes_sent += n; |
| 336 | + bytes_sent += (size_t)n; |
331 | 337 | } |
332 | 338 | return true; |
333 | 339 | } |
334 | 340 |
|
335 | 341 | static bool recv_data(sockfd_t sockfd, void * data, size_t size) { |
336 | 342 | size_t bytes_recv = 0; |
337 | 343 | while (bytes_recv < size) { |
338 | | - ssize_t n = recv(sockfd, (char *)data + bytes_recv, size - bytes_recv, 0); |
339 | | - if (n <= 0) { |
| 344 | + size_t size_to_recv = std::min(size - bytes_recv, MAX_CHUNK_SIZE); |
| 345 | + ssize_t n = recv(sockfd, (char *)data + bytes_recv, size_to_recv, 0); |
| 346 | + if (n < 0) { |
| 347 | + GGML_LOG_ERROR("recv failed (bytes_recv=%zu, size_to_recv=%zu)\n", |
| 348 | + bytes_recv, size_to_recv); |
| 349 | + return false; |
| 350 | + } |
| 351 | + if (n == 0) { |
| 352 | + GGML_LOG_ERROR("recv returned 0 (peer closed?)\n"); |
340 | 353 | return false; |
341 | 354 | } |
342 | | - bytes_recv += n; |
| 355 | + bytes_recv += (size_t)n; |
343 | 356 | } |
344 | 357 | return true; |
345 | 358 | } |
|
0 commit comments