]> git.tdb.fi Git - xinema.git/commitdiff
Use a datagram socket for automatic service discovery
authorMikko Rasa <tdb@tdb.fi>
Wed, 14 Oct 2015 23:49:29 +0000 (02:49 +0300)
committerMikko Rasa <tdb@tdb.fi>
Wed, 14 Oct 2015 23:49:29 +0000 (02:49 +0300)
source/networkinterface.cpp
source/networkinterface.h

index 74c80dc6263aa9a17963f26ffa3e4da2a4bb09dc..1bf7b4a8e159bfc004d951e48e8dd2a02f9fa7df 100644 (file)
@@ -6,17 +6,23 @@
 using namespace std;
 using namespace Msp;
 
+#include <msp/io/print.h>
+
 NetworkInterface::NetworkInterface(Xinema &x):
        xinema(x)
 {
        Net::SockAddr *addr = Net::resolve("::", "34588", Net::INET6);
        server_sock = new Net::StreamServerSocket(addr->get_family());
        server_sock->listen(*addr);
+       discover_sock = new Net::DatagramSocket(addr->get_family());
+       discover_sock->bind(*addr);
        delete addr;
 
        server_sock->signal_data_available.connect(sigc::mem_fun(this, &NetworkInterface::connection_available));
+       discover_sock->signal_data_available.connect(sigc::mem_fun(this, &NetworkInterface::discover_request));
 
        event_disp.add(*server_sock);
+       event_disp.add(*discover_sock);
 
        thread = new NetworkThread(*this);
 }
@@ -35,6 +41,16 @@ void NetworkInterface::connection_available()
        clients.push_back(new Client(xinema, sock));
 }
 
+void NetworkInterface::discover_request()
+{
+       Net::SockAddr *peer_addr;
+       char rbuf[1024];
+       discover_sock->recvfrom(rbuf, sizeof(rbuf), peer_addr);
+       IO::print("Discovery beacon received from %s\n", peer_addr->str());
+       discover_sock->sendto("xinema", 6, *peer_addr);
+       delete peer_addr;
+}
+
 
 NetworkInterface::NetworkThread::NetworkThread(NetworkInterface &n):
        network(n),
index fbb66b9fa8fb2e710bccf3484cdecebe1e34e241..9814f6a16286bb67bbb30328453c00faf1e03a96 100644 (file)
@@ -4,6 +4,7 @@
 #include <msp/core/thread.h>
 #include <msp/io/eventdispatcher.h>
 #include <msp/io/pipe.h>
+#include <msp/net/datagramsocket.h>
 #include <msp/net/streamserversocket.h>
 
 class Client;
@@ -30,6 +31,7 @@ private:
 
        Xinema &xinema;
        Msp::Net::StreamServerSocket *server_sock;
+       Msp::Net::DatagramSocket *discover_sock;
        Msp::IO::EventDispatcher event_disp;
        NetworkThread *thread;
        std::list<Client *> clients;
@@ -40,6 +42,7 @@ public:
 
 private:
        void connection_available();
+       void discover_request();
 };
 
 #endif