Skip to content

Latest commit

 

History

History
62 lines (55 loc) · 1.85 KB

File metadata and controls

62 lines (55 loc) · 1.85 KB

TUD

Client - Server discovery

The Tablo Udp Discovery module can be used to easely implement UDP discovery into your project.

To use TUD in your project you need to include it first. After including TUD to your project you can implement the server like this:

#include "server_discovery.h"
auto serverDiscovery = std::make_shared<tud::ServerDiscovery>(interface, 4000, 4001, "yourNewIdenifier");
std::thread serverDiscoveryThread([serverDiscovery]() {
  serverDiscovery->discoveryCycle();
});

And the client like this:

#include "client_discovery.h"
auto clientDiscovery = std::make_shared<tud::ClientDiscovery>(interface, 4000, 4001, "yourNewIdenifier");
std::thread clientDiscoveryThread([clientDiscovery]() {
  clientDiscovery->discoveryCycle();
});

Both constructors want:

  1. your internet interface (string)
  2. your in port (int)
  3. your out port (int)
  4. your identifier (optional string)

Then you can use it with these functions:

std::vector<std::string> getDiscoveredAddresses();
void removeDiscoveredAddress(std::string address);

std::string getLocalIpAddress(std::string interface);
std::string getBroadcastIpAddress();
bool isValidIpV4(std::string &ipString);

Peer discovery

If you have multiple peers that need to discover each other via broadcast, you can use peer mode as follows:

#include "peer_discovery.h"
auto peerDiscovery = std::make_shared<tud::PeerDiscovery>(INTERFACE, PORT);
std::thread peerDiscoveryThread([peerDiscovery]() {
  peerDiscovery->discoveryCycle();
});

After setting up peer discovery, you can use it via the following API functions:

std::vector<std::string> getDiscoveredAddresses();
std::vector<std::string> getDiscoveredIdentifiers();
std::map<std::string, std::string> getDiscoveredPeers();
      
void removeDiscoveredAddress(std::string address);