Пример #1
0
NPT_UdpMulticastSocket* Server::getNewMulticastSocket()
{
    NPT_NetworkInterface* serverInterface = gMS->serverInterface();

    auto addresses = serverInterface->GetAddresses();
    if (!addresses.GetFirstItem())
    {
        throw new std::runtime_error("No usable addresses found for UPnP multicast");
    }

    NPT_SocketAddress localAddress((*addresses.GetFirstItem()).GetPrimaryAddress(), 0);

    NPT_UdpMulticastSocket* ssdpSocket = new NPT_UdpMulticastSocket();
    ssdpSocket->Bind(localAddress, true);
    ssdpSocket->SetTimeToLive(32);
    return ssdpSocket;
}
Пример #2
0
NPT_Result FrontEnd::start()
{
	WriteLocker locker(m_stateLock);
	if (m_state != State_Stopped) {
		return NPT_ERROR_INVALID_STATE;
	}

	NPT_Result nr;

	NPT_List<NPT_NetworkInterface*> ifList;
	nr = NPT_NetworkInterface::GetNetworkInterfaces(ifList);
	if (NPT_FAILED(nr)) {
		return nr;
	}

	for (NPT_Ordinal i = 0; i < ifList.GetItemCount(); i++) {
		NPT_NetworkInterface *nif = *ifList.GetItem(i);
		if (nif->GetAddresses().GetFirstItem() && (m_includeLoopback || ((nif->GetFlags() & NPT_NETWORK_INTERFACE_FLAG_LOOPBACK) == 0))) {
			Interface *intf = new Interface();
			intf->m_owner = this;
			intf->m_nif = nif;
			intf->m_context.m_ifAddr = nif->GetAddresses().GetFirstItem()->GetPrimaryAddress();
			intf->m_httpConnector = new HttpConnector(intf, intf->m_context.m_ifAddr);
			intf->m_ssdpConnector = new SsdpConnector(intf, intf->m_context.m_ifAddr);
			intf->m_context.m_httpPort = 0;
			intf->m_context.m_ssdpPort = 0;
			m_ifList.Add(intf);
			m_interfaceContextList.Add(&intf->m_context);
		} else {
			delete nif;
		}
	}

	if (m_ifList.GetItemCount() == 0) {
		return NPT_FAILURE;
	}

	for (NPT_Ordinal i = 0; i < m_ifList.GetItemCount(); i++) {
		Interface *intf = *m_ifList.GetItem(i);
		if (NPT_SUCCEEDED(intf->m_httpConnector->start())) {
			intf->m_context.m_httpPort = intf->m_httpConnector->port();
		}
		if (NPT_SUCCEEDED(intf->m_ssdpConnector->start())) {
			intf->m_context.m_ssdpPort = intf->m_ssdpConnector->port();
		}
	}

	m_taskGroup->reset();

	{
		ReadLocker locker1(m_cpLock);
		for (NPT_Ordinal i = 0; i < m_controlPointList.GetItemCount(); i++) {
			ControlPointInfo *info = *m_controlPointList.GetItem(i);
			info->m_controlPoint->implAttach(this, info->m_context);
		}

		for (NPT_Ordinal i = 0; i < m_deviceImplList.GetItemCount(); i++) {
			DeviceImplInfo *info = *m_deviceImplList.GetItem(i);
			info->m_deviceImpl->implAttach(this, info->m_context);
		}

		broadcastLocked(NULL, true);
	}

	m_taskGroup->startTask(new SsdpBroadcastTask(this));

	m_state = State_Running;
	return NPT_SUCCESS;
}