/// <summary>
 /// Triggered when a SSDP search result is received
 /// </summary>
 private void UPnPControlPointSearchSink(IPEndPoint source, IPEndPoint local, Uri LocationURL, String USN, String SearchTarget, int MaxAge)
 {
     // A bit like getting a SSDP notification, but we don't do automatic
     // source change in this case. The only valid scenario of a search
     // result is device creation.
     lock (deviceTableLock)
     {
         if (deviceTable.ContainsKey(USN) == false)
         {
             // Never saw this device before
             DeviceInfo deviceInfo = new DeviceInfo();
             deviceInfo.Device     = null;
             deviceInfo.UDN        = USN;
             deviceInfo.NotifyTime = DateTime.Now;
             deviceInfo.BaseURL    = LocationURL;
             deviceInfo.MaxAge     = MaxAge;
             deviceInfo.LocalEP    = local;
             deviceTable[USN]      = deviceInfo;
             deviceFactory.CreateDevice(deviceInfo.BaseURL, deviceInfo.MaxAge, local.Address, USN);
         }
         else
         {
             DeviceInfo deviceInfo = (DeviceInfo)deviceTable[USN];
             if (deviceInfo.Device != null) // If the device is in creation mode, do nothing
             {
                 if (deviceInfo.BaseURL.Equals(LocationURL))
                 {
                     // Cancel a possible source change
                     deviceUpdateClock.Remove(deviceInfo);
                     deviceInfo.PendingBaseURL  = null;
                     deviceInfo.PendingMaxAge   = 0;
                     deviceInfo.PendingLocalEP  = null;
                     deviceInfo.PendingSourceEP = null;
                     // Then simply update the lifetime
                     deviceInfo.NotifyTime = DateTime.Now;
                     deviceTable[USN]      = deviceInfo;
                     deviceLifeTimeClock.Add(deviceInfo.UDN, MaxAge);
                 }
                 else
                 {
                     // Wow, same device, different source - Check timing
                     if (deviceInfo.NotifyTime.AddSeconds(10).Ticks < DateTime.Now.Ticks)
                     {
                         // This is a possible source change. Wait for 3 seconds and make the switch.
                         deviceInfo.PendingBaseURL  = LocationURL;
                         deviceInfo.PendingMaxAge   = MaxAge;
                         deviceInfo.PendingLocalEP  = local;
                         deviceInfo.PendingSourceEP = source;
                         deviceUpdateClock.Add(deviceInfo.UDN, 3);
                     }
                 }
             }
         }
     }
 }
        internal UPnPDevice UnprotectedRemoveMe(string UDN)
        {
            DeviceInfo deviceInfo;
            UPnPDevice removedDevice = null;

            if (deviceTable.ContainsKey(UDN))
            {
                deviceInfo    = (DeviceInfo)deviceTable[UDN];
                removedDevice = deviceInfo.Device;
                deviceTable.Remove(UDN);
                deviceLifeTimeClock.Remove(deviceInfo.UDN);
                deviceUpdateClock.Remove(deviceInfo);
                activeDeviceList.Remove(removedDevice);
            }

            return(removedDevice);
        }
Example #3
0
        private void ContinueRequest(IPEndPoint dest, string PQ, object Tag, HTTPMessage MSG)
        {
            HTTPMessage r;

            if (MSG == null)
            {
                r              = new HTTPMessage();
                r.Directive    = "GET";
                r.DirectiveObj = PQ;
                if (dest.AddressFamily == AddressFamily.InterNetwork)
                {
                    r.AddTag("Host", dest.ToString());
                }
                if (dest.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    r.AddTag("Host", "[" + RemoveIPv6Scope(dest.ToString()) + "]");
                }
            }
            else
            {
                r = MSG;
            }

            lock (TagQueue)
            {
                IdleTimeout = false;
                KeepAliveTimer.Remove(GetHashCode());

                if ((PIPELINE == false && _PIPELINE == false) || (_PIPELINE == false))
                {
                    HTTPRequest TR = new HTTPRequest();
                    TR.ProxySetting = ProxySetting;
                    TR._PIPELINE    = true;
                    if (OnSniff != null)
                    {
                        TR.OnSniff += NonPipelinedSniffSink;
                    }
                    if (OnSniffPacket != null)
                    {
                        TR.OnSniffPacket += NonPipelinedSniffPacketSink;
                    }
                    TR.OnResponse        += NonPipelinedResponseSink;
                    NotPipelinedTable[TR] = TR;
                    TR.PipelineRequest(dest, r, Tag);
                    return;
                }

                TagQueue.Enqueue(new StateData(r, dest, Tag, null));

                IPAddress localif = IPAddress.Any;
                if (dest.AddressFamily == AddressFamily.InterNetworkV6)
                {
                    localif = IPAddress.IPv6Any;
                }

                if (s == null)
                {
                    ReceivedFirstResponse = false;
                    if (ProxySetting != null)
                    {
                        s = new HTTPSession(new IPEndPoint(localif, 0),
                                            ProxySetting,
                                            CreateSink,
                                            CreateFailedSink,
                                            null);
                    }
                    else
                    {
                        s = new HTTPSession(new IPEndPoint(localif, 0),
                                            dest,
                                            CreateSink,
                                            CreateFailedSink,
                                            null);
                    }
                }
                else
                {
                    if (s.IsConnected && ReceivedFirstResponse)
                    {
                        try
                        {
                            if (ProxySetting == null)
                            {
                                s.Send(r);
                            }
                            else
                            {
                                HTTPMessage pr = (HTTPMessage)r.Clone();
                                pr.DirectiveObj = "http://" + dest + pr.DirectiveObj;
                                pr.Version      = "1.0";
                                s.Send(pr);
                            }
                        }
                        catch (Exception ex)
                        {
                            EventLogger.Log(ex);
                        }
                    }
                }
            }
        }
Example #4
0
 private void HandleHeader(HTTPSession sender, HTTPMessage Header, Stream StreamObject)
 {
     SessionTimer.Remove(sender);
     OnHeaderEvent.Fire(Header, sender);
 }