MMulticastSocketDevice::MMulticastSocketDevice( QString sAddress, quint16 nPort, u_char ttl) : MSocketDevice(MSocketDevice::Datagram), m_address(sAddress), m_port(nPort) { #if 0 ttl = UPnp::GetConfiguration()->GetValue( "UPnP/TTL", 4 ); #endif if (ttl == 0) ttl = 4; setProtocol(IPv4); setSocket(createNewSocket(), MSocketDevice::Datagram); m_imr.imr_multiaddr.s_addr = inet_addr(sAddress.toLatin1().constData()); m_imr.imr_interface.s_addr = htonl(INADDR_ANY); if (setsockopt(socket(), IPPROTO_IP, IP_ADD_MEMBERSHIP, (const char *)&m_imr, sizeof( m_imr )) < 0) { LOG(VB_GENERAL, LOG_ERR, LOC + "setsockopt - IP_ADD_MEMBERSHIP " + ENO); } if (setsockopt(socket(), IPPROTO_IP, IP_MULTICAST_TTL, (const char *)&ttl, sizeof(ttl)) < 0) { LOG(VB_GENERAL, LOG_ERR, LOC + "setsockopt - IP_MULTICAST_TTL " + ENO); } setAddressReusable(true); if (!bind(m_address, m_port)) LOG(VB_GENERAL, LOG_ERR, LOC + "bind failed"); }
QMulticastSocket::QMulticastSocket( QString sAddress, quint16 nPort, quint8 ttl ) : MSocketDevice( MSocketDevice::Datagram ) { m_address.setAddress( sAddress ); m_port = nPort; // ttl = UPnp::GetConfiguration()_pConfig->GetValue( "UPnP/TTL", 4 ); if (ttl == 0) ttl = 4; // Force to IPv4 until a proper upnp spec is complete for ipv6 as well. setProtocol(IPv4); setSocket(createNewSocket(), MSocketDevice::Datagram); // ---------------------------------------------------------------------- // Set the numer of subnets to traverse (TTL) // ---------------------------------------------------------------------- setsockopt( socket(), IPPROTO_IP, IP_MULTICAST_TTL, (char *)(&ttl), sizeof(ttl) ); setAddressReusable( true ); // ---------------------------------------------------------------------- // bind to the local address/port (win32 requires QHostAddress::Any be used // ---------------------------------------------------------------------- if (bind( QHostAddress::Any, m_port ) == false) VERBOSE(VB_IMPORTANT, QString( "QMulticastSocket: bind failed (%1)" ).arg( GET_SOCKET_ERROR )); // ---------------------------------------------------------------------- // Now join the Multicast group // ---------------------------------------------------------------------- QByteArray addr = sAddress.toLatin1(); struct ip_mreq imr; imr.imr_multiaddr.s_addr = inet_addr( addr.constData() ); imr.imr_interface.s_addr = htonl(INADDR_ANY); if ( setsockopt( socket(), IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)(&imr), sizeof( imr )) != 0) VERBOSE(VB_IMPORTANT, QString( "QMulticastSocket: setsockopt - IP_ADD_MEMBERSHIP Error (%1)" ).arg( GET_SOCKET_ERROR )); }
/** * \brief connect to host * \return true on success */ bool MythSocket::connect(const QHostAddress &addr, quint16 port) { if (state() == Connected) { VERBOSE(VB_SOCKET, LOC + "connect() called with already open socket, closing"); close(); } VERBOSE(VB_SOCKET, LOC + QString("attempting connect() to (%1:%2)") .arg(addr.toString()).arg(port)); if (!MSocketDevice::connect(addr, port)) { VERBOSE(VB_SOCKET, LOC + QString("connect() failed (%1)") .arg(errorToString())); setState(Idle); return false; } setReceiveBufferSize(kSocketBufferSize); setAddressReusable(true); setKeepalive(true); if (state() == Connecting) { setState(Connected); if (m_cb) { VERBOSE(VB_SOCKET, LOC + "calling m_cb->connected()"); m_cb->connected(this); s_readyread_thread->WakeReadyReadThread(); } } else { setState(Connected); } return true; }