UdpTransport.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Net;
  3. using System.Net.Sockets;
  4. namespace Microsoft.Xna.Framework.Net
  5. {
  6. /// <summary>
  7. /// Provides a UDP-based implementation of the <see cref="INetworkTransport"/> interface for sending and receiving network data.
  8. /// </summary>
  9. public class UdpTransport : INetworkTransport
  10. {
  11. private readonly UdpClient udpClient;
  12. private bool isBound;
  13. /// <summary>
  14. /// Initializes a new instance of the <see cref="UdpTransport"/> class.
  15. /// </summary>
  16. public UdpTransport()
  17. {
  18. udpClient = new UdpClient();
  19. }
  20. /// <summary>
  21. /// Receives data from the network in a blocking manner.
  22. /// </summary>
  23. /// <returns>A tuple containing the received data and the sender's endpoint.</returns>
  24. public (byte[] data, IPEndPoint sender) Receive()
  25. {
  26. // Call the async version and block until it completes
  27. return ReceiveAsync().GetAwaiter().GetResult();
  28. }
  29. /// <summary>
  30. /// Receives data from the network asynchronously.
  31. /// </summary>
  32. /// <returns>A task that represents the asynchronous operation. The result contains the received data and the sender's endpoint.</returns>
  33. public async Task<(byte[] data, IPEndPoint sender)> ReceiveAsync()
  34. {
  35. var result = await udpClient.ReceiveAsync();
  36. return (result.Buffer, result.RemoteEndPoint);
  37. }
  38. /// <summary>
  39. /// Sends data to the specified endpoint in a blocking manner.
  40. /// </summary>
  41. /// <param name="data">The data to send.</param>
  42. /// <param name="endpoint">The endpoint to send the data to.</param>
  43. public void Send(byte[] data, IPEndPoint endpoint)
  44. {
  45. // Call the async version and block until it completes
  46. SendAsync(data, endpoint).GetAwaiter().GetResult();
  47. }
  48. /// <summary>
  49. /// Sends data to the specified endpoint asynchronously.
  50. /// </summary>
  51. /// <param name="data">The data to send.</param>
  52. /// <param name="endpoint">The endpoint to send the data to.</param>
  53. /// <returns>A task that represents the asynchronous operation.</returns>
  54. public async Task SendAsync(byte[] data, IPEndPoint endpoint)
  55. {
  56. await udpClient.SendAsync(data, data.Length, endpoint);
  57. }
  58. /// <summary>
  59. /// Releases all resources used by the <see cref="UdpTransport"/> class.
  60. /// </summary>
  61. public void Dispose()
  62. {
  63. udpClient?.Close();
  64. udpClient?.Dispose();
  65. }
  66. /// <summary>
  67. /// Binds the transport to a local endpoint.
  68. /// </summary>
  69. public void Bind()
  70. {
  71. if (!isBound)
  72. {
  73. udpClient.Client.Bind(new IPEndPoint(IPAddress.Any, 0));
  74. isBound = true;
  75. }
  76. }
  77. /// <summary>
  78. /// Gets a value indicating whether the transport is bound to a local endpoint.
  79. /// </summary>
  80. public bool IsBound => isBound;
  81. }
  82. }