Skip to content

Commit

Permalink
implement TelegramBuffer which acts like a stream, with ability to ex…
Browse files Browse the repository at this point in the history
…tract well formed telegrams
  • Loading branch information
w0lek committed Nov 17, 2023
1 parent eb61212 commit 06a974e
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/InteractivePathAnalysis/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,12 @@ void Client::handleResponse(const QByteArray& bytes)
}
}

void Client::sendRequest(const QByteArray& requestBytes, const QString& initiator)
void Client::sendRequest(QByteArray& requestBytes, const QString& initiator)
{
if (!m_socket.isConnected()) {
m_socket.connect();
}
requestBytes.append(static_cast<unsigned char>(TELEGRAM_FRAME_DELIMETER));
qDebug() << "sending:" << requestBytes << QString("requested by [%1]").arg(initiator);
if (!m_socket.write(requestBytes)) {
qCritical() << "unable to send" << requestBytes;
Expand Down
2 changes: 1 addition & 1 deletion src/InteractivePathAnalysis/client/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public slots:
QTimer m_timer;
#endif // ENABLE_AUTOMATIC_REQUEST

void sendRequest(const QByteArray&, const QString&);
void sendRequest(QByteArray&, const QString&);
void handleResponse(const QByteArray&);
};

2 changes: 1 addition & 1 deletion src/InteractivePathAnalysis/client/keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ constexpr const char* KEY_CMD = "CMD";
constexpr const char* KEY_OPTIONS = "OPTIONS";
constexpr const char* KEY_DATA = "DATA";
constexpr const char* KEY_STATUS = "STATUS";
constexpr const char* END_TELEGRAM_SEQUENCE = "###___END_FRAME___###";
constexpr const unsigned char TELEGRAM_FRAME_DELIMETER{0x17}; // 0x17 - End of Transmission Block

enum CMD {
CMD_GET_PATH_LIST_ID=0,
Expand Down
12 changes: 6 additions & 6 deletions src/InteractivePathAnalysis/client/tcpsocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,18 @@ void TcpSocket::handleStateChanged(QAbstractSocket::SocketState state)
void TcpSocket::handleDataReady()
{
QByteArray bytes = m_socket.readAll();
m_bytesBuf.append(bytes);
m_telegramBuff.append(bytes.constData());

if (bytes.endsWith(END_TELEGRAM_SEQUENCE)) {
m_bytesBuf.chop(strlen(END_TELEGRAM_SEQUENCE)); // remove end telegram sequence
emit dataRecieved(m_bytesBuf);
m_bytesBuf.clear();
auto frames = m_telegramBuff.takeFrames();
for (const ByteArray& frame: frames) {
QByteArray bytes(reinterpret_cast<const char*>(frame.data().data()), frame.data().size());
emit dataRecieved(bytes);
}
}

void TcpSocket::handleError(QAbstractSocket::SocketError)
{
m_bytesBuf.clear();
m_telegramBuff.clear();
qDebug() << m_socket.errorString();
}

Expand Down
4 changes: 3 additions & 1 deletion src/InteractivePathAnalysis/client/tcpsocket.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include "telegrambuffer.h"

#include <QObject>
#include <QByteArray>
#include <QTcpSocket>
Expand Down Expand Up @@ -32,7 +34,7 @@ class TcpSocket : public QObject {
private:
QTcpSocket m_socket;
QTimer m_connectionWatcher;
QByteArray m_bytesBuf; // to aggregate chunk of telegrams
TelegramBuffer m_telegramBuff;

bool ensureConnected();

Expand Down
25 changes: 25 additions & 0 deletions src/InteractivePathAnalysis/client/telegrambuffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "telegrambuffer.h"
#include "keys.h"

void TelegramBuffer::append(const ByteArray& bytes)
{
m_rawBuffer.append(bytes);
}

std::vector<ByteArray> TelegramBuffer::takeFrames()
{
std::vector<ByteArray> result;
ByteArray candidate;
for (unsigned char b: m_rawBuffer.data()) {
if (b == TELEGRAM_FRAME_DELIMETER) {
if (!candidate.empty()) {
result.push_back(candidate);
}
candidate = ByteArray();
} else {
candidate.append(b);
}
}
std::swap(m_rawBuffer, candidate);
return result;
}
57 changes: 57 additions & 0 deletions src/InteractivePathAnalysis/client/telegrambuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#pragma once

#include <vector>
#include <string>
#include <cstring>

class ByteArray {
public:
static const std::size_t DEFAULT_SIZE_HINT = 1024;

ByteArray(const char* data)
: m_data(reinterpret_cast<const unsigned char*>(data),
reinterpret_cast<const unsigned char*>(data + strlen(data)))
{}
ByteArray(std::size_t sizeHint = DEFAULT_SIZE_HINT) {
m_data.reserve(sizeHint);
}

void append(const ByteArray& appendix) {
for (unsigned char b: appendix.data()) {
m_data.push_back(b);
}
}

void append(unsigned char b) {
m_data.push_back(b);
}

std::string to_string() const {
return std::string(reinterpret_cast<const char*>(m_data.data()), m_data.size());
}

bool empty() const { return m_data.empty(); }
const std::vector<unsigned char>& data() const { return m_data; }
void clear() { m_data.clear(); }

private:
std::vector<unsigned char> m_data;
};

class TelegramBuffer
{
static const std::size_t DEFAULT_SIZE_HINT = 1024;
public:
TelegramBuffer(std::size_t sizeHint = DEFAULT_SIZE_HINT): m_rawBuffer(sizeHint) {}
~TelegramBuffer()=default;

void clear() { m_rawBuffer.clear(); }

void append(const ByteArray&);
std::vector<ByteArray> takeFrames();

const ByteArray& data() const { return m_rawBuffer; }

private:
ByteArray m_rawBuffer;
};
2 changes: 2 additions & 0 deletions src/Main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ set (SRC_CPP_LIST ../Main/Foedag_ql.cpp
../InteractivePathAnalysis/client/client.cpp
../InteractivePathAnalysis/client/process.cpp
../InteractivePathAnalysis/client/tcpsocket.cpp
../InteractivePathAnalysis/client/telegrambuffer.cpp
../InteractivePathAnalysis/client/requestcreator.cpp
)

Expand Down Expand Up @@ -138,6 +139,7 @@ set (SRC_H_LIST ../Main/Foedag.h
../InteractivePathAnalysis/client/process.h
../InteractivePathAnalysis/client/keys.h
../InteractivePathAnalysis/client/tcpsocket.h
../InteractivePathAnalysis/client/telegrambuffer.h
../InteractivePathAnalysis/client/requestcreator.h
)

Expand Down

0 comments on commit 06a974e

Please sign in to comment.