<commit_before>using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Security;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Rssdp.Infrastructure;

namespace Rssdp
{
	// THIS IS A LINKED FILE - SHARED AMONGST MULTIPLE PLATFORMS	
	// Be careful to check any changes compile and work for all platform projects it is shared in.
	internal sealed class UdpSocket : DisposableManagedObjectBase, IUdpSocket
	{

		#region Fields

		private System.Net.Sockets.Socket _Socket;
		private int _LocalPort;

		#endregion

		#region Constructors

		public UdpSocket(Socket socket, string ipAddress, int localPort)
		{
			if (socket == null) throw new ArgumentNullException("socket");

			_Socket = socket;
			_LocalPort = localPort;

			var ip = String.IsNullOrEmpty(ipAddress) ? GetDefaultIpAddress(socket) : IPAddress.Parse(ipAddress);

			_Socket.Bind(new IPEndPoint(ip, _LocalPort));
			if (_LocalPort == 0)
				_LocalPort = ((IPEndPoint) _Socket.LocalEndPoint).Port;
		}

		#endregion

		#region IUdpSocket Members

		public System.Threading.Tasks.Task<ReceivedUdpData> ReceiveAsync()
		{
			ThrowIfDisposed();

			var tcs = new TaskCompletionSource<ReceivedUdpData>();

			System.Net.EndPoint receivedFromEndPoint = new IPEndPoint(GetDefaultIpAddress(_Socket), 0);
			var state = new AsyncReceiveState(_Socket, receivedFromEndPoint);
			state.TaskCompletionSource = tcs;
#if NETSTANDARD1_3
			_Socket.ReceiveFromAsync(new System.ArraySegment<Byte>(state.Buffer), System.Net.Sockets.SocketFlags.None, state.EndPoint)
				.ContinueWith((task, asyncState) =>
				{
					if (this.IsDisposed) return;

					try
					{
						if (task.Status != TaskStatus.Faulted)
						{
							var receiveState = asyncState as AsyncReceiveState;
							receiveState.EndPoint = task.Result.RemoteEndPoint;
							ProcessResponse(receiveState, () => task.Result.ReceivedBytes);
						}
					}
					catch (ObjectDisposedException) { if (!this.IsDisposed) throw; } //Only rethrow disposed exceptions if we're NOT disposed, because then they are unexpected.
				}, state);
#else
			_Socket.BeginReceiveFrom(state.Buffer, 0, state.Buffer.Length, System.Net.Sockets.SocketFlags.None, ref state.EndPoint,
				new AsyncCallback((result) => ProcessResponse(state, () => state.Socket.EndReceiveFrom(result, ref state.EndPoint))), state);
#endif

			return tcs.Task;
		}

		public void SendTo(byte[] messageData, UdpEndPoint endPoint)
		{
			ThrowIfDisposed();

			if (messageData == null) throw new ArgumentNullException("messageData");
			if (endPoint == null) throw new ArgumentNullException("endPoint");

			_Socket.SendTo(messageData, new System.Net.IPEndPoint(IPAddress.Parse(endPoint.IPAddress), endPoint.Port));
		}

		#endregion

		#region Overrides

		protected override void Dispose(bool disposing)<commit_msg>Prevent raising exceptions on UdpSocket on async receive on inner disposed socket<commit_after>using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Security;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Rssdp.Infrastructure;

namespace Rssdp
{
	// THIS IS A LINKED FILE - SHARED AMONGST MULTIPLE PLATFORMS	
	// Be careful to check any changes compile and work for all platform projects it is shared in.
	internal sealed class UdpSocket : DisposableManagedObjectBase, IUdpSocket
	{

		#region Fields

		private System.Net.Sockets.Socket _Socket;
		private int _LocalPort;

		#endregion

		#region Constructors

		public UdpSocket(Socket socket, string ipAddress, int localPort)
		{
			if (socket == null) throw new ArgumentNullException("socket");

			_Socket = socket;
			_LocalPort = localPort;

			var ip = String.IsNullOrEmpty(ipAddress) ? GetDefaultIpAddress(socket) : IPAddress.Parse(ipAddress);

			_Socket.Bind(new IPEndPoint(ip, _LocalPort));
			if (_LocalPort == 0)
				_LocalPort = ((IPEndPoint) _Socket.LocalEndPoint).Port;
		}

		#endregion

		#region IUdpSocket Members

		public System.Threading.Tasks.Task<ReceivedUdpData> ReceiveAsync()
		{
			ThrowIfDisposed();

			var tcs = new TaskCompletionSource<ReceivedUdpData>();

			System.Net.EndPoint receivedFromEndPoint = new IPEndPoint(GetDefaultIpAddress(_Socket), 0);
			var state = new AsyncReceiveState(_Socket, receivedFromEndPoint);
			state.TaskCompletionSource = tcs;
#if NETSTANDARD1_3
			_Socket.ReceiveFromAsync(new System.ArraySegment<Byte>(state.Buffer), System.Net.Sockets.SocketFlags.None, state.EndPoint)
				.ContinueWith((task, asyncState) =>
				{
					if (this.IsDisposed) return;

					try
					{
						if (task.Status != TaskStatus.Faulted)
						{
							var receiveState = asyncState as AsyncReceiveState;
							receiveState.EndPoint = task.Result.RemoteEndPoint;
							ProcessResponse(receiveState, () => task.Result.ReceivedBytes);
						}
					}
					catch (ObjectDisposedException) { if (!this.IsDisposed) throw; } //Only rethrow disposed exceptions if we're NOT disposed, because then they are unexpected.
				}, state);
#else
			try
			{
				_Socket.BeginReceiveFrom(state.Buffer, 0, state.Buffer.Length, System.Net.Sockets.SocketFlags.None, ref state.EndPoint,
					new AsyncCallback((result) => ProcessResponse(state, () => state.Socket.EndReceiveFrom(result, ref state.EndPoint))), state);
			}
			catch (ObjectDisposedException) { if (!this.IsDisposed) throw; } //Only rethrow disposed exceptions if we're NOT disposed, because then they are unexpected.

#endif

			return tcs.Task;
		}

		public void SendTo(byte[] messageData, UdpEndPoint endPoint)
		{
			ThrowIfDisposed();

			if (messageData == null) throw new ArgumentNullException("messageData");
			if (endPoint == null) throw new ArgumentNullException("endPoint");

			_Socket.SendTo(messageData, new System.Net.IPEndPoint(IPAddress.Parse(endPoint.IPAddress), endPoint.Port));
		}

		#endregion

		#region Overrides

		protected override void Dispose(bool disposing)
{'fraction_non_alphanumeric': 0.0998003992015968, 'fraction_numerical': 0.00166333998669328, 'mean_word_length': 4.417117117117117, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, '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': '5208001', 'n_tokens_mistral': 2104, 'n_tokens_neox': 1776, 'n_words': 451}