Skip to content

Commit

Permalink
Merge pull request #412 from silicon-heaven/change-socket-frame-api
Browse files Browse the repository at this point in the history
Change Socket frame retrieve API to less error prone
  • Loading branch information
fvacek authored Feb 12, 2024
2 parents c74e75c + d4cddfa commit 3d84095
Show file tree
Hide file tree
Showing 14 changed files with 28 additions and 41 deletions.
2 changes: 1 addition & 1 deletion libshvchainpack/include/shv/chainpack/rpcdriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SHVCHAINPACK_DECL_EXPORT RpcDriver

virtual void writeFrameData(const std::string &frame_data) = 0;
/// call it when new data arrived
virtual void onFrameDataRead(std::string &&frame_data);
virtual void onFrameDataRead(const std::string &frame_data);

virtual void onRpcFrameReceived(RpcFrame &&frame);
virtual void onParseDataException(const shv::chainpack::ParseException &e) = 0;
Expand Down
2 changes: 1 addition & 1 deletion libshvchainpack/include/shv/chainpack/rpcmessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct SHVCHAINPACK_DECL_EXPORT RpcFrame
bool isValid() const { return !meta.isEmpty() && !data.empty(); }
RpcMessage toRpcMessage(std::string *errmsg = nullptr) const;
std::string toChainPack() const;
static RpcFrame fromChainPack(std::string &&frame_data);
static RpcFrame fromChainPack(const std::string &frame_data);
};

class SHVCHAINPACK_DECL_EXPORT RpcMessage
Expand Down
4 changes: 2 additions & 2 deletions libshvchainpack/src/chainpack/rpcdriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ void RpcDriver::sendRpcFrame(RpcFrame &&frame)
writeFrameData(frame_data);
}

void RpcDriver::onFrameDataRead(std::string &&frame_data)
void RpcDriver::onFrameDataRead(const std::string &frame_data)
{
logRpcData() << __PRETTY_FUNCTION__ << "+++++++++++++++++++++++++++++++++";
logRpcData().nospace() << "FRAME DATA " << frame_data.size() << " bytes of data read:\n" << shv::chainpack::utils::hexDump(frame_data);
try {
auto frame = RpcFrame::fromChainPack(std::move(frame_data));
auto frame = RpcFrame::fromChainPack(frame_data);
onRpcFrameReceived(std::move(frame));
}
catch (const ParseException &e) {
Expand Down
2 changes: 1 addition & 1 deletion libshvchainpack/src/chainpack/rpcmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ std::string RpcFrame::toChainPack() const
return ret;
}

RpcFrame RpcFrame::fromChainPack(std::string &&frame_data)
RpcFrame RpcFrame::fromChainPack(const std::string &frame_data)
{
std::istringstream in(frame_data);
auto protocol = in.get();
Expand Down
2 changes: 1 addition & 1 deletion libshviotqt/include/shv/iotqt/rpc/localsocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SHVIOTQT_DECL_EXPORT LocalSocket : public Socket
LocalSocket(QLocalSocket *socket, Protocol protocol, QObject *parent = nullptr);
~LocalSocket() override;

std::string readFrameData() override;
std::vector<std::string> takeFrames() override;
void writeFrameData(const std::string &frame_data) override;

void connectToHost(const QUrl &url) override;
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 @@ -57,7 +57,7 @@ class SHVIOTQT_DECL_EXPORT SerialPortSocket : public Socket
public:
SerialPortSocket(QSerialPort *port, QObject *parent = nullptr);

std::string readFrameData() override;
std::vector<std::string> takeFrames() override;
void writeFrameData(const std::string &frame_data) override;

void setReceiveTimeout(int millis);
Expand Down
19 changes: 8 additions & 11 deletions libshviotqt/include/shv/iotqt/rpc/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include <QSslSocket>
#include <QSslError>

#include <deque>
#include <vector>

class QTcpSocket;

Expand All @@ -24,16 +24,13 @@ class FrameReader {
virtual ~FrameReader() = default;
virtual void addData(std::string_view data) = 0;
bool isEmpty() const { return m_frames.empty(); }
std::string getFrame() {
if (!m_frames.empty()) {
auto frame = m_frames.front();
m_frames.pop_front();
return frame;
}
return {};
std::vector<std::string> takeFrames() {
auto frames = std::move(m_frames);
m_frames = {};
return frames;
}
protected:
std::deque<std::string> m_frames;
std::vector<std::string> m_frames;
};

class FrameWriter
Expand Down Expand Up @@ -93,7 +90,7 @@ class SHVIOTQT_DECL_EXPORT Socket : public QObject
virtual QHostAddress peerAddress() const = 0;
virtual quint16 peerPort() const = 0;

virtual std::string readFrameData() = 0;
virtual std::vector<std::string> takeFrames() = 0;
virtual void writeFrameData(const std::string &frame_data) = 0;

virtual void ignoreSslErrors() = 0;
Expand All @@ -117,7 +114,7 @@ class SHVIOTQT_DECL_EXPORT TcpSocket : public Socket
public:
TcpSocket(QTcpSocket *socket, QObject *parent = nullptr);

std::string readFrameData() override;
std::vector<std::string> takeFrames() override;
void writeFrameData(const std::string &frame_data) override;

void connectToHost(const QUrl &url) override;
Expand Down
2 changes: 1 addition & 1 deletion libshviotqt/include/shv/iotqt/rpc/websocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SHVIOTQT_DECL_EXPORT WebSocket : public Socket
public:
WebSocket(QWebSocket *socket, QObject *parent = nullptr);

std::string readFrameData() override;
std::vector<std::string> takeFrames() override;
void writeFrameData(const std::string &frame_data) override;

void connectToHost(const QUrl &url) override;
Expand Down
6 changes: 2 additions & 4 deletions libshviotqt/src/rpc/localsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,9 @@ LocalSocket::~LocalSocket()
delete m_frameWriter;
}

std::string LocalSocket::readFrameData()
std::vector<std::string> LocalSocket::takeFrames()
{
if(m_frameReader->isEmpty())
return {};
return m_frameReader->getFrame();
return m_frameReader->takeFrames();
}

void LocalSocket::writeFrameData(const std::string &frame_data)
Expand Down
6 changes: 2 additions & 4 deletions libshviotqt/src/rpc/serialportsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,9 @@ quint16 SerialPortSocket::peerPort() const
return 0;
}

std::string SerialPortSocket::readFrameData()
std::vector<std::string> SerialPortSocket::takeFrames()
{
if(m_frameReader.isEmpty())
return {};
return m_frameReader.getFrame();
return m_frameReader.takeFrames();
}

void SerialPortSocket::writeFrameData(const std::string &frame_data)
Expand Down
6 changes: 2 additions & 4 deletions libshviotqt/src/rpc/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,9 @@ quint16 TcpSocket::peerPort() const
return m_socket->peerPort();
}

std::string TcpSocket::readFrameData()
std::vector<std::string> TcpSocket::takeFrames()
{
if(m_frameReader.isEmpty())
return {};
return m_frameReader.getFrame();
return m_frameReader.takeFrames();
}

void TcpSocket::writeFrameData(const std::string &frame_data)
Expand Down
8 changes: 3 additions & 5 deletions libshviotqt/src/rpc/socketrpcconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,9 @@ void SocketRpcConnection::connectToHost(const QUrl &url)

void SocketRpcConnection::onReadyRead()
{
while (true) {
auto frame_data = socket()->readFrameData();
if (frame_data.empty())
break;
onFrameDataRead(std::move(frame_data));
auto frames = socket()->takeFrames();
for (const auto &frame : frames) {
onFrameDataRead(frame);
};
emit socketDataReadyRead();
}
Expand Down
6 changes: 2 additions & 4 deletions libshviotqt/src/rpc/websocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,9 @@ quint16 WebSocket::peerPort() const
return m_socket->peerPort();
}

std::string WebSocket::readFrameData()
std::vector<std::string> WebSocket::takeFrames()
{
if(m_frameReader.isEmpty())
return {};
return m_frameReader.getFrame();
return m_frameReader.takeFrames();
}

void WebSocket::writeFrameData(const std::string &frame_data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ DOCTEST_TEST_CASE("Test CRC error")
data[5] = ~data[5];

serial->setDataToReceive(data);
REQUIRE(socket->readFrameData().empty());
REQUIRE(socket->takeFrames().empty());
}
/*
DOCTEST_TEST_CASE("Test RESET message")
Expand Down

0 comments on commit 3d84095

Please sign in to comment.