<commit_before>using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Windows.Networking;
using Windows.Networking.Connectivity;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
using Windows.Web.Http;

namespace Kazyx.DeviceDiscovery
{
    public class SsdpDiscovery
    {
        private const string MULTICAST_ADDRESS = "172.16.17.32";

        private readonly HostName MULTICAST_HOST = new HostName(MULTICAST_ADDRESS);

        private const int SSDP_PORT = 1900;
        private const int RESULT_BUFFER = 8192;

        private const int SSDP_RES_MAX_READ_LINES = 20;

        public const string ST_ALL = "ssdp:all";

        private uint _MX = 1;
        public uint MX
        {
            set { _MX = value; }
            get { return _MX; }
        }

        private readonly TimeSpan DEFAULT_TIMEOUT = new TimeSpan(0, 0, 5);

        public delegate void SonyCameraDeviceHandler(object sender, SonyCameraDeviceEventArgs e);

        public event SonyCameraDeviceHandler SonyCameraDeviceDiscovered;

        public delegate void DeviceDescriptionHandler(object sender, DeviceDescriptionEventArgs e);

        public event DeviceDescriptionHandler DescriptionObtained;

        public event EventHandler Finished;

        private async void Search(string st, TimeSpan? timeout = null)
        {
            Log("Search - " + st);

            var ssdp_data = new StringBuilder()
                .Append("M-SEARCH * HTTP/1.1").Append("\r\n")
                .Append("HOST: ").Append(MULTICAST_ADDRESS).Append(":").Append(SSDP_PORT.ToString()).Append("\r\n")
                .Append("MAN: ").Append("\"ssdp:discover\"").Append("\r\n")
                .Append("MX: ").Append(MX.ToString()).Append("\r\n")
                .Append("ST: ").Append(st).Append("\r\n")
                .Append("\r\n")
                .ToString();

            var adapters = TargetNetworkAdapters ?? await GetActiveAdaptersAsync().ConfigureAwait(false);

            await Task.WhenAll(adapters.Select(async adapter =>
            {
                using (var socket = new DatagramSocket())
                {
                    socket.Control.DontFragment = true;
                    socket.MessageReceived += OnDatagramSocketMessageReceived;

                    try
                    {
                        await socket.BindServiceNameAsync("", adapter);

                        using (var output = await socket.GetOutputStreamAsync(MULTICAST_HOST, SSDP_PORT.ToString()))
                        {
                            using (var writer = new DataWriter(output))
                            {
                                writer.WriteString(ssdp_data);
                                await writer.StoreAsync();
                            }
                        }

                        await Task.Delay(timeout ?? DEFAULT_TIMEOUT).ConfigureAwait(false);
                        Log("Search Timeout");
                        await socket.CancelIOAsync();
                    }
                    catch (Exception e)
                    {
                        Log("Failed to send multicast: " + e.StackTrace);
                    }

                    socket.MessageReceived -= OnDatagramSocketMessageReceived;
                }
            })).ConfigureAwait(false);

            Finished?.Invoke(this, new EventArgs());
        }

        private void OnDatagramSocketMessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
        {
            Log("Datagram message received");
            if (args == null)
            {
                return;
            }
            string data;
            using (var reader = args.GetDataReader())
            {
                data = reader.ReadString(reader.UnconsumedBufferLength);
            }
            Log(data);
            Task task = GetDeviceDescriptionAsync(data, args.LocalAddress);
        }

        public IList<NetworkAdapter> TargetNetworkAdapters
        {
            set; get;
        }

        public static Task<IList<NetworkAdapter>> GetActiveAdaptersAsync()
        {
            var tcs = new TaskCompletionSource<IList<NetworkAdapter>>();

            Task.Run(() =>
            {
                var profiles = NetworkInformation.GetConnectionProfiles();
                var list = new List<NetworkAdapter>();
                foreach (var profile in profiles)
                {
                    if (profile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.None)
                    {
                        // Historical profiles.
                        // Log("ConnectivityLevel None: " + profile.ProfileName);
                        continue;
                    }

                    var adapter = profile.NetworkAdapter;

                    switch (adapter.IanaInterfaceType)
                    {
                        case 6: // Ethernet
                        case 71: // 802.11
                            break;
                        default:
                            // Log("Type mismatch: " + profile.ProfileName);
                            continue;
                    }

                    if (!list.Contains(adapter))
                    {
                        Log("Active Adapter: " + profile.ProfileName);
                        list.Add(adapter);
                    }
                }
<commit_msg>Change type of network adapter collection from List to HashSet<commit_after>using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using Windows.Networking;
using Windows.Networking.Connectivity;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
using Windows.Web.Http;

namespace Kazyx.DeviceDiscovery
{
    public class SsdpDiscovery
    {
        private const string MULTICAST_ADDRESS = "172.16.17.32";

        private readonly HostName MULTICAST_HOST = new HostName(MULTICAST_ADDRESS);

        private const int SSDP_PORT = 1900;
        private const int RESULT_BUFFER = 8192;

        private const int SSDP_RES_MAX_READ_LINES = 20;

        public const string ST_ALL = "ssdp:all";

        private uint _MX = 1;
        public uint MX
        {
            set { _MX = value; }
            get { return _MX; }
        }

        private readonly TimeSpan DEFAULT_TIMEOUT = new TimeSpan(0, 0, 5);

        public delegate void SonyCameraDeviceHandler(object sender, SonyCameraDeviceEventArgs e);

        public event SonyCameraDeviceHandler SonyCameraDeviceDiscovered;

        public delegate void DeviceDescriptionHandler(object sender, DeviceDescriptionEventArgs e);

        public event DeviceDescriptionHandler DescriptionObtained;

        public event EventHandler Finished;

        private async void Search(string st, TimeSpan? timeout = null)
        {
            Log("Search - " + st);

            var ssdp_data = new StringBuilder()
                .Append("M-SEARCH * HTTP/1.1").Append("\r\n")
                .Append("HOST: ").Append(MULTICAST_ADDRESS).Append(":").Append(SSDP_PORT.ToString()).Append("\r\n")
                .Append("MAN: ").Append("\"ssdp:discover\"").Append("\r\n")
                .Append("MX: ").Append(MX.ToString()).Append("\r\n")
                .Append("ST: ").Append(st).Append("\r\n")
                .Append("\r\n")
                .ToString();

            var adapters = TargetNetworkAdapters ?? await GetActiveAdaptersAsync().ConfigureAwait(false);

            await Task.WhenAll(adapters.Select(async adapter =>
            {
                using (var socket = new DatagramSocket())
                {
                    socket.Control.DontFragment = true;
                    socket.MessageReceived += OnDatagramSocketMessageReceived;

                    try
                    {
                        await socket.BindServiceNameAsync("", adapter);

                        using (var output = await socket.GetOutputStreamAsync(MULTICAST_HOST, SSDP_PORT.ToString()))
                        {
                            using (var writer = new DataWriter(output))
                            {
                                writer.WriteString(ssdp_data);
                                await writer.StoreAsync();
                            }
                        }

                        await Task.Delay(timeout ?? DEFAULT_TIMEOUT).ConfigureAwait(false);
                        Log("Search Timeout");
                        await socket.CancelIOAsync();
                    }
                    catch (Exception e)
                    {
                        Log("Failed to send multicast: " + e.StackTrace);
                    }

                    socket.MessageReceived -= OnDatagramSocketMessageReceived;
                }
            })).ConfigureAwait(false);

            Finished?.Invoke(this, new EventArgs());
        }

        private void OnDatagramSocketMessageReceived(DatagramSocket sender, DatagramSocketMessageReceivedEventArgs args)
        {
            Log("Datagram message received");
            if (args == null)
            {
                return;
            }
            string data;
            using (var reader = args.GetDataReader())
            {
                data = reader.ReadString(reader.UnconsumedBufferLength);
            }
            Log(data);
            Task task = GetDeviceDescriptionAsync(data, args.LocalAddress);
        }

        public HashSet<NetworkAdapter> TargetNetworkAdapters
        {
            set; get;
        }

        public static Task<HashSet<NetworkAdapter>> GetActiveAdaptersAsync()
        {
            var tcs = new TaskCompletionSource<HashSet<NetworkAdapter>>();

            Task.Run(() =>
            {
                var profiles = NetworkInformation.GetConnectionProfiles();
                var list = new HashSet<NetworkAdapter>();
                foreach (var profile in profiles)
                {
                    if (profile.GetNetworkConnectivityLevel() == NetworkConnectivityLevel.None)
                    {
                        // Historical profiles.
                        // Log("ConnectivityLevel None: " + profile.ProfileName);
                        continue;
                    }

                    var adapter = profile.NetworkAdapter;

                    switch (adapter.IanaInterfaceType)
                    {
                        case 6: // Ethernet
                        case 71: // 802.11
                            break;
                        default:
                            // Log("Type mismatch: " + profile.ProfileName);
                            continue;
                    }

                    if (!list.Contains(adapter))
                    {
                        Log("Active Adapter: " + profile.ProfileName);
                        list.Add(adapter);
                    }
                }

{'fraction_non_alphanumeric': 0.08832011535688536, 'fraction_numerical': 0.005948089401586157, 'mean_word_length': 1.444273127753304, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4709295', 'n_tokens_mistral': 2724, 'n_tokens_neox': 2497, 'n_words': 559}