private void OnSSDPMulticastReceive(IAsyncResult ar)
        {
            lock (_cpData.SyncObj)
                if (!_isActive)
                {
                    return;
                }
            UDPAsyncReceiveState <EndpointConfiguration> state = (UDPAsyncReceiveState <EndpointConfiguration>)ar.AsyncState;
            EndpointConfiguration config = state.Endpoint;
            Socket socket = config.SSDP_UDP_MulticastReceiveSocket;

            if (socket == null)
            {
                return;
            }
            try
            {
                EndPoint remoteEP = new IPEndPoint(state.Endpoint.AddressFamily == AddressFamily.InterNetwork ?
                                                   IPAddress.Any : IPAddress.IPv6Any, 0);
                using (Stream stream = new MemoryStream(state.Buffer, 0, socket.EndReceiveFrom(ar, ref remoteEP)))
                {
                    try
                    {
                        SimpleHTTPRequest header;
                        SimpleHTTPRequest.Parse(stream, out header);
                        HandleSSDPRequest(header, config, (IPEndPoint)remoteEP);
                    }
                    catch (Exception e)
                    {
                        UPnPConfiguration.LOGGER.Debug("SSDPClientController: Problem parsing incoming multicast UDP packet. Error message: '{0}'",
                                                       e.Message);
                    }
                }
                StartMulticastReceive(state);
            }
            catch (Exception) // SocketException, ObjectDisposedException
            {
                // Socket was closed - ignore this exception
                UPnPConfiguration.LOGGER.Info("SSDPClientController: Stopping listening for multicast messages at IP endpoint '{0}'",
                                              NetworkHelper.IPAddrToString(config.EndPointIPAddress));
            }
        }
Пример #2
0
        private void OnSSDPReceive(IAsyncResult ar)
        {
            lock (_serverData.SyncObj)
                if (!_serverData.IsActive)
                {
                    return;
                }
            UDPAsyncReceiveState <EndpointConfiguration> state = (UDPAsyncReceiveState <EndpointConfiguration>)ar.AsyncState;
            EndpointConfiguration config = state.Endpoint;
            Socket socket = state.Socket;

            try
            {
                EndPoint remoteEP = new IPEndPoint(
                    state.Endpoint.AddressFamily == AddressFamily.InterNetwork ?
                    IPAddress.Any : IPAddress.IPv6Any, 0);
                // To retrieve the remote endpoint address, it is necessary that the SocketOptionName.PacketInformation is set to true on the socket
                using (Stream stream = new MemoryStream(state.Buffer, 0, socket.EndReceiveFrom(ar, ref remoteEP)))
                {
                    try
                    {
                        SimpleHTTPRequest header;
                        SimpleHTTPRequest.Parse(stream, out header);
                        HandleSSDPRequest(header, config, (IPEndPoint)remoteEP);
                    }
                    catch (Exception e)
                    {
                        UPnPConfiguration.LOGGER.Debug(
                            "SSDPServerController: Problem parsing incoming packet at IP endpoint '{0}'. Error message: '{1}'", e,
                            NetworkHelper.IPAddrToString(config.EndPointIPAddress), e.Message);
                    }
                }
                StartReceive(state);
            }
            catch (Exception) // SocketException, ObjectDisposedException
            {
                // Socket was closed - ignore this exception
                UPnPConfiguration.LOGGER.Info("SSDPServerController: Stopping listening for multicast messages at IP endpoint '{0}'",
                                              NetworkHelper.IPAddrToString(config.EndPointIPAddress));
            }
        }
Пример #3
0
        private static void Parse(List <string> requestHeaders)
        {
            // Construct header using different line break styles
            IEnumerable <string> delimiters = new[] { "\r\n", "\n" };

            foreach (string delimiter in delimiters)
            {
                string fullRequest = string.Join(delimiter, requestHeaders.ToArray());

                using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(fullRequest)))
                {
                    SimpleHTTPRequest result;
                    SimpleHTTPRequest.Parse(stream, out result);
                    for (int index = 1; index < requestHeaders.Count - 2; index++)
                    {
                        string requestHeader = requestHeaders[index];
                        int    pos           = requestHeader.IndexOf(':');
                        string header        = requestHeader.Substring(0, pos).ToUpperInvariant();
                        string value         = requestHeader.Substring(pos + 1).Trim();
                        Assert.AreEqual(value, result[header]);
                    }
                }
            }
        }