Пример #1
0
        /// <summary>
        /// The UnicastReceiveCallback receives Http Responses
        /// and Fired the SatIpDeviceFound Event for adding the SatIpDevice
        /// </summary>
        /// <param name="ar"></param>
        private void UnicastReceiveCallback(IAsyncResult ar)
        {
            var udpClient = ((UdpState)(ar.AsyncState)).UdpClient;
            var endpoint  = ((UdpState)(ar.AsyncState)).EndPoint;

            if (udpClient.Client != null)
            {
                try
                {
                    var responseBytes = udpClient.EndReceive(ar, ref endpoint);
                    var args          = SsdpHelper.ParseSsdpResponse(responseBytes);

                    args.EndPoint = endpoint;

                    OnMessageReceived(args, false);

                    UnicastSetBeginReceive();
                }
                catch (ObjectDisposedException)
                {
                }
                catch (SocketException)
                {
                }
                catch (Exception)
                {
                    // If called while shutting down, seeing a NullReferenceException inside EndReceive
                }
            }
        }
Пример #2
0
        private void ReceiveCallback(IAsyncResult result)
        {
            if (_isDisposed)
            {
                return;
            }

            try
            {
                EndPoint endpoint      = new IPEndPoint(IPAddress.Any, SSDPPort);
                var      receivedCount = _socket.EndReceiveFrom(result, ref endpoint);
                var      received      = (byte[])result.AsyncState;

                var args = SsdpHelper.ParseSsdpResponse(received, (IPEndPoint)endpoint);

                if (_config.Configuration.DlnaOptions.EnableDebugLogging)
                {
                    var headerTexts = args.Headers.Select(i => string.Format("{0}={1}", i.Key, i.Value));
                    var headerText  = string.Join(",", headerTexts.ToArray());

                    _logger.Debug("{0} message received from {1}. Headers: {2}", args.Method, args.EndPoint, headerText);
                }

                OnMessageReceived(args);
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Failed to read SSDP message", ex);
            }

            if (_socket != null)
            {
                Receive();
            }
        }
Пример #3
0
        private void ReceiveCallback(IAsyncResult result)
        {
            if (_isDisposed)
            {
                return;
            }

            try
            {
                EndPoint endpoint = new IPEndPoint(IPAddress.Any, SSDPPort);

                var length = _socket.EndReceiveFrom(result, ref endpoint);

                var received = (byte[])result.AsyncState;

                var enableDebugLogging = _config.GetDlnaConfiguration().EnableDebugLogging;

                if (enableDebugLogging)
                {
                    _logger.Debug(Encoding.ASCII.GetString(received));
                }

                var args = SsdpHelper.ParseSsdpResponse(received);
                args.EndPoint = endpoint;

                if (IsSelfNotification(args))
                {
                    return;
                }

                if (enableDebugLogging)
                {
                    var headerTexts = args.Headers.Select(i => string.Format("{0}={1}", i.Key, i.Value));
                    var headerText  = string.Join(",", headerTexts.ToArray());

                    _logger.Debug("{0} message received from {1} on {3}. Headers: {2}", args.Method, args.EndPoint, headerText, _socket.LocalEndPoint);
                }

                OnMessageReceived(args);
            }
            catch (ObjectDisposedException)
            {
                if (!_isDisposed)
                {
                    //StartSocketRetryTimer();
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Failed to read SSDP message", ex);
            }

            if (_socket != null)
            {
                Receive();
            }
        }
Пример #4
0
        private void CreateListener(IPAddress localIp)
        {
            Task.Factory.StartNew(async(o) =>
            {
                try
                {
                    _logger.Info("Creating SSDP listener on {0}", localIp);

                    var endPoint = new IPEndPoint(localIp, 1900);

                    var socket = GetMulticastSocket(localIp, endPoint);

                    var receiveBuffer = new byte[64000];

                    CreateNotifier(localIp);

                    while (!_tokenSource.IsCancellationRequested)
                    {
                        var receivedBytes = await socket.ReceiveAsync(receiveBuffer, 0, 64000);

                        if (receivedBytes > 0)
                        {
                            var args           = SsdpHelper.ParseSsdpResponse(receiveBuffer);
                            args.EndPoint      = endPoint;
                            args.LocalEndPoint = new IPEndPoint(localIp, 0);

                            if (_ssdpHandler.IgnoreMessage(args, true))
                            {
                                return;
                            }

                            _ssdpHandler.LogMessageReceived(args, true);

                            TryCreateDevice(args);
                        }
                    }

                    _logger.Info("SSDP listener - Task completed");
                }
                catch (OperationCanceledException)
                {
                }
                catch (Exception e)
                {
                    _logger.ErrorException("Error in listener", e);
                }
            }, _tokenSource.Token, TaskCreationOptions.LongRunning);
        }
Пример #5
0
        /// <summary>
        /// The UnicastReceiveCallback receives Http Responses
        /// and Fired the SatIpDeviceFound Event for adding the SatIpDevice
        /// </summary>
        /// <param name="ar"></param>
        private void UnicastReceiveCallback(IAsyncResult ar)
        {
            var udpClient = ((UdpState)(ar.AsyncState)).UdpClient;
            var endpoint  = ((UdpState)(ar.AsyncState)).EndPoint;

            if (udpClient.Client != null)
            {
                var responseBytes = udpClient.EndReceive(ar, ref endpoint);
                var args          = SsdpHelper.ParseSsdpResponse(responseBytes);

                args.EndPoint = endpoint;

                OnMessageReceived(args);

                UnicastSetBeginReceive();
            }
        }
Пример #6
0
        private void ReceiveCallback(IAsyncResult result)
        {
            if (_isDisposed)
            {
                return;
            }

            try
            {
                EndPoint endpoint = new IPEndPoint(IPAddress.Any, SSDPPort);

                var length = _multicastSocket.EndReceiveFrom(result, ref endpoint);

                var received = (byte[])result.AsyncState;

                var enableDebugLogging = _config.GetDlnaConfiguration().EnableDebugLog;

                if (enableDebugLogging)
                {
                    _logger.Debug(Encoding.ASCII.GetString(received));
                }

                var args = SsdpHelper.ParseSsdpResponse(received);
                args.EndPoint = endpoint;

                OnMessageReceived(args, true);
            }
            catch (ObjectDisposedException)
            {
                if (!_isDisposed)
                {
                    //StartSocketRetryTimer();
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorException("Failed to read SSDP message", ex);
            }

            if (_multicastSocket != null)
            {
                Receive();
            }
        }
Пример #7
0
        private void CreateListener(IPAddress localIp, int networkInterfaceIndex)
        {
            Task.Factory.StartNew(async(o) =>
            {
                try
                {
                    var socket = GetMulticastSocket(networkInterfaceIndex);

                    var endPoint = new IPEndPoint(localIp, 1900);

                    socket.Bind(endPoint);

                    _logger.Info("Creating SSDP listener on {0}, network interface index {1}", localIp, networkInterfaceIndex);

                    var receiveBuffer = new byte[64000];

                    CreateNotifier(localIp);

                    while (!_tokenSource.IsCancellationRequested)
                    {
                        var receivedBytes = await socket.ReceiveAsync(receiveBuffer, 0, 64000);

                        if (receivedBytes > 0)
                        {
                            var args      = SsdpHelper.ParseSsdpResponse(receiveBuffer);
                            args.EndPoint = endPoint;
                            args.LocalIp  = localIp;

                            TryCreateDevice(args);
                        }
                    }

                    _logger.Info("SSDP listener - Task completed");
                }
                catch (OperationCanceledException)
                {
                }
                catch (Exception e)
                {
                    _logger.ErrorException("Error in listener", e);
                }
            }, _tokenSource.Token, TaskCreationOptions.LongRunning);
        }