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);
}
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),
#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;
Xinema &xinema;
Msp::Net::StreamServerSocket *server_sock;
+ Msp::Net::DatagramSocket *discover_sock;
Msp::IO::EventDispatcher event_disp;
NetworkThread *thread;
std::list<Client *> clients;
private:
void connection_available();
+ void discover_request();
};
#endif