You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
lokinet/llarp/router_version.cpp

93 lines
2.1 KiB
C++

4 years ago
#include <router_version.hpp>
#include <constants/version.hpp>
#include <constants/proto.hpp>
#include <algorithm>
#include <cassert>
namespace llarp
{
RouterVersion::RouterVersion(const Version_t& router, uint64_t proto)
: m_Version(router), m_ProtoVersion(proto)
4 years ago
{
}
bool
RouterVersion::IsCompatableWith(const RouterVersion& other) const
{
return m_ProtoVersion == other.m_ProtoVersion;
4 years ago
}
bool
RouterVersion::BEncode(llarp_buffer_t* buf) const
{
if(not bencode_start_list(buf))
return false;
if(not IsEmpty())
{
if(not bencode_write_uint64(buf, m_ProtoVersion))
return false;
for(const auto& i : m_Version)
{
if(not bencode_write_uint64(buf, i))
4 years ago
return false;
}
4 years ago
}
return bencode_end(buf);
}
void
RouterVersion::Clear()
{
m_Version.fill(0);
m_ProtoVersion = LLARP_PROTO_VERSION;
4 years ago
assert(IsEmpty());
}
static const RouterVersion emptyRouterVersion({0, 0, 0}, LLARP_PROTO_VERSION);
4 years ago
bool
RouterVersion::IsEmpty() const
{
return std::equal(begin(), end(), emptyRouterVersion.begin(),
emptyRouterVersion.end());
4 years ago
}
bool
RouterVersion::BDecode(llarp_buffer_t* buf)
{
// clear before hand
Clear();
4 years ago
size_t idx = 0;
if(not bencode_read_list(
[self = this, &idx](llarp_buffer_t* buffer, bool has) {
if(has)
{
if(idx == 0)
{
if(not bencode_read_integer(buffer, &self->m_ProtoVersion))
return false;
}
else if(not bencode_read_integer(buffer, &self->at(idx - 1)))
4 years ago
return false;
++idx;
}
return true;
},
buf))
return false;
// either full list or empty list is valid
return idx == 4 || idx == 0;
4 years ago
}
std::string
RouterVersion::ToString() const
{
return std::to_string(m_Version.at(0)) + "."
+ std::to_string(m_Version.at(1)) + "."
+ std::to_string(m_Version.at(2)) + " protocol version "
+ std::to_string(m_ProtoVersion);
4 years ago
}
} // namespace llarp