<commit_before>﻿// Copyright 2021 <NAME>. All rights reserved.
// Use of this source code is governed by the Apache License 2.0,
// as found in the LICENSE.txt file.

using OscCore;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;

namespace OscMixerControl
{
    public sealed class UdpOscClient : IOscClient
    {
        private readonly UdpClient client;
        private readonly CancellationTokenSource cts;

        public event EventHandler<OscPacket> PacketReceived;

        public UdpOscClient(string hostName, int port)
        {
            client = new UdpClient();
            client.Connect(hostName, port);
            cts = new CancellationTokenSource();
            StartReceiving();
        }

        public Task SendAsync(OscPacket packet)
        {
            var data = packet.ToByteArray();
            return client.SendAsync(data, data.Length);
        }

        public Task SendAsync(OscPacket packet, IPEndPoint endPoint)
        {
            var data = packet.ToByteArray();
            return client.SendAsync(data, data.Length, endPoint);
        }

        private async void StartReceiving()
        {
            while (!cts.IsCancellationRequested)
            {
                var result = await client.ReceiveAsync();
                var buffer = result.Buffer;
                var packet = OscPacket.Read(buffer, 0, buffer.Length);
                // TODO: Maybe do this asynchronously...
                PacketReceived?.Invoke(this, packet);
            }
        }

        public void Dispose() => cts.Cancel();
    }
}<commit_msg>Fix failure to dispose of the UdpClient for mixers<commit_after>﻿// Copyright 2021 <NAME>. All rights reserved.
// Use of this source code is governed by the Apache License 2.0,
// as found in the LICENSE.txt file.

using OscCore;
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Threading.Tasks;

namespace OscMixerControl
{
    public sealed class UdpOscClient : IOscClient
    {
        private readonly UdpClient client;
        private readonly CancellationTokenSource cts;

        public event EventHandler<OscPacket> PacketReceived;

        public UdpOscClient(string hostName, int port)
        {
            client = new UdpClient();
            client.Connect(hostName, port);
            cts = new CancellationTokenSource();
            StartReceiving();
        }

        public Task SendAsync(OscPacket packet)
        {
            var data = packet.ToByteArray();
            return client.SendAsync(data, data.Length);
        }

        public Task SendAsync(OscPacket packet, IPEndPoint endPoint)
        {
            var data = packet.ToByteArray();
            return client.SendAsync(data, data.Length, endPoint);
        }

        private async void StartReceiving()
        {
            while (!cts.IsCancellationRequested)
            {
                var result = await client.ReceiveAsync();
                var buffer = result.Buffer;
                var packet = OscPacket.Read(buffer, 0, buffer.Length);
                // TODO: Maybe do this asynchronously...
                PacketReceived?.Invoke(this, packet);
            }
        }

        public void Dispose()
        {
            cts.Cancel();
            try
            {
                client.Dispose();
            }
            catch
            {
                // If disposing the UdpClient fails, there's really nothing we can
                // we about it, and it shouldn't do any further harm. Just swallow the error.
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08120713305898491, 'fraction_numerical': 0.003840877914951989, 'mean_word_length': 1.8395638629283488, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '6431400', 'n_tokens_mistral': 965, 'n_tokens_neox': 923, 'n_words': 279}