#include <conexus/conexus.h> #include <iostream> // This example watches a UDP object and calls a simple print function for any data received. // Prototype of the print callback function void callback_function(const Conexus::Data& data); int main() { // All applications must call this function which will initialize various aspects of the Conexus library. // This should generally be done early in main(), and must be done before any I/O is attempted or servers started. Conexus::init(); // We need to declare an endpoint Conexus::IPv4::UDP udp_endpoint; // The UDP protocol requires a port to be assigned to the endpoint, so we'll randomly pick port 1500 udp_endpoint.local_interface().set_port(1500); // Creating the slot and connecting to the endpoint is generally accomplished in a single line of code udp_endpoint.signal_data().connect( sigc::ptr_fun(&callback_function) ); // The Conexus::IP::UDP endpoint class inherits from the Conexus::Server class, so it supports a threaded server model that may be started udp_endpoint.start(); // We have started the endpoint running, and any callbacks will occur in a separate thread // Set up a loop that will run for 20 seconds and print the time every 5 // seconds. Since the server is threaded, the sleep(1) call will not effect // the servicing thread. std::cout << "Starting..." << std::endl; for (int i=1; i <= 20; i++) { if (i%5 == 0) std::cout << "Time: " << i << std::endl; sleep(1); } // Stop the server and prepare for shutdown udp_endpoint.stop(); return 0; } // And of course, we have to write the body of the function void callback_function( const Conexus::Data& datagram ) { std::cout << "Function received " << datagram.size() << " bytes of data" << std::endl; }
#include <conexus/conexus.h> #include <iostream> // This example watches a UDP object and calls a simple class method for any data received. // Declare the class that will have a callback method class ExampleClass { public: void callback_method(const Conexus::Data& data); }; int main() { // All applications must call this function which will initialize various aspects of the conexus library. // This should generally be done early in main(), and must be done before any I/O is attempted or servers started. Conexus::init(); // We can't connect to a class method unless we have an instance of the class ExampleClass class_with_callback; // And, of course we need to declare an endpoint Conexus::IPv4::UDP udp_endpoint; // The UDP protocol requires a port to be assigned to the endpoint, so we'll randomly pick port 1500 udp_endpoint.local_interface().set_port(1500); // Creating the slot and connecting to the endpoint is generally accomplished in a single line of code udp_endpoint.signal_data().connect( sigc::mem_fun(class_with_callback, &ExampleClass::callback_method) ); // The Conexus::IP::UDP endpoint class inherits from the Conexus::Server class, so it supports a threaded server model that may be started udp_endpoint.start(); // We have started the endpoint running, and any callbacks will occur in a separate thread // Set up a loop that will run for 20 seconds and print the time every 5 // seconds. Since the server is threaded, the sleep(1) call will not effect // the servicing thread. std::cout << "Starting..." << std::endl; for (int i=1; i <= 20; i++) { if (i%5 == 0) std::cout << "Time: " << i << std::endl; sleep(1); } // Stop the server and prepare for shutdown udp_endpoint.stop(); return 0; } // And of course, we have to write the body of the class method void ExampleClass::callback_method( const Conexus::Data& datagram ) { std::cout << "Class method received " << datagram.size() << " bytes of data" << std::endl; }
Notice that the main difference between the two examples is the use of the sigc++ library mechanisms for creating function slots using sigc::ptr_fun
and class member slots using sigc::mem_fun
.