lokinet/llarp/service/intro.hpp

94 lines
1.9 KiB
C++
Raw Normal View History

2018-07-09 17:32:11 +00:00
#ifndef LLARP_SERVICE_INTRO_HPP
#define LLARP_SERVICE_INTRO_HPP
#include <crypto/types.hpp>
2019-01-11 01:19:36 +00:00
#include <path/path_types.hpp>
#include <util/bencode.hpp>
2019-02-08 19:43:25 +00:00
#include <util/status.hpp>
2018-07-09 17:32:11 +00:00
#include <iostream>
namespace llarp
{
namespace service
{
2019-05-24 02:01:36 +00:00
struct Introduction
2018-07-09 17:32:11 +00:00
{
2019-04-19 15:10:26 +00:00
PubKey router;
PathID_t pathID;
llarp_time_t latency = 0s;
2020-02-24 19:40:45 +00:00
llarp_time_t expiresAt = 0s;
uint64_t version = LLARP_PROTO_VERSION;
2019-02-11 17:14:43 +00:00
util::StatusObject
2019-04-19 15:10:26 +00:00
ExtractStatus() const;
2019-02-08 19:43:25 +00:00
bool
IsExpired(llarp_time_t now) const
{
return now >= expiresAt;
}
bool
2020-02-24 19:40:45 +00:00
ExpiresSoon(llarp_time_t now, llarp_time_t dlt = 30s) const
{
return IsExpired(now + dlt);
}
2019-02-24 23:46:37 +00:00
std::ostream&
print(std::ostream& stream, int level, int spaces) const;
2018-07-09 17:32:11 +00:00
bool
2019-05-24 02:01:36 +00:00
BEncode(llarp_buffer_t* buf) const;
bool
BDecode(llarp_buffer_t* buf)
{
return bencode_decode_dict(*this, buf);
}
2018-07-09 17:32:11 +00:00
bool
2019-05-24 02:01:36 +00:00
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf);
2018-07-09 17:32:11 +00:00
2018-07-22 23:14:29 +00:00
void
Clear();
2018-07-09 17:32:11 +00:00
bool
operator<(const Introduction& other) const
{
return expiresAt < other.expiresAt || pathID < other.pathID || router < other.router
|| version < other.version || latency < other.latency;
2018-07-09 17:32:11 +00:00
}
2018-09-17 15:32:37 +00:00
bool
operator==(const Introduction& other) const
{
2018-09-27 17:51:42 +00:00
return pathID == other.pathID && router == other.router;
2018-09-17 15:32:37 +00:00
}
bool
operator!=(const Introduction& other) const
{
2018-10-08 21:23:45 +00:00
return pathID != other.pathID || router != other.router;
2018-09-17 15:32:37 +00:00
}
struct Hash
{
size_t
operator()(const Introduction& i) const
{
return PubKey::Hash()(i.router) ^ PathID_t::Hash()(i.pathID);
}
};
2018-07-09 17:32:11 +00:00
};
2019-02-24 23:46:37 +00:00
inline std::ostream&
operator<<(std::ostream& out, const Introduction& i)
{
return i.print(out, -1, -1);
}
2018-07-09 17:32:11 +00:00
} // namespace service
} // namespace llarp
#endif