Skip to content
Snippets Groups Projects
Commit 00330fd8 authored by Adrien Béraud's avatar Adrien Béraud Committed by Sébastien Blin
Browse files

file transfer: use string_view

Change-Id: If44dea4f9f1bf31ccb7237dca29c42735ecce5a8
parent 037d2f72
No related branches found
No related tags found
No related merge requests found
......@@ -281,7 +281,7 @@ public:
void close() noexcept override;
void closeAndEmit(DRing::DataTransferEventCode code) const noexcept;
bool read(std::vector<uint8_t>&) const override;
bool write(const std::vector<uint8_t>& buffer) override;
bool write(std::string_view) override;
void emit(DRing::DataTransferEventCode code) const override;
void cancel() override
......@@ -290,7 +290,7 @@ public:
account->closePeerConnection(id);
}
void setOnRecv(std::function<void(std::vector<uint8_t>&&)>&& cb) override
void setOnRecv(std::function<void(std::string_view)>&& cb) override
{
bool send = false;
{
......@@ -322,7 +322,7 @@ private:
headerSent_ = true;
emit(DRing::DataTransferEventCode::wait_peer_acceptance);
if (onRecvCb_)
onRecvCb_(std::move(buf));
onRecvCb_(std::string_view((const char*)buf.data(), buf.size()));
}
void sendFile() const
......@@ -340,7 +340,7 @@ private:
metaInfo_->updateInfo(info_);
}
if (onRecvCb_)
onRecvCb_(std::move(buf));
onRecvCb_(std::string_view((const char*)buf.data(), buf.size()));
}
JAMI_DBG() << "FTP#" << getId() << ": sent " << info_.bytesProgress << " bytes";
if (internalCompletionCb_)
......@@ -358,7 +358,7 @@ private:
mutable std::unique_ptr<std::thread> timeoutThread_;
mutable std::atomic_bool stopTimeout_ {false};
std::mutex onRecvCbMtx_;
std::function<void(std::vector<uint8_t>&&)> onRecvCb_ {};
std::function<void(std::string_view)> onRecvCb_ {};
};
SubOutgoingFileTransfer::SubOutgoingFileTransfer(DRing::DataTransferId tid,
......@@ -436,7 +436,7 @@ SubOutgoingFileTransfer::read(std::vector<uint8_t>& buf) const
}
bool
SubOutgoingFileTransfer::write(const std::vector<uint8_t>& buffer)
SubOutgoingFileTransfer::write(std::string_view buffer)
{
if (buffer.empty())
return true;
......@@ -528,7 +528,7 @@ public:
void close() noexcept override;
void cancel()
void cancel() override
{
for (const auto& subtransfer : subtransfer_)
subtransfer->cancel();
......@@ -591,7 +591,7 @@ public:
void accept(const std::string&, std::size_t offset) override;
bool write(const uint8_t* buffer, std::size_t length) override;
bool write(std::string_view data) override;
void cancel() override
{
......@@ -713,15 +713,15 @@ IncomingFileTransfer::accept(const std::string& filename, std::size_t offset)
}
bool
IncomingFileTransfer::write(const uint8_t* buffer, std::size_t length)
IncomingFileTransfer::write(std::string_view buffer)
{
if (!length)
if (buffer.empty())
return true;
fout_.write(reinterpret_cast<const char*>(buffer), length);
fout_ << buffer;
if (!fout_)
return false;
std::lock_guard<std::mutex> lk {infoMutex_};
info_.bytesProgress += length;
info_.bytesProgress += buffer.size();
return true;
}
......
......@@ -32,6 +32,8 @@
#include <iterator>
#include <charconv>
using namespace std::literals;
namespace jami {
//==============================================================================
......@@ -98,20 +100,7 @@ FtpServer::startNewFile()
}
if (shared->onRecvCb_) {
std::vector<uint8_t> buffer;
if (shared->go_) {
buffer.resize(3);
buffer[0] = 'G';
buffer[1] = 'O';
buffer[2] = '\n';
} else {
buffer.resize(4);
buffer[0] = 'N';
buffer[1] = 'G';
buffer[2] = 'O';
buffer[3] = '\n';
}
shared->onRecvCb_(std::move(buffer));
shared->onRecvCb_(shared->go_ ? "GO\n"sv : "NGO\n"sv);
}
if (shared->out_.stream) {
......@@ -123,9 +112,7 @@ FtpServer::startNewFile()
break;
auto size_needed = shared->fileSize_ - shared->rx_;
count = std::min(count, size_needed);
shared->out_.stream->write(reinterpret_cast<const uint8_t*>(
&shared->line_[0]),
count);
shared->out_.stream->write(std::string_view(shared->line_.data(), count));
shared->rx_ += count;
if (shared->rx_ == shared->fileSize_) {
shared->closeCurrentFile();
......@@ -179,7 +166,7 @@ FtpServer::read(std::vector<uint8_t>& buffer) const
}
bool
FtpServer::write(const std::vector<uint8_t>& buffer)
FtpServer::write(std::string_view buffer)
{
switch (state_) {
case FtpState::WAIT_ACCEPTANCE:
......@@ -196,7 +183,7 @@ FtpServer::write(const std::vector<uint8_t>& buffer)
case FtpState::READ_DATA: {
if (out_.stream)
out_.stream->write(&buffer[0], buffer.size());
out_.stream->write(buffer);
auto size_needed = fileSize_ - rx_;
auto read_size = std::min(buffer.size(), size_needed);
rx_ += read_size;
......@@ -217,9 +204,9 @@ FtpServer::write(const std::vector<uint8_t>& buffer)
}
bool
FtpServer::parseStream(const std::vector<uint8_t>& buffer)
FtpServer::parseStream(std::string_view buffer)
{
headerStream_.write((const char*)buffer.data(), buffer.size());
headerStream_ << buffer;
// Simple line stream parser
while (headerStream_.getline(&line_[0], line_.size())) {
......
......@@ -30,7 +30,7 @@
namespace jami {
using RecvCb = std::function<void(std::vector<uint8_t>&& buf)>;
using RecvCb = std::function<void(std::string_view buf)>;
class FtpServer final : public Stream, public std::enable_shared_from_this<FtpServer>
{
......@@ -41,12 +41,12 @@ public:
InternalCompletionCb&& cb = {});
bool read(std::vector<uint8_t>& buffer) const override;
bool write(const std::vector<uint8_t>& buffer) override;
bool write(std::string_view data) override;
DRing::DataTransferId getId() const override;
void close() noexcept override;
void setOnRecv(RecvCb&& cb) { onRecvCb_ = cb; }
void setOnStateChangedCb(const OnStateChangedCb& cb)
void setOnRecv(RecvCb&& cb) override { onRecvCb_ = cb; }
void setOnStateChangedCb(const OnStateChangedCb& cb) override
{
// If out_ is not attached, store the callback
// inside a temporary object. Will be linked when out_.stream
......@@ -58,7 +58,7 @@ public:
}
private:
bool parseStream(const std::vector<uint8_t>&);
bool parseStream(std::string_view);
bool parseLine(std::string_view);
void handleHeader(std::string_view key, std::string_view value);
void startNewFile();
......
......@@ -56,14 +56,14 @@ ChanneledOutgoingTransfer::linkTransfer(const std::shared_ptr<Stream>& file)
return;
file_ = file;
channel_->setOnRecv([this](const uint8_t* buf, size_t len) {
file_->write(std::vector<uint8_t>(buf, buf + len));
file_->write(std::string_view((const char*)buf, len));
return len;
});
file_->setOnRecv(
[channel = std::weak_ptr<ChannelSocket>(channel_)](std::vector<uint8_t>&& data) {
[channel = std::weak_ptr<ChannelSocket>(channel_)](std::string_view data) {
if (auto c = channel.lock()) {
std::error_code ec;
c->write(data.data(), data.size(), ec);
c->write((const uint8_t*)data.data(), data.size(), ec);
}
});
file_->setOnStateChangedCb(stateChangedCb_);
......@@ -76,13 +76,13 @@ ChanneledIncomingTransfer::ChanneledIncomingTransfer(const std::shared_ptr<Chann
, channel_(channel)
{
channel_->setOnRecv([this](const uint8_t* buf, size_t len) {
ftp_->write(std::vector<uint8_t>(buf, buf + len));
ftp_->write(std::string_view((const char*)buf, len));
return len;
});
ftp_->setOnRecv([channel = std::weak_ptr<ChannelSocket>(channel_)](std::vector<uint8_t>&& data) {
ftp_->setOnRecv([channel = std::weak_ptr<ChannelSocket>(channel_)](std::string_view data) {
if (auto c = channel.lock()) {
std::error_code ec;
c->write(data.data(), data.size(), ec);
c->write((const uint8_t*)data.data(), data.size(), ec);
}
});
ftp_->setOnStateChangedCb(cb);
......
......@@ -68,18 +68,11 @@ public:
(void) buffer;
return false;
}
virtual bool write(const std::vector<uint8_t>& buffer)
virtual bool write(std::string_view)
{
(void) buffer;
return false;
};
virtual bool write(const uint8_t* buffer, std::size_t length)
{
(void) buffer;
(void) length;
return false;
};
virtual void setOnRecv(std::function<void(std::vector<uint8_t>&&)>&&)
virtual void setOnRecv(std::function<void(std::string_view)>&&)
{
// Not implemented
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment