1
0
mirror of https://github.com/OpenTTD/OpenTTD synced 2026-01-18 09:52:44 +01:00

Codechange: use std::span for transferring data in network code

This commit is contained in:
Rubidium
2025-04-27 20:08:45 +02:00
committed by rubidium42
parent b7e7f08f78
commit c6ea0ce961
9 changed files with 71 additions and 88 deletions

View File

@@ -446,18 +446,6 @@ static bool GunzipFile(const ContentInfo &ci)
#endif /* defined(WITH_ZLIB) */
}
/**
* Simple wrapper around fwrite to be able to pass it to Packet's TransferOut.
* @param file The file to write data to.
* @param buffer The buffer to write to the file.
* @param amount The number of bytes to write.
* @return The number of bytes that were written.
*/
static inline ssize_t TransferOutFWrite(std::optional<FileHandle> &file, const char *buffer, size_t amount)
{
return fwrite(buffer, 1, amount, *file);
}
bool ClientNetworkContentSocketHandler::Receive_SERVER_CONTENT(Packet &p)
{
if (!this->cur_file.has_value()) {
@@ -474,8 +462,11 @@ bool ClientNetworkContentSocketHandler::Receive_SERVER_CONTENT(Packet &p)
}
} else {
/* We have a file opened, thus are downloading internal content */
size_t to_read = p.RemainingBytesToTransfer();
if (to_read != 0 && static_cast<size_t>(p.TransferOut(TransferOutFWrite, std::ref(this->cur_file))) != to_read) {
ssize_t to_read = p.RemainingBytesToTransfer();
auto write_to_disk = [this](std::span<const uint8_t> buffer) {
return fwrite(buffer.data(), 1, buffer.size(), *this->cur_file);
};
if (to_read != 0 && p.TransferOut(write_to_disk) != to_read) {
CloseWindowById(WC_NETWORK_STATUS_WINDOW, WN_NETWORK_STATUS_WINDOW_CONTENT_DOWNLOAD);
ShowErrorMessage(
GetEncodedString(STR_CONTENT_ERROR_COULD_NOT_DOWNLOAD),