MayaFlux 0.2.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ read_message()

std::expected< std::string, std::string > Lila::Server::read_message ( int  client_fd)
private

Reads a message from a client.

Parameters
client_fdClient file descriptor
Returns
Expected message string or error string

Definition at line 351 of file Server.cpp.

352{
353 constexpr size_t BUFFER_SIZE = 4096;
354 std::string result;
355
356 result.reserve(BUFFER_SIZE);
357 std::array<char, BUFFER_SIZE> buffer {};
358
359 while (true) {
360 ssize_t bytes_read = socket_read(client_fd, buffer.data(), buffer.size() - 1);
361
362 if (bytes_read < 0) {
363#ifdef MAYAFLUX_PLATFORM_WINDOWS
364 int code = socket_errno();
365 if (code == WSAEWOULDBLOCK) {
366 return std::unexpected("would_block");
367 }
368 return std::unexpected("read error: " + socket_error_string(code));
369#else
370 if (errno == EAGAIN || errno == EWOULDBLOCK) {
371 return std::unexpected("would_block");
372 }
373 return std::unexpected("read error: " + std::string(strerror(errno)));
374#endif
375 }
376
377 if (bytes_read == 0) {
378 return std::unexpected("client_disconnected");
379 }
380
381 buffer[bytes_read] = '\0';
382 result.append(buffer.data());
383
384 if (!result.empty() && result.back() == '\n') {
385 result.pop_back();
386 if (!result.empty() && result.back() == '\r') {
387 result.pop_back();
388 }
389 break;
390 }
391
392 if (result.size() > static_cast<size_t>(1024 * 1024)) {
393 return std::unexpected("message_too_large");
394 }
395 }
396
397 return result;
398}
static std::string socket_error_string(int code)
Definition Server.cpp:33
static int socket_errno()
Definition Server.cpp:24
static ssize_t socket_read(int fd, void *buf, size_t len)
Definition Server.cpp:52

References Lila::socket_errno(), Lila::socket_error_string(), and Lila::socket_read().

Referenced by handle_client().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: