My Project 1.7.4
C++ Distributed Hash Table
|
An abstraction of communication protocol on the network. More...
#include <network_engine.h>
Public Types | |
using | RequestCb = std::function< void(const Request &, RequestAnswer &&)> |
using | RequestExpiredCb = std::function< void(const Request &, bool)> |
Public Member Functions | |
NetworkEngine (Logger &log, Scheduler &scheduler, const int &s=-1, const int &s6=-1) | |
NetworkEngine (InfoHash &myid, NetId net, const int &s, const int &s6, Logger &log, Scheduler &scheduler, decltype(NetworkEngine::onError) onError, decltype(NetworkEngine::onNewNode) onNewNode, decltype(NetworkEngine::onReportedAddr) onReportedAddr, decltype(NetworkEngine::onPing) onPing, decltype(NetworkEngine::onFindNode) onFindNode, decltype(NetworkEngine::onGetValues) onGetValues, decltype(NetworkEngine::onListen) onListen, decltype(NetworkEngine::onAnnounce) onAnnounce, decltype(NetworkEngine::onRefresh) onRefresh) | |
void | clear () |
void | tellListener (Sp< Node > n, Tid socket_id, const InfoHash &hash, want_t want, const Blob &ntoken, std::vector< Sp< Node > > &&nodes, std::vector< Sp< Node > > &&nodes6, std::vector< Sp< Value > > &&values, const Query &q) |
void | tellListenerRefreshed (Sp< Node > n, Tid socket_id, const InfoHash &hash, const Blob &ntoken, const std::vector< Value::Id > &values) |
void | tellListenerExpired (Sp< Node > n, Tid socket_id, const InfoHash &hash, const Blob &ntoken, const std::vector< Value::Id > &values) |
bool | isRunning (sa_family_t af) const |
want_t | want () const |
void | connectivityChanged (sa_family_t) |
Sp< Request > | sendPing (Sp< Node > n, RequestCb &&on_done, RequestExpiredCb &&on_expired) |
Sp< Request > | sendPing (const sockaddr *sa, socklen_t salen, RequestCb &&on_done, RequestExpiredCb &&on_expired) |
Sp< Request > | sendFindNode (Sp< Node > n, const InfoHash &hash, want_t want, RequestCb &&on_done, RequestExpiredCb &&on_expired) |
Sp< Request > | sendGetValues (Sp< Node > n, const InfoHash &hash, const Query &query, want_t want, RequestCb &&on_done, RequestExpiredCb &&on_expired) |
Sp< Request > | sendListen (Sp< Node > n, const InfoHash &hash, const Query &query, const Blob &token, Sp< Request > previous, RequestCb &&on_done, RequestExpiredCb &&on_expired, SocketCb &&socket_cb) |
Sp< Request > | sendAnnounceValue (Sp< Node > n, const InfoHash &hash, const Sp< Value > &v, time_point created, const Blob &token, RequestCb &&on_done, RequestExpiredCb &&on_expired) |
Sp< Request > | sendRefreshValue (Sp< Node > n, const InfoHash &hash, const Value::Id &vid, const Blob &token, RequestCb &&on_done, RequestExpiredCb &&on_expired) |
void | processMessage (const uint8_t *buf, size_t buflen, const SockAddr &addr) |
Sp< Node > | insertNode (const InfoHash &myid, const SockAddr &addr) |
std::vector< unsigned > | getNodeMessageStats (bool in) |
void | blacklistNode (const Sp< Node > &n) |
std::vector< Sp< Node > > | getCachedNodes (const InfoHash &id, sa_family_t sa_f, size_t count) |
An abstraction of communication protocol on the network.
The NetworkEngine processes all requests to nodes by offering a public interface for handling sending and receiving packets. The following parameters specify callbacks for DHT work:
onError | callback for handling error messages. |
onNewNode | callback for handling new nodes. |
onReportedAddr | callback for reporting an our address as seen from the other peer. |
onPing | callback for ping request. |
onFindNode | callback for "find node" request. |
onGetValues | callback for "get values" request. |
onListen | callback for "listen" request. |
onAnnounce | callback for "announce" request. |
onRefresh | callback for "refresh" request. |
Definition at line 117 of file network_engine.h.
using dht::net::NetworkEngine::RequestCb = std::function<void(const Request&, RequestAnswer&&)> |
Definition at line 204 of file network_engine.h.
using dht::net::NetworkEngine::RequestExpiredCb = std::function<void(const Request&, bool)> |
Definition at line 205 of file network_engine.h.
|
inline |
Definition at line 413 of file network_engine.h.
|
inline |
Definition at line 404 of file network_engine.h.
|
inline |
Definition at line 398 of file network_engine.h.
void dht::net::NetworkEngine::processMessage | ( | const uint8_t * | buf, |
size_t | buflen, | ||
const SockAddr & | addr | ||
) |
Parses a message and calls appropriate callbacks.
buf | The buffer containing the binary message. |
buflen | The length of the buffer. |
from | The address info of the sender. |
fromlen | The length of the corresponding sockaddr structure. |
now | The time to adjust the clock in the network engine. |
Sp< Request > dht::net::NetworkEngine::sendAnnounceValue | ( | Sp< Node > | n, |
const InfoHash & | hash, | ||
const Sp< Value > & | v, | ||
time_point | created, | ||
const Blob & | token, | ||
RequestCb && | on_done, | ||
RequestExpiredCb && | on_expired | ||
) |
Send a "announce" request to a given node.
n | The node. |
hash | The target hash. |
created | The time when the value was created (avoiding extended value lifetime) |
token | A security token. |
on_done | Request callback when the request is completed. |
on_expired | Request callback when the request expires. |
Sp< Request > dht::net::NetworkEngine::sendFindNode | ( | Sp< Node > | n, |
const InfoHash & | hash, | ||
want_t | want, | ||
RequestCb && | on_done, | ||
RequestExpiredCb && | on_expired | ||
) |
Send a "find node" request to a given node.
n | The node. |
target | The target hash. |
want | Indicating wether IPv4 or IPv6 are wanted in response. Use NetworkEngine::want() |
on_done | Request callback when the request is completed. |
on_expired | Request callback when the request expires. |
Sp< Request > dht::net::NetworkEngine::sendGetValues | ( | Sp< Node > | n, |
const InfoHash & | hash, | ||
const Query & | query, | ||
want_t | want, | ||
RequestCb && | on_done, | ||
RequestExpiredCb && | on_expired | ||
) |
Send a "get" request to a given node.
n | The node. |
hash | The target hash. |
query | The query describing filters. |
token | A security token. |
want | Indicating wether IPv4 or IPv6 are wanted in response. Use NetworkEngine::want() |
on_done | Request callback when the request is completed. |
on_expired | Request callback when the request expires. |
Sp< Request > dht::net::NetworkEngine::sendListen | ( | Sp< Node > | n, |
const InfoHash & | hash, | ||
const Query & | query, | ||
const Blob & | token, | ||
Sp< Request > | previous, | ||
RequestCb && | on_done, | ||
RequestExpiredCb && | on_expired, | ||
SocketCb && | socket_cb | ||
) |
Send a "listen" request to a given node.
n | The node. |
hash | The storage's hash. |
query | The query describing filters. |
token | A security token. |
previous | The previous request "listen" sent to this node. |
socket | UNUSED The socket for further response. For backward compatibility purpose, sendListen has to handle creation of the socket. Therefor, you cannot use openSocket yourself. TODO: Once we don't support the old "listen" negociation, sendListen shall not create the socket itself. |
on_done | Request callback when the request is completed. |
on_expired | Request callback when the request expires. |
socket_cb | Callback to execute each time new updates arrive on the socket. |
|
inline |
Send a "ping" request to a given node.
sa | The node's ip sockaddr info. |
salen | The associated sockaddr struct length. |
on_done | Request callback when the request is completed. |
on_expired | Request callback when the request expires. |
Definition at line 274 of file network_engine.h.
Sp< Request > dht::net::NetworkEngine::sendPing | ( | Sp< Node > | n, |
RequestCb && | on_done, | ||
RequestExpiredCb && | on_expired | ||
) |
Send a "ping" request to a given node.
n | The node. |
on_done | Request callback when the request is completed. |
on_expired | Request callback when the request expires. |
Sp< Request > dht::net::NetworkEngine::sendRefreshValue | ( | Sp< Node > | n, |
const InfoHash & | hash, | ||
const Value::Id & | vid, | ||
const Blob & | token, | ||
RequestCb && | on_done, | ||
RequestExpiredCb && | on_expired | ||
) |
Send a "refresh" request to a given node. Asks a node to keep the associated value Value.type.expiration more minutes in its storage.
n | The node. |
hash | The target hash. |
vid | The value id. |
token | A security token. |
on_done | Request callback when the request is completed. |
on_expired | Request callback when the request expires. |
void dht::net::NetworkEngine::tellListener | ( | Sp< Node > | n, |
Tid | socket_id, | ||
const InfoHash & | hash, | ||
want_t | want, | ||
const Blob & | ntoken, | ||
std::vector< Sp< Node > > && | nodes, | ||
std::vector< Sp< Node > > && | nodes6, | ||
std::vector< Sp< Value > > && | values, | ||
const Query & | q | ||
) |
Sends values (with closest nodes) to a listenner.
sa | The address of the listenner. |
sslen | The length of the sockaddr structure. |
socket_id | The tid to use to write to the request socket. |
hash | The hash key of the value. |
want | Wether to send ipv4 and/or ipv6 nodes. |
ntoken | Listen security token. |
nodes | The ipv4 closest nodes. |
nodes6 | The ipv6 closest nodes. |
values | The values to send. |
|
inline |
Definition at line 244 of file network_engine.h.