Default ValidateMessage implementation

This commit is contained in:
Dan Paulat 2021-12-24 12:06:38 -06:00
parent 0303412519
commit cb86ab4b9b
6 changed files with 59 additions and 48 deletions

View file

@ -30,11 +30,13 @@ protected:
Message(Message&&) noexcept; Message(Message&&) noexcept;
Message& operator=(Message&&) noexcept; Message& operator=(Message&&) noexcept;
virtual bool ValidateMessage(std::istream& is, size_t bytesRead) const = 0; virtual bool ValidateMessage(std::istream& is, size_t bytesRead) const;
public: public:
virtual ~Message(); virtual ~Message();
virtual size_t data_size() const = 0;
virtual bool Parse(std::istream& is) = 0; virtual bool Parse(std::istream& is) = 0;
static void ReadBoolean(std::istream& is, bool& value) static void ReadBoolean(std::istream& is, bool& value)

View file

@ -29,11 +29,11 @@ protected:
Level2Message(Level2Message&&) noexcept; Level2Message(Level2Message&&) noexcept;
Level2Message& operator=(Level2Message&&) noexcept; Level2Message& operator=(Level2Message&&) noexcept;
bool ValidateMessage(std::istream& is, size_t bytesRead) const override;
public: public:
virtual ~Level2Message(); virtual ~Level2Message();
size_t data_size() const override;
const Level2MessageHeader& header() const; const Level2MessageHeader& header() const;
void set_header(Level2MessageHeader&& header); void set_header(Level2MessageHeader&& header);

View file

@ -48,9 +48,11 @@ public:
bool IsCompressionEnabled() const; bool IsCompressionEnabled() const;
bool Parse(std::istream& is); size_t data_size() const override;
static const size_t SIZE = 102u; bool Parse(std::istream& is) override;
static constexpr size_t SIZE = 102u;
private: private:
std::unique_ptr<ProductDescriptionBlockImpl> p; std::unique_ptr<ProductDescriptionBlockImpl> p;

View file

@ -1,5 +1,7 @@
#include <scwx/wsr88d/message.hpp> #include <scwx/wsr88d/message.hpp>
#include <boost/log/trivial.hpp>
namespace scwx namespace scwx
{ {
namespace wsr88d namespace wsr88d
@ -20,5 +22,46 @@ Message::~Message() = default;
Message::Message(Message&&) noexcept = default; Message::Message(Message&&) noexcept = default;
Message& Message::operator=(Message&&) noexcept = default; Message& Message::operator=(Message&&) noexcept = default;
bool Message::ValidateMessage(std::istream& is, size_t bytesRead) const
{
bool messageValid = true;
const size_t dataSize = data_size();
if (is.eof())
{
BOOST_LOG_TRIVIAL(warning) << logPrefix_ << "Reached end of data stream";
messageValid = false;
}
else if (is.fail())
{
BOOST_LOG_TRIVIAL(warning)
<< logPrefix_ << "Could not read from input stream";
messageValid = false;
}
else if (bytesRead != dataSize)
{
is.seekg(static_cast<std::streamoff>(dataSize) -
static_cast<std::streamoff>(bytesRead),
std::ios_base::cur);
if (bytesRead < dataSize)
{
BOOST_LOG_TRIVIAL(trace)
<< logPrefix_ << "Message contents smaller than size: " << bytesRead
<< " < " << dataSize << " bytes";
}
if (bytesRead > dataSize)
{
BOOST_LOG_TRIVIAL(warning)
<< logPrefix_ << "Message contents larger than size: " << bytesRead
<< " > " << dataSize << " bytes";
messageValid = false;
}
}
return messageValid;
}
} // namespace wsr88d } // namespace wsr88d
} // namespace scwx } // namespace scwx

View file

@ -29,44 +29,9 @@ Level2Message::~Level2Message() = default;
Level2Message::Level2Message(Level2Message&&) noexcept = default; Level2Message::Level2Message(Level2Message&&) noexcept = default;
Level2Message& Level2Message::operator=(Level2Message&&) noexcept = default; Level2Message& Level2Message::operator=(Level2Message&&) noexcept = default;
bool Level2Message::ValidateMessage(std::istream& is, size_t bytesRead) const size_t Level2Message::data_size() const
{ {
bool messageValid = true; return (header().message_size() * 2 - header().SIZE);
size_t dataSize = header().message_size() * 2 - header().SIZE;
if (is.eof())
{
BOOST_LOG_TRIVIAL(warning) << logPrefix_ << "Reached end of data stream";
messageValid = false;
}
else if (is.fail())
{
BOOST_LOG_TRIVIAL(warning)
<< logPrefix_ << "Could not read from input stream";
messageValid = false;
}
else if (bytesRead != dataSize)
{
is.seekg(static_cast<std::streamoff>(dataSize) -
static_cast<std::streamoff>(bytesRead),
std::ios_base::cur);
if (bytesRead < dataSize)
{
BOOST_LOG_TRIVIAL(trace)
<< logPrefix_ << "Message contents smaller than size: " << bytesRead
<< " < " << dataSize << " bytes";
}
if (bytesRead > dataSize)
{
BOOST_LOG_TRIVIAL(warning)
<< logPrefix_ << "Message contents larger than size: " << bytesRead
<< " > " << dataSize << " bytes";
messageValid = false;
}
}
return messageValid;
} }
const Level2MessageHeader& Level2Message::header() const const Level2MessageHeader& Level2Message::header() const

View file

@ -7,12 +7,6 @@
#include <boost/log/trivial.hpp> #include <boost/log/trivial.hpp>
#ifdef WIN32
# include <WinSock2.h>
#else
# include <arpa/inet.h>
#endif
namespace scwx namespace scwx
{ {
namespace wsr88d namespace wsr88d
@ -209,6 +203,11 @@ bool ProductDescriptionBlock::IsCompressionEnabled() const
return isCompressed; return isCompressed;
} }
size_t ProductDescriptionBlock::data_size() const
{
return SIZE;
}
bool ProductDescriptionBlock::Parse(std::istream& is) bool ProductDescriptionBlock::Parse(std::istream& is)
{ {
bool blockValid = true; bool blockValid = true;