mirror of
https://gitlab.com/niansa/llama_nds.git
synced 2025-03-06 20:53:28 +01:00
43 lines
904 B
C++
43 lines
904 B
C++
#include "Receiver.hpp"
|
|
|
|
#include <string_view>
|
|
#include <array>
|
|
#ifndef PLATFORM_WINDOWS
|
|
# include <sys/select.h>
|
|
# include <sys/socket.h>
|
|
#else
|
|
# include <ws2tcpip.h>
|
|
#endif
|
|
|
|
|
|
|
|
std::string Receiver::Simple::read(size_t amount) {
|
|
// Create buffer
|
|
std::string fres;
|
|
fres.resize(amount);
|
|
|
|
// Read into buffer
|
|
read(reinterpret_cast<std::byte*>(fres.data()), fres.size());
|
|
|
|
// Return final buffer
|
|
return fres;
|
|
}
|
|
void Receiver::Simple::read(std::byte *buffer, size_t size) {
|
|
recv(fd, buffer, size, MSG_WAITALL);
|
|
}
|
|
|
|
std::string Receiver::Simple::readSome(size_t max) {
|
|
// Create buffer
|
|
std::string fres;
|
|
fres.resize(max);
|
|
|
|
// Receive data
|
|
ssize_t bytesRead;
|
|
if ((bytesRead = recv(fd, fres.data(), max, MSG_WAITALL)) < 0) [[unlikely]] {
|
|
return "";
|
|
}
|
|
|
|
// Resize and return final buffer
|
|
fres.resize(bytesRead);
|
|
return fres;
|
|
}
|