Skip to content

Commit

Permalink
Rewrite FrameWriter::addFrame() from move to const reference
Browse files Browse the repository at this point in the history
  • Loading branch information
Fanda Vacek committed Nov 18, 2024
1 parent b6e3fc1 commit 9bbfc83
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 16 deletions.
11 changes: 10 additions & 1 deletion libshvchainpack/src/socketrpcdriver.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <shv/chainpack/socketrpcdriver.h>

#include <shv/chainpack/chainpackreader.h>
#include <shv/chainpack/chainpackwriter.h>
#include <shv/chainpack/utils.h>

#include <necrolog.h>
Expand Down Expand Up @@ -62,8 +63,16 @@ void SocketRpcDriver::writeFrame(RpcFrame &&frame)
nInfo() << "Write to closed socket";
return;
}
using namespace shv::chainpack;
auto frame_head = frame.toFrameHead();
if (m_writeBuffer.size() + frame_head.size() + frame.data.size() < m_maxWriteBufferLength) {
std::ostringstream out;
{
ChainPackWriter wr(out);
wr.writeUIntData(frame_head.size() + frame.data.size());
}
auto len_data = out.str();
if (m_writeBuffer.size() + len_data.size() + frame_head.size() + frame.data.size() < m_maxWriteBufferLength) {
m_writeBuffer += len_data;
m_writeBuffer += frame_head;
m_writeBuffer += frame.data;
flush();
Expand Down
2 changes: 1 addition & 1 deletion libshviotqt/include/shv/iotqt/rpc/serialportsocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class SHVIOTQT_DECL_EXPORT SerialFrameWriter : public FrameWriter

void resetCommunication() override;
protected:
void addFrameData(std::string &&frame_data) override;
void addFrameData(const std::string &frame_head, const std::string &frame_data) override;
private:
bool m_withCrcCheck = true;
};
Expand Down
9 changes: 5 additions & 4 deletions libshviotqt/include/shv/iotqt/rpc/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ class SHVIOTQT_DECL_EXPORT FrameWriter
{
public:
virtual ~FrameWriter() = default;
void addFrame(shv::chainpack::RpcFrame &&frame);
void addFrame(const shv::chainpack::RpcFrame &frame);
virtual void resetCommunication() {}
void flushToDevice(QIODevice *device);
void clear();
#ifdef WITH_SHV_WEBSOCKETS
void flushToWebSocket(QWebSocket *socket);
#endif
protected:
virtual void addFrameData(std::string &&frame_data) = 0;
virtual void addFrameData(const std::string &frame_head, const std::string &frame_data) = 0;
protected:
QList<QByteArray> m_messageDataToWrite;
};
Expand All @@ -72,8 +72,9 @@ class SHVIOTQT_DECL_EXPORT StreamFrameWriter : public FrameWriter
{
public:
~StreamFrameWriter() override = default;

protected:
void addFrameData(std::string &&frame_data) override;
void addFrameData(const std::string &frame_head, const std::string &frame_data) override;
};

/// wrapper class for QTcpSocket and QWebSocket
Expand Down Expand Up @@ -105,7 +106,7 @@ class SHVIOTQT_DECL_EXPORT Socket : public QObject
virtual quint16 peerPort() const = 0;

std::vector<chainpack::RpcFrame> takeFrames();
void writeFrame(chainpack::RpcFrame &&frame);
void writeFrame(const chainpack::RpcFrame &frame);

virtual void ignoreSslErrors() = 0;

Expand Down
7 changes: 5 additions & 2 deletions libshviotqt/src/rpc/serialportsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ SerialFrameWriter::SerialFrameWriter(CrcCheck crc)
{
}

void SerialFrameWriter::addFrameData(string &&frame_data)
void SerialFrameWriter::addFrameData(const std::string &frame_head, const std::string &frame_data)
{
QByteArray data_to_write;
auto write_escaped = [&data_to_write](uint8_t b) {
Expand All @@ -190,6 +190,9 @@ void SerialFrameWriter::addFrameData(string &&frame_data)
}
};
data_to_write += static_cast<char>(STX);
for(uint8_t b : frame_head) {
write_escaped(b);
}
for(uint8_t b : frame_data) {
write_escaped(b);
}
Expand All @@ -213,7 +216,7 @@ void SerialFrameWriter::resetCommunication()

QByteArray data_to_write;
data_to_write.append(static_cast<char>(00));
addFrameData(data_to_write.toStdString());
addFrameData(data_to_write.toStdString(), {});
}

//======================================================
Expand Down
14 changes: 7 additions & 7 deletions libshviotqt/src/rpc/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ namespace shv::iotqt::rpc {
//======================================================
// FrameWriter
//======================================================
void FrameWriter::addFrame(chainpack::RpcFrame &&frame)
void FrameWriter::addFrame(const chainpack::RpcFrame &frame)
{
try {
auto frame_data = frame.toFrameHead();
frame_data += frame.data;
addFrameData(std::move(frame_data));
auto frame_head = frame.toFrameHead();
addFrameData(frame_head, frame.data);
} catch (const std::runtime_error &e) {
shvWarning() << "Error converting frame to data:" << e.what();
}
Expand Down Expand Up @@ -171,7 +170,7 @@ QList<int> StreamFrameReader::addData(std::string_view data)
//======================================================
// StreamFrameWriter
//======================================================
void StreamFrameWriter::addFrameData(std::string &&frame_data)
void StreamFrameWriter::addFrameData(const std::string &frame_head, const std::string &frame_data)
{
using namespace shv::chainpack;
std::ostringstream out;
Expand All @@ -181,6 +180,7 @@ void StreamFrameWriter::addFrameData(std::string &&frame_data)
}
auto len_data = out.str();
QByteArray data(len_data.data(), len_data.size());
data.append(frame_head.data(), frame_head.size());
data.append(frame_data.data(), frame_data.size());
m_messageDataToWrite.append(data);
}
Expand Down Expand Up @@ -256,10 +256,10 @@ std::vector<chainpack::RpcFrame> Socket::takeFrames()
return m_frameReader->takeFrames();
}

void Socket::writeFrame(shv::chainpack::RpcFrame &&frame)
void Socket::writeFrame(const shv::chainpack::RpcFrame &frame)
{
Q_ASSERT(m_frameWriter);
m_frameWriter->addFrame(std::move(frame));
m_frameWriter->addFrame(frame);
flushWriteBuffer();
}

Expand Down
2 changes: 1 addition & 1 deletion libshviotqt/src/rpc/socketrpcconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ bool SocketRpcConnection::isOpen()

void SocketRpcConnection::writeFrame(chainpack::RpcFrame &&frame)
{
socket()->writeFrame(std::move(frame));
socket()->writeFrame(frame);
}

void SocketRpcConnection::sendRpcMessage(const shv::chainpack::RpcMessage &rpc_msg)
Expand Down

0 comments on commit 9bbfc83

Please sign in to comment.