Fixed file transfer not working

This commit is contained in:
WolverinDEV 2019-10-26 12:34:37 +01:00
parent 62bd63b6a9
commit d1f9778406
2 changed files with 21 additions and 6 deletions

View File

@ -376,7 +376,6 @@ void Transfer::callback_write(short flags) {
uint64_t buffer_size = 1400; /* best TCP packet size (equal to the MTU) */
pipes::buffer buffer{buffer_size};
auto read_status = source->read_bytes(error, buffer.data_ptr<uint8_t>(), buffer_size);
this->last_source_read = system_clock::now();
if(read_status != error::success) {
if(read_status == error::would_block) {
readd_write_for_read = true;
@ -398,6 +397,7 @@ void Transfer::callback_write(short flags) {
break;
}
this->last_source_read = system_clock::now();
{
lock_guard lock(this->queue_lock);
this->write_queue.push_back(buffer.range(0, buffer_size));

View File

@ -207,6 +207,7 @@ uint64_t TransferFileSource::byte_length() const {
bool TransferFileSource::initialize(std::string &error) {
auto file = fs::u8path(this->_path) / fs::u8path(this->_name);
log_debug(category::file_transfer, tr("Opening source file for transfer: {} ({})"), file.string(), fs::absolute(file).string());
error_code errc;
if(!fs::exists(file)) {
@ -252,22 +253,36 @@ void TransferFileSource::finalize() {
}
error::value TransferFileSource::read_bytes(std::string &error, uint8_t *buffer, uint64_t &length) {
auto result = this->file_stream.readsome((char*) buffer, length);
error.clear();
#ifdef WIN32
auto blength = this->byte_length();
if(this->position >= blength) {
error = "eof reached";
return error::custom;
}
if(this->position + length > this->byte_length())
length = this->byte_length() - this->position;
this->file_stream.read((char*) buffer, length);
this->position += length;
#else
auto result = this->file_stream.readsome((char*) buffer, length);
if(result > 0) {
length = result;
this->position += result;
return error::success;
}
} else if(result == 0) {
return error::would_block;
} else
error = "read returned " + to_string(result) + "/" + to_string(length);
#endif
if(!this->file_stream) {
if(this->file_stream.eof())
error = "eof reached";
else
error = "io error. failed to read";
} else {
error = "read returned " + to_string(result) + "/" + to_string(length);
}
return error::custom;
return error.empty() ? error::success : error::custom;
}
uint64_t TransferFileSource::stream_index() const {