UdpSocket.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Security;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using Emby.Common.Implementations.Networking;
  10. using MediaBrowser.Model.Net;
  11. namespace Emby.Common.Implementations.Net
  12. {
  13. // THIS IS A LINKED FILE - SHARED AMONGST MULTIPLE PLATFORMS
  14. // Be careful to check any changes compile and work for all platform projects it is shared in.
  15. internal sealed class UdpSocket : DisposableManagedObjectBase, ISocket
  16. {
  17. #region Fields
  18. private Socket _Socket;
  19. private int _LocalPort;
  20. #endregion
  21. public UdpSocket(Socket socket, int localPort, IPAddress ip)
  22. {
  23. if (socket == null) throw new ArgumentNullException("socket");
  24. _Socket = socket;
  25. _LocalPort = localPort;
  26. LocalIPAddress = NetworkManager.ToIpAddressInfo(ip);
  27. _Socket.Bind(new IPEndPoint(ip, _LocalPort));
  28. }
  29. public UdpSocket(Socket socket, IpEndPointInfo endPoint)
  30. {
  31. if (socket == null) throw new ArgumentNullException("socket");
  32. _Socket = socket;
  33. _Socket.Connect(NetworkManager.ToIPEndPoint(endPoint));
  34. }
  35. public IpAddressInfo LocalIPAddress
  36. {
  37. get;
  38. private set;
  39. }
  40. #region ISocket Members
  41. public Task<SocketReceiveResult> ReceiveAsync(CancellationToken cancellationToken)
  42. {
  43. ThrowIfDisposed();
  44. var tcs = new TaskCompletionSource<SocketReceiveResult>();
  45. EndPoint receivedFromEndPoint = new IPEndPoint(IPAddress.Any, 0);
  46. var state = new AsyncReceiveState(_Socket, receivedFromEndPoint);
  47. state.TaskCompletionSource = tcs;
  48. cancellationToken.Register(() => tcs.TrySetCanceled());
  49. #if NETSTANDARD1_6
  50. _Socket.ReceiveFromAsync(new ArraySegment<Byte>(state.Buffer), SocketFlags.None, state.RemoteEndPoint)
  51. .ContinueWith((task, asyncState) =>
  52. {
  53. if (task.Status != TaskStatus.Faulted)
  54. {
  55. var receiveState = asyncState as AsyncReceiveState;
  56. receiveState.RemoteEndPoint = task.Result.RemoteEndPoint;
  57. ProcessResponse(receiveState, () => task.Result.ReceivedBytes, LocalIPAddress);
  58. }
  59. }, state);
  60. #else
  61. _Socket.BeginReceiveFrom(state.Buffer, 0, state.Buffer.Length, SocketFlags.None, ref state.RemoteEndPoint, ProcessResponse, state);
  62. #endif
  63. return tcs.Task;
  64. }
  65. public Task SendAsync(byte[] buffer, int size, IpEndPointInfo endPoint, CancellationToken cancellationToken)
  66. {
  67. ThrowIfDisposed();
  68. if (buffer == null) throw new ArgumentNullException("messageData");
  69. if (endPoint == null) throw new ArgumentNullException("endPoint");
  70. var ipEndPoint = NetworkManager.ToIPEndPoint(endPoint);
  71. #if NETSTANDARD1_6
  72. if (size != buffer.Length)
  73. {
  74. byte[] copy = new byte[size];
  75. Buffer.BlockCopy(buffer, 0, copy, 0, size);
  76. buffer = copy;
  77. }
  78. cancellationToken.ThrowIfCancellationRequested();
  79. _Socket.SendTo(buffer, ipEndPoint);
  80. return Task.FromResult(true);
  81. #else
  82. var taskSource = new TaskCompletionSource<bool>();
  83. try
  84. {
  85. _Socket.BeginSendTo(buffer, 0, size, SocketFlags.None, ipEndPoint, result =>
  86. {
  87. if (cancellationToken.IsCancellationRequested)
  88. {
  89. taskSource.TrySetCanceled();
  90. return;
  91. }
  92. try
  93. {
  94. _Socket.EndSend(result);
  95. taskSource.TrySetResult(true);
  96. }
  97. catch (Exception ex)
  98. {
  99. taskSource.TrySetException(ex);
  100. }
  101. }, null);
  102. }
  103. catch (Exception ex)
  104. {
  105. taskSource.TrySetException(ex);
  106. }
  107. //_Socket.SendTo(messageData, new System.Net.IPEndPoint(IPAddress.Parse(RemoteEndPoint.IPAddress), RemoteEndPoint.Port));
  108. return taskSource.Task;
  109. #endif
  110. }
  111. #endregion
  112. #region Overrides
  113. protected override void Dispose(bool disposing)
  114. {
  115. if (disposing)
  116. {
  117. var socket = _Socket;
  118. if (socket != null)
  119. socket.Dispose();
  120. }
  121. }
  122. #endregion
  123. #region Private Methods
  124. private static void ProcessResponse(AsyncReceiveState state, Func<int> receiveData, IpAddressInfo localIpAddress)
  125. {
  126. try
  127. {
  128. var bytesRead = receiveData();
  129. var ipEndPoint = state.RemoteEndPoint as IPEndPoint;
  130. state.TaskCompletionSource.TrySetResult(
  131. new SocketReceiveResult
  132. {
  133. Buffer = state.Buffer,
  134. ReceivedBytes = bytesRead,
  135. RemoteEndPoint = ToIpEndPointInfo(ipEndPoint),
  136. LocalIPAddress = localIpAddress
  137. }
  138. );
  139. }
  140. catch (ObjectDisposedException)
  141. {
  142. state.TaskCompletionSource.TrySetCanceled();
  143. }
  144. catch (SocketException se)
  145. {
  146. if (se.SocketErrorCode != SocketError.Interrupted && se.SocketErrorCode != SocketError.OperationAborted && se.SocketErrorCode != SocketError.Shutdown)
  147. state.TaskCompletionSource.TrySetException(se);
  148. else
  149. state.TaskCompletionSource.TrySetCanceled();
  150. }
  151. catch (Exception ex)
  152. {
  153. state.TaskCompletionSource.TrySetException(ex);
  154. }
  155. }
  156. private static IpEndPointInfo ToIpEndPointInfo(IPEndPoint endpoint)
  157. {
  158. if (endpoint == null)
  159. {
  160. return null;
  161. }
  162. return NetworkManager.ToIpEndPointInfo(endpoint);
  163. }
  164. private void ProcessResponse(IAsyncResult asyncResult)
  165. {
  166. #if NET46
  167. var state = asyncResult.AsyncState as AsyncReceiveState;
  168. try
  169. {
  170. var bytesRead = state.Socket.EndReceiveFrom(asyncResult, ref state.RemoteEndPoint);
  171. var ipEndPoint = state.RemoteEndPoint as IPEndPoint;
  172. state.TaskCompletionSource.TrySetResult(
  173. new SocketReceiveResult
  174. {
  175. Buffer = state.Buffer,
  176. ReceivedBytes = bytesRead,
  177. RemoteEndPoint = ToIpEndPointInfo(ipEndPoint),
  178. LocalIPAddress = LocalIPAddress
  179. }
  180. );
  181. }
  182. catch (ObjectDisposedException)
  183. {
  184. state.TaskCompletionSource.TrySetCanceled();
  185. }
  186. catch (Exception ex)
  187. {
  188. state.TaskCompletionSource.TrySetException(ex);
  189. }
  190. #endif
  191. }
  192. #endregion
  193. #region Private Classes
  194. private class AsyncReceiveState
  195. {
  196. public AsyncReceiveState(Socket socket, EndPoint remoteEndPoint)
  197. {
  198. this.Socket = socket;
  199. this.RemoteEndPoint = remoteEndPoint;
  200. }
  201. public EndPoint RemoteEndPoint;
  202. public byte[] Buffer = new byte[8192];
  203. public Socket Socket { get; private set; }
  204. public TaskCompletionSource<SocketReceiveResult> TaskCompletionSource { get; set; }
  205. }
  206. #endregion
  207. }
  208. }