UdpClientAdapter.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Net.Sockets;
  5. using System.Threading;
  6. using NModbus.Unme.Common;
  7. namespace NModbus.IO
  8. {
  9. /// <summary>
  10. /// Concrete Implementor - http://en.wikipedia.org/wiki/Bridge_Pattern
  11. /// </summary>
  12. public class UdpClientAdapter : IStreamResource
  13. {
  14. // strategy for cross platform r/w
  15. private const int MaxBufferSize = ushort.MaxValue;
  16. private UdpClient _udpClient;
  17. private readonly byte[] _buffer = new byte[MaxBufferSize];
  18. private int _bufferOffset;
  19. public UdpClientAdapter(UdpClient udpClient)
  20. {
  21. if (udpClient == null)
  22. {
  23. throw new ArgumentNullException(nameof(udpClient));
  24. }
  25. _udpClient = udpClient;
  26. }
  27. public int InfiniteTimeout => Timeout.Infinite;
  28. public int ReadTimeout
  29. {
  30. get => _udpClient.Client.ReceiveTimeout;
  31. set => _udpClient.Client.ReceiveTimeout = value;
  32. }
  33. public int WriteTimeout
  34. {
  35. get => _udpClient.Client.SendTimeout;
  36. set => _udpClient.Client.SendTimeout = value;
  37. }
  38. public void DiscardInBuffer()
  39. {
  40. // no-op
  41. }
  42. public int Read(byte[] buffer, int offset, int count)
  43. {
  44. if (buffer == null)
  45. {
  46. throw new ArgumentNullException(nameof(buffer));
  47. }
  48. if (offset < 0)
  49. {
  50. throw new ArgumentOutOfRangeException(
  51. nameof(offset),
  52. "Argument offset must be greater than or equal to 0.");
  53. }
  54. if (offset > buffer.Length)
  55. {
  56. throw new ArgumentOutOfRangeException(
  57. nameof(offset),
  58. "Argument offset cannot be greater than the length of buffer.");
  59. }
  60. if (count < 0)
  61. {
  62. throw new ArgumentOutOfRangeException(
  63. nameof(count),
  64. "Argument count must be greater than or equal to 0.");
  65. }
  66. if (count > buffer.Length - offset)
  67. {
  68. throw new ArgumentOutOfRangeException(
  69. nameof(count),
  70. "Argument count cannot be greater than the length of buffer minus offset.");
  71. }
  72. if (_bufferOffset == 0)
  73. {
  74. _bufferOffset = _udpClient.Client.Receive(_buffer);
  75. }
  76. if (_bufferOffset < count)
  77. {
  78. throw new IOException("Not enough bytes in the datagram.");
  79. }
  80. Buffer.BlockCopy(_buffer, 0, buffer, offset, count);
  81. _bufferOffset -= count;
  82. Buffer.BlockCopy(_buffer, count, _buffer, 0, _bufferOffset);
  83. return count;
  84. }
  85. public void Write(byte[] buffer, int offset, int count)
  86. {
  87. if (buffer == null)
  88. {
  89. throw new ArgumentNullException(nameof(buffer));
  90. }
  91. if (offset < 0)
  92. {
  93. throw new ArgumentOutOfRangeException(
  94. nameof(offset),
  95. "Argument offset must be greater than or equal to 0.");
  96. }
  97. if (offset > buffer.Length)
  98. {
  99. throw new ArgumentOutOfRangeException(
  100. nameof(offset),
  101. "Argument offset cannot be greater than the length of buffer.");
  102. }
  103. if (count < 0)
  104. {
  105. throw new ArgumentOutOfRangeException(
  106. nameof(count),
  107. "Argument count must be greater than or equal to 0.");
  108. }
  109. if (count > buffer.Length - offset)
  110. {
  111. throw new ArgumentOutOfRangeException(
  112. nameof(count),
  113. "Argument count cannot be greater than the length of buffer minus offset.");
  114. }
  115. _udpClient.Client.Send(buffer.Skip(offset).Take(count).ToArray());
  116. }
  117. public void Dispose()
  118. {
  119. Dispose(true);
  120. GC.SuppressFinalize(this);
  121. }
  122. protected virtual void Dispose(bool disposing)
  123. {
  124. if (disposing)
  125. {
  126. DisposableUtility.Dispose(ref _udpClient);
  127. }
  128. }
  129. }
  130. }