Commit 540a3025 authored by Sébastien Blin's avatar Sébastien Blin

ftp_server: always get data_transfer id

Currently, when an incoming request is cancelled the transfer id
is lost. This patch avoid this case and allow the cancel part in
p2p.cpp to clear the data related to this transfer.

Change-Id: Ia5b69b514fce19e80ee3344d40eb395470212ef3
parent 24c74f15
...@@ -598,7 +598,6 @@ void ...@@ -598,7 +598,6 @@ void
DataTransferFacade::Impl::cancel(DataTransfer& transfer) DataTransferFacade::Impl::cancel(DataTransfer& transfer)
{ {
transfer.close(); transfer.close();
map_.erase(transfer.getId());
} }
std::shared_ptr<DataTransfer> std::shared_ptr<DataTransfer>
...@@ -773,15 +772,16 @@ DataTransferFacade::info(const DRing::DataTransferId& id, ...@@ -773,15 +772,16 @@ DataTransferFacade::info(const DRing::DataTransferId& id,
return DRing::DataTransferError::unknown; return DRing::DataTransferError::unknown;
} }
std::shared_ptr<Stream> IncomingFileInfo
DataTransferFacade::onIncomingFileRequest(const DRing::DataTransferInfo& info) DataTransferFacade::onIncomingFileRequest(const DRing::DataTransferInfo &info) {
{
auto transfer = pimpl_->createIncomingFileTransfer(info); auto transfer = pimpl_->createIncomingFileTransfer(info);
if (!transfer)
return {};
auto filename = transfer->requestFilename(); auto filename = transfer->requestFilename();
if (!filename.empty()) if (!filename.empty())
if (transfer->start()) if (transfer->start())
return std::static_pointer_cast<Stream>(transfer); return {transfer->getId(), std::static_pointer_cast<Stream>(transfer)};
return {}; return {transfer->getId(), nullptr};
} }
} // namespace jami } // namespace jami
...@@ -29,6 +29,11 @@ namespace jami { ...@@ -29,6 +29,11 @@ namespace jami {
class Stream; class Stream;
struct IncomingFileInfo {
DRing::DataTransferId id;
std::shared_ptr<Stream> stream;
};
/// Front-end to data transfer service /// Front-end to data transfer service
class DataTransferFacade class DataTransferFacade
{ {
...@@ -61,7 +66,7 @@ public: ...@@ -61,7 +66,7 @@ public:
/// Create an IncomingFileTransfer object. /// Create an IncomingFileTransfer object.
/// \return a shared pointer on created Stream object, or nullptr in case of error /// \return a shared pointer on created Stream object, or nullptr in case of error
std::shared_ptr<Stream> onIncomingFileRequest(const DRing::DataTransferInfo& info); IncomingFileInfo onIncomingFileRequest(const DRing::DataTransferInfo &info);
private: private:
class Impl; class Impl;
......
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
#include "logger.h" #include "logger.h"
#include "string_utils.h" #include "string_utils.h"
#include "data_transfer.h"
#include "manager.h" #include "manager.h"
#include <algorithm> #include <algorithm>
...@@ -46,9 +45,8 @@ DRing::DataTransferId ...@@ -46,9 +45,8 @@ DRing::DataTransferId
FtpServer::getId() const FtpServer::getId() const
{ {
// Because FtpServer is just the protocol on the top of a stream so the id // Because FtpServer is just the protocol on the top of a stream so the id
// of the stream is the id of out_. Because the output stream can be // of the stream is the id of out_.
// uninitialized, a FtpServer without a stream can have an id equal to 0. return out_.id;
return out_? out_->getId() : 0;
} }
void void
...@@ -70,21 +68,21 @@ FtpServer::startNewFile() ...@@ -70,21 +68,21 @@ FtpServer::startNewFile()
info.bytesProgress = 0; info.bytesProgress = 0;
rx_ = 0; rx_ = 0;
out_ = Manager::instance().dataTransfers->onIncomingFileRequest(info); // we block here until answer from client out_ = Manager::instance().dataTransfers->onIncomingFileRequest(info); // we block here until answer from client
if (!out_) { if (!out_.stream) {
JAMI_DBG() << "[FTP] transfer aborted by client"; JAMI_DBG() << "[FTP] transfer aborted by client";
closed_ = true; // send NOK msg at next read() closed_ = true; // send NOK msg at next read()
} else { } else {
go_ = true; go_ = true;
} }
return bool(out_); return bool(out_.stream);
} }
void void
FtpServer::closeCurrentFile() FtpServer::closeCurrentFile()
{ {
if (out_) { if (out_.stream) {
out_->close(); out_.stream->close();
out_.reset(); out_.stream.reset();
closed_ = true; closed_ = true;
} }
} }
...@@ -92,7 +90,7 @@ FtpServer::closeCurrentFile() ...@@ -92,7 +90,7 @@ FtpServer::closeCurrentFile()
bool bool
FtpServer::read(std::vector<uint8_t>& buffer) const FtpServer::read(std::vector<uint8_t>& buffer) const
{ {
if (!out_) { if (!out_.stream) {
if (closed_) { if (closed_) {
closed_ = false; closed_ = false;
if (rx_ < fileSize_) { if (rx_ < fileSize_) {
...@@ -134,8 +132,8 @@ FtpServer::write(const std::vector<uint8_t>& buffer) ...@@ -134,8 +132,8 @@ FtpServer::write(const std::vector<uint8_t>& buffer)
break; break;
auto size_needed = fileSize_ - rx_; auto size_needed = fileSize_ - rx_;
count = std::min(count, size_needed); count = std::min(count, size_needed);
if (out_) if (out_.stream)
out_->write(reinterpret_cast<const uint8_t*>(&line_[0]), count); out_.stream->write(reinterpret_cast<const uint8_t*>(&line_[0]), count);
rx_ += count; rx_ += count;
if (rx_ == fileSize_) { if (rx_ == fileSize_) {
closeCurrentFile(); closeCurrentFile();
...@@ -150,8 +148,8 @@ FtpServer::write(const std::vector<uint8_t>& buffer) ...@@ -150,8 +148,8 @@ FtpServer::write(const std::vector<uint8_t>& buffer)
case FtpState::READ_DATA: case FtpState::READ_DATA:
{ {
if (out_) if (out_.stream)
out_->write(&buffer[0], buffer.size()); out_.stream->write(&buffer[0], buffer.size());
auto size_needed = fileSize_ - rx_; auto size_needed = fileSize_ - rx_;
auto read_size = std::min(buffer.size(), size_needed); auto read_size = std::min(buffer.size(), size_needed);
rx_ += read_size; rx_ += read_size;
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#pragma once #pragma once
#include "data_transfer.h"
#include "peer_connection.h" #include "peer_connection.h"
#include <vector> #include <vector>
...@@ -53,7 +54,7 @@ private: ...@@ -53,7 +54,7 @@ private:
const std::string accountId_; const std::string accountId_;
const std::string peerUri_; const std::string peerUri_;
std::shared_ptr<Stream> out_; IncomingFileInfo out_ {0, nullptr};
std::size_t fileSize_ {0}; std::size_t fileSize_ {0};
std::size_t rx_ {0}; std::size_t rx_ {0};
std::stringstream headerStream_; std::stringstream headerStream_;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment