/*---------------------------------------------------------------------- | PLT_UPnP::Start() +---------------------------------------------------------------------*/ NPT_Result PLT_UPnP::Start() { NPT_LOG_INFO("Starting UPnP..."); NPT_AutoLock lock(m_Lock); if (m_Started == true) NPT_CHECK_SEVERE(NPT_ERROR_INVALID_STATE); NPT_List<NPT_IpAddress> ips; PLT_UPnPMessageHelper::GetIPAddresses(ips); /* Create multicast socket and bind on 1900. If other apps didn't play nicely by setting the REUSE_ADDR flag, this could fail */ NPT_UdpMulticastSocket* socket = new NPT_UdpMulticastSocket(); NPT_CHECK_SEVERE(socket->Bind(NPT_SocketAddress(NPT_IpAddress::Any, 1900), true)); /* Join multicast group for every ip we found */ NPT_CHECK_SEVERE(ips.ApplyUntil(PLT_SsdpInitMulticastIterator(socket), NPT_UntilResultNotEquals(NPT_SUCCESS))); /* create the ssdp listener */ m_SsdpListenTask = new PLT_SsdpListenTask(socket); NPT_CHECK_SEVERE(m_TaskManager.StartTask(m_SsdpListenTask)); /* start devices & ctrlpoints */ // TODO: Starting devices and ctrlpoints could fail? m_CtrlPoints.Apply(PLT_UPnP_CtrlPointStartIterator(m_SsdpListenTask)); m_Devices.Apply(PLT_UPnP_DeviceStartIterator(m_SsdpListenTask)); m_Started = true; return NPT_SUCCESS; }
/*---------------------------------------------------------------------- | PLT_SsdpListenTask::DoInit +---------------------------------------------------------------------*/ void PLT_SsdpListenTask::DoInit() { if (m_Multicast) { if (m_JoinHard) { NPT_List<NPT_IpAddress> ips; PLT_UPnPMessageHelper::GetIPAddresses(ips); /* Join multicast group for every ip we found */ ips.Apply(PLT_SsdpInitMulticastIterator((NPT_UdpMulticastSocket*)m_Socket)); } else { NPT_IpAddress addr; addr.ResolveName("239.255.255.250"); ((NPT_UdpMulticastSocket*)m_Socket)->JoinGroup(addr, NPT_IpAddress::Any); } } }
/*---------------------------------------------------------------------- | PLT_SsdpListenTask::DoInit +---------------------------------------------------------------------*/ void PLT_SsdpListenTask::DoInit() { if (m_IsMulticast) { NPT_List<NPT_NetworkInterface*> if_list; NPT_CHECK_LABEL_FATAL(PLT_UPnPMessageHelper::GetNetworkInterfaces(if_list), done); /* Join multicast group for every interface we found */ if_list.ApplyUntil( PLT_SsdpInitMulticastIterator((NPT_UdpMulticastSocket*)m_Socket), NPT_UntilResultNotEquals(NPT_SUCCESS)); if_list.Apply(NPT_ObjectDeleter<NPT_NetworkInterface>()); } done: return; }