C++ Distributed Hash Table
Classes | Public Types | Public Member Functions | List of all members
dht::net::NetworkEngine Class Referencefinal

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=-1, 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< NodeinsertNode (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)
 

Detailed Description

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:

Parameters
onErrorcallback for handling error messages.
onNewNodecallback for handling new nodes.
onReportedAddrcallback for reporting an our address as seen from the other peer.
onPingcallback for ping request.
onFindNodecallback for "find node" request.
onGetValuescallback for "get values" request.
onListencallback for "listen" request.
onAnnouncecallback for "announce" request.
onRefreshcallback for "refresh" request.

Definition at line 117 of file network_engine.h.

Member Function Documentation

◆ processMessage()

void dht::net::NetworkEngine::processMessage ( const uint8_t *  buf,
size_t  buflen,
const SockAddr addr 
)

Parses a message and calls appropriate callbacks.

Parameters
bufThe buffer containing the binary message.
buflenThe length of the buffer.
fromThe address info of the sender.
fromlenThe length of the corresponding sockaddr structure.
nowThe time to adjust the clock in the network engine.

◆ sendAnnounceValue()

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.

Parameters
nThe node.
hashThe target hash.
createdThe time when the value was created (avoiding extended value lifetime)
tokenA security token.
on_doneRequest callback when the request is completed.
on_expiredRequest callback when the request expires.
Returns
the request with information concerning its success.

◆ sendFindNode()

Sp<Request> dht::net::NetworkEngine::sendFindNode ( Sp< Node n,
const InfoHash hash,
want_t  want = -1,
RequestCb &&  on_done = {},
RequestExpiredCb &&  on_expired = {} 
)

Send a "find node" request to a given node.

Parameters
nThe node.
targetThe target hash.
wantIndicating wether IPv4 or IPv6 are wanted in response. Use NetworkEngine::want()
on_doneRequest callback when the request is completed.
on_expiredRequest callback when the request expires.
Returns
the request with information concerning its success.

◆ sendGetValues()

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.

Parameters
nThe node.
hashThe target hash.
queryThe query describing filters.
tokenA security token.
wantIndicating wether IPv4 or IPv6 are wanted in response. Use NetworkEngine::want()
on_doneRequest callback when the request is completed.
on_expiredRequest callback when the request expires.
Returns
the request with information concerning its success.

◆ sendListen()

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.

Parameters
nThe node.
hashThe storage's hash.
queryThe query describing filters.
tokenA security token.
previousThe previous request "listen" sent to this node.
socketUNUSED 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_doneRequest callback when the request is completed.
on_expiredRequest callback when the request expires.
socket_cbCallback to execute each time new updates arrive on the socket.
Returns
the request with information concerning its success.

◆ sendPing() [1/2]

Sp<Request> dht::net::NetworkEngine::sendPing ( Sp< Node n,
RequestCb &&  on_done,
RequestExpiredCb &&  on_expired 
)

Send a "ping" request to a given node.

Parameters
nThe node.
on_doneRequest callback when the request is completed.
on_expiredRequest callback when the request expires.
Returns
the request with information concerning its success.

◆ sendPing() [2/2]

Sp<Request> dht::net::NetworkEngine::sendPing ( const sockaddr *  sa,
socklen_t  salen,
RequestCb &&  on_done,
RequestExpiredCb &&  on_expired 
)
inline

Send a "ping" request to a given node.

Parameters
saThe node's ip sockaddr info.
salenThe associated sockaddr struct length.
on_doneRequest callback when the request is completed.
on_expiredRequest callback when the request expires.
Returns
the request with information concerning its success.

Definition at line 274 of file network_engine.h.

◆ sendRefreshValue()

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.

Parameters
nThe node.
hashThe target hash.
vidThe value id.
tokenA security token.
on_doneRequest callback when the request is completed.
on_expiredRequest callback when the request expires.
Returns
the request with information concerning its success.

◆ tellListener()

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.

Parameters
saThe address of the listenner.
sslenThe length of the sockaddr structure.
socket_idThe tid to use to write to the request socket.
hashThe hash key of the value.
wantWether to send ipv4 and/or ipv6 nodes.
ntokenListen security token.
nodesThe ipv4 closest nodes.
nodes6The ipv6 closest nodes.
valuesThe values to send.

The documentation for this class was generated from the following file: