ISCSIConnectionBuffer.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* Copyright (C) 2012-2016 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using Utilities;
  11. namespace ISCSI
  12. {
  13. /// <summary>
  14. /// iSCSI Connection Receive Buffer
  15. /// </summary>
  16. public class ISCSIConnectionReceiveBuffer
  17. {
  18. private byte[] m_buffer;
  19. private int m_readOffset = 0;
  20. private int m_bytesInBuffer = 0;
  21. private int? m_pduLength;
  22. /// <param name="bufferLength">
  23. /// bufferLength should be set to BasicHeaderSegmentLength + TotalAHSLength + HeaderDigestLength + MaxRecvDataSegmentLength + DataDigestLength.
  24. /// (because DataSegmentLength MUST not exceed MaxRecvDataSegmentLength for the direction it is sent)
  25. /// </param>
  26. public ISCSIConnectionReceiveBuffer(int bufferLength)
  27. {
  28. m_buffer = new byte[bufferLength];
  29. }
  30. public void SetNumberOfBytesReceived(int numberOfBytesReceived)
  31. {
  32. m_bytesInBuffer += numberOfBytesReceived;
  33. }
  34. public bool HasCompletePDU()
  35. {
  36. if (m_bytesInBuffer >= 8)
  37. {
  38. if (!m_pduLength.HasValue)
  39. {
  40. m_pduLength = ISCSIPDU.GetPDULength(m_buffer, m_readOffset);
  41. }
  42. return m_bytesInBuffer >= m_pduLength.Value;
  43. }
  44. return false;
  45. }
  46. /// <summary>
  47. /// HasCompletePDU must be called and return true before calling DequeuePDU
  48. /// </summary>
  49. /// <exception cref="System.IO.InvalidDataException"></exception>
  50. public ISCSIPDU DequeuePDU()
  51. {
  52. ISCSIPDU pdu;
  53. try
  54. {
  55. pdu = ISCSIPDU.GetPDU(m_buffer, m_readOffset);
  56. }
  57. catch (IndexOutOfRangeException ex)
  58. {
  59. throw new System.IO.InvalidDataException("Invalid PDU", ex);
  60. }
  61. RemovePDUBytes();
  62. return pdu;
  63. }
  64. /// <summary>
  65. /// HasCompletePDU must be called and return true before calling DequeuePDUBytes
  66. /// </summary>
  67. public byte[] DequeuePDUBytes()
  68. {
  69. byte[] pduBytes = ByteReader.ReadBytes(m_buffer, m_readOffset, m_pduLength.Value);
  70. RemovePDUBytes();
  71. return pduBytes;
  72. }
  73. private void RemovePDUBytes()
  74. {
  75. m_bytesInBuffer -= m_pduLength.Value;
  76. if (m_bytesInBuffer == 0)
  77. {
  78. m_readOffset = 0;
  79. m_pduLength = null;
  80. }
  81. else
  82. {
  83. m_readOffset += m_pduLength.Value;
  84. m_pduLength = null;
  85. if (!HasCompletePDU())
  86. {
  87. Array.Copy(m_buffer, m_readOffset, m_buffer, 0, m_bytesInBuffer);
  88. m_readOffset = 0;
  89. }
  90. }
  91. }
  92. public byte[] Buffer
  93. {
  94. get
  95. {
  96. return m_buffer;
  97. }
  98. }
  99. public int WriteOffset
  100. {
  101. get
  102. {
  103. return m_readOffset + m_bytesInBuffer;
  104. }
  105. }
  106. public int BytesInBuffer
  107. {
  108. get
  109. {
  110. return m_bytesInBuffer;
  111. }
  112. }
  113. public int AvailableLength
  114. {
  115. get
  116. {
  117. return m_buffer.Length - (m_readOffset + m_bytesInBuffer);
  118. }
  119. }
  120. }
  121. }