2014-02-26 22:31:47 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Net.Sockets;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
2014-07-18 21:07:28 +02:00
|
|
|
|
namespace MediaBrowser.Dlna.Ssdp
|
2014-02-26 22:31:47 +01:00
|
|
|
|
{
|
|
|
|
|
public static class Extensions
|
|
|
|
|
{
|
|
|
|
|
public static Task<int> ReceiveAsync(this Socket socket, byte[] buffer, int offset, int size)
|
|
|
|
|
{
|
|
|
|
|
var tcs = new TaskCompletionSource<int>(socket);
|
|
|
|
|
var remoteip = new IPEndPoint(IPAddress.Any, 0);
|
|
|
|
|
var endpoint = (EndPoint)remoteip;
|
|
|
|
|
|
|
|
|
|
socket.BeginReceiveFrom(buffer, offset, size, SocketFlags.None, ref endpoint, iar =>
|
|
|
|
|
{
|
|
|
|
|
var result = (TaskCompletionSource<int>)iar.AsyncState;
|
|
|
|
|
var iarSocket = (Socket)result.Task.AsyncState;
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
result.TrySetResult(iarSocket.EndReceive(iar));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception exc)
|
|
|
|
|
{
|
|
|
|
|
result.TrySetException(exc);
|
|
|
|
|
}
|
|
|
|
|
}, tcs);
|
|
|
|
|
|
|
|
|
|
return tcs.Task;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string GetValue(this XElement container, XName name)
|
|
|
|
|
{
|
|
|
|
|
var node = container.Element(name);
|
|
|
|
|
|
|
|
|
|
return node == null ? null : node.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-06 19:53:23 +02:00
|
|
|
|
public static string GetAttributeValue(this XElement container, XName name)
|
|
|
|
|
{
|
|
|
|
|
var node = container.Attribute(name);
|
|
|
|
|
|
|
|
|
|
return node == null ? null : node.Value;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-26 22:31:47 +01:00
|
|
|
|
public static string GetDescendantValue(this XElement container, XName name)
|
|
|
|
|
{
|
|
|
|
|
var node = container.Descendants(name)
|
|
|
|
|
.FirstOrDefault();
|
|
|
|
|
|
|
|
|
|
return node == null ? null : node.Value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|