forked from os-fpga/FOEDAG
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement TelegramBuffer which acts like a stream, with ability to ex…
…tract well formed telegrams
- Loading branch information
Showing
8 changed files
with
97 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters