Skip to content

Commit

Permalink
Merge pull request #9 from fcouceiro/master
Browse files Browse the repository at this point in the history
Updates callbacks to std::function allowing c++ classes method binding.
Change UDP socket behavior to start in constructor and stop in destructor.
  • Loading branch information
mrdunk committed Nov 18, 2017
2 parents 0214e00 + 205838a commit b9a8205
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
15 changes: 9 additions & 6 deletions mdns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ void PrintHex(const unsigned char data) {
Serial.print(" ");
}

void MDns::startUdpMulticast(){
Serial.println("Initializing Multicast.");
Udp.beginMulticast(WiFi.localIP(), IPAddress(224, 0, 0, 251), MDNS_TARGET_PORT);
}

bool MDns::loop() {
if (!init) {
init = true;
Serial.println("Initializing Multicast.");
Udp.beginMulticast(WiFi.localIP(), IPAddress(224, 0, 0, 251), MDNS_TARGET_PORT);
}
data_size = Udp.parsePacket();
if ( data_size > 12) {
if(data_size > largest_packet_seen){
Expand All @@ -38,7 +38,6 @@ bool MDns::loop() {
// We've received a packet which is long enough to contain useful data so
// read the data from it.
Udp.read(data_buffer, data_size); // read the packet into the buffer

// data_buffer[0] and data_buffer[1] contain the Query ID field which is unused in mDNS.

// data_buffer[2] and data_buffer[3] are DNS flags which are mostly unused in mDNS.
Expand Down Expand Up @@ -512,6 +511,10 @@ void MDns::PopulateAnswerResult(Answer* answer) {
}
}

MDns::~MDns(){
Udp.stop();
};

bool writeToBuffer(const byte value, char* p_name_buffer, int* p_name_buffer_pos,
const int name_buffer_len) {
if (*p_name_buffer_pos < name_buffer_len - 1) {
Expand Down
40 changes: 23 additions & 17 deletions mdns.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ class MDns {
// p_packet_function : Callback fires for every mDNS packet that arrives.
// p_query_function : Callback fires for every mDNS Query that arrives as part of a packet.
// p_answer_function : Callback fires for every mDNS Answer that arrives as part of a packet.
MDns(void (*p_packet_function)(const MDns*),
void (*p_query_function)(const Query*),
void (*p_answer_function)(const Answer*)) :
MDns(std::function<void(const MDns*)> p_packet_function,
std::function<void(const Query*)> p_query_function,
std::function<void(const Answer*)> p_answer_function) :
MDns(p_packet_function, p_query_function, p_answer_function, MAX_PACKET_SIZE) { }

// Constructor takes callbacks which fire when mDNS data arrives.
Expand All @@ -89,9 +89,9 @@ class MDns {
// p_query_function : Callback fires for every mDNS Query that arrives as part of a packet.
// p_answer_function : Callback fires for every mDNS Answer that arrives as part of a packet.
// max_packet_size_ : Set the data_buffer size allocated to store incoming packets.
MDns(void (*p_packet_function)(const MDns*),
void (*p_query_function)(const Query*),
void (*p_answer_function)(const Answer*),
MDns(std::function<void(const MDns*)> p_packet_function,
std::function<void(const Query*)> p_query_function,
std::function<void(const Answer*)> p_answer_function,
int max_packet_size_) :
#ifdef DEBUG_STATISTICS
buffer_size_fail(0),
Expand All @@ -104,7 +104,9 @@ class MDns {
buffer_pointer(0),
data_buffer(new byte[max_packet_size_]),
max_packet_size(max_packet_size_)
{ };
{
this->startUdpMulticast();
};

// Constructor can be passed the buffer to hold the mDNS data.
// This way the potentially large buffer can be shared with other processes.
Expand All @@ -113,9 +115,9 @@ class MDns {
// p_query_function : Callback fires for every mDNS Query that arrives as part of a packet.
// p_answer_function : Callback fires for every mDNS Answer that arrives as part of a packet.
// max_packet_size_ : Set the data_buffer size allocated to store incoming packets.
MDns(void (*p_packet_function)(const MDns*),
void (*p_query_function)(const Query*),
void (*p_answer_function)(const Answer*),
MDns(std::function<void(const MDns*)> p_packet_function,
std::function<void(const Query*)> p_query_function,
std::function<void(const Answer*)> p_answer_function,
byte* data_buffer_,
int max_packet_size_) :
#ifdef DEBUG_STATISTICS
Expand All @@ -129,7 +131,11 @@ class MDns {
buffer_pointer(0),
data_buffer(data_buffer_),
max_packet_size(max_packet_size_)
{ };
{
this->startUdpMulticast();
};

~MDns();

// Call this regularly to check for an incoming packet.
bool loop();
Expand Down Expand Up @@ -171,22 +177,22 @@ class MDns {
unsigned int packet_count;
#endif
private:
// Initializes udp multicast
void startUdpMulticast();

void Parse_Query(Query& query);
void Parse_Answer(Answer& answer);
unsigned int PopulateName(const char* name_buffer);
void PopulateAnswerResult(Answer* answer);

// Whether UDP socket has not yet been initialised.
bool init;

// Pointer to function that gets called for every incoming mDNS packet.
void (*p_packet_function_)(const MDns*);
std::function<void(const MDns*)> p_packet_function_;

// Pointer to function that gets called for every incoming query.
void (*p_query_function_)(const Query*);
std::function<void(const Query*)> p_query_function_;

// Pointer to function that gets called for every incoming answer.
void (*p_answer_function_)(const Answer*);
std::function<void(const Answer*)> p_answer_function_;

// Position in data_buffer while processing packet.
unsigned int buffer_pointer;
Expand Down

0 comments on commit b9a8205

Please sign in to comment.