Skip to content

Commit 514a5ff

Browse files
committed
ggml-rpc: rename RPC_IO_CHUNK->MAX_CHUNK_SIZE, use std::min() for cap, switch to GGML_LOG_ERROR, handle 0-length send/recv
1 parent 0e7aa4e commit 514a5ff

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

ggml/src/ggml-rpc/ggml-rpc.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929
#include <cstring>
3030
#include <fstream>
3131
#include <filesystem>
32+
#include <algorithm>
3233

3334
namespace fs = std::filesystem;
3435

35-
static constexpr size_t RPC_IO_CHUNK = 1024ull * 1024ull * 1024ull; // 1 GiB
36+
static constexpr size_t MAX_CHUNK_SIZE = 1024ull * 1024ull * 1024ull; // 1 GiB
3637

3738
#ifdef _WIN32
3839
typedef SOCKET sockfd_t;
@@ -325,43 +326,45 @@ static std::shared_ptr<socket_t> create_server_socket(const char * host, int por
325326
static bool send_data(sockfd_t sockfd, const void * data, size_t size) {
326327
size_t bytes_sent = 0;
327328
while (bytes_sent < size) {
328-
size_t size_to_send = size - bytes_sent;
329-
if (size_to_send > RPC_IO_CHUNK) size_to_send = RPC_IO_CHUNK;
329+
size_t size_to_send = std::min(size - bytes_sent, MAX_CHUNK_SIZE);
330330
ssize_t n = send(sockfd, (const char *)data + bytes_sent, size_to_send, 0);
331331
if (n < 0) {
332-
#ifndef _WIN32
333-
perror("send");
334-
#else
335-
fprintf(stderr, "send failed (bytes_sent=%zu, size_to_send=%zu)\n", bytes_sent, size_to_send);
336-
#endif
332+
GGML_LOG_ERROR("send failed (bytes_sent=%zu, size_to_send=%zu)\n",
333+
bytes_sent, size_to_send);
334+
return false;
335+
}
336+
if (n == 0) {
337+
GGML_LOG_ERROR("send returned 0 (peer closed?)\n");
337338
return false;
338339
}
339-
bytes_sent += n;
340+
bytes_sent += (size_t)n;
340341
}
341342
return true;
342343
}
343344

344345

346+
345347
static bool recv_data(sockfd_t sockfd, void * data, size_t size) {
346348
size_t bytes_recv = 0;
347349
while (bytes_recv < size) {
348-
size_t size_to_recv = size - bytes_recv;
349-
if (size_to_recv > RPC_IO_CHUNK) size_to_recv = RPC_IO_CHUNK;
350+
size_t size_to_recv = std::min(size - bytes_recv, MAX_CHUNK_SIZE);
350351
ssize_t n = recv(sockfd, (char *)data + bytes_recv, size_to_recv, 0);
351-
if (n <= 0) {
352-
#ifndef _WIN32
353-
perror("recv");
354-
#else
355-
fprintf(stderr, "recv failed (bytes_recv=%zu, size_to_recv=%zu)\n", bytes_recv, size_to_recv);
356-
#endif
352+
if (n < 0) {
353+
GGML_LOG_ERROR("recv failed (bytes_recv=%zu, size_to_recv=%zu)\n",
354+
bytes_recv, size_to_recv);
357355
return false;
358356
}
359-
bytes_recv += n;
357+
if (n == 0) {
358+
GGML_LOG_ERROR("recv returned 0 (peer closed?)\n");
359+
return false;
360+
}
361+
bytes_recv += (size_t)n;
360362
}
361363
return true;
362364
}
363365

364366

367+
365368
static bool send_msg(sockfd_t sockfd, const void * msg, size_t msg_size) {
366369
if (!send_data(sockfd, &msg_size, sizeof(msg_size))) {
367370
return false;

0 commit comments

Comments
 (0)