TransactNamedPipeInputStream.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. using System;
  18. using System.IO;
  19. using SharpCifs.Util.Sharpen;
  20. namespace SharpCifs.Smb
  21. {
  22. internal class TransactNamedPipeInputStream : SmbFileInputStream
  23. {
  24. private const int InitPipeSize = 4096;
  25. private byte[] _pipeBuf = new byte[InitPipeSize];
  26. private int _begIdx;
  27. private int _nxtIdx;
  28. private int _used;
  29. private bool _dcePipe;
  30. internal object Lock;
  31. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  32. /// <exception cref="System.UriFormatException"></exception>
  33. /// <exception cref="UnknownHostException"></exception>
  34. internal TransactNamedPipeInputStream(SmbNamedPipe pipe) : base(pipe, (pipe.PipeType
  35. & unchecked((int)(0xFFFF00FF))) | SmbFile.OExcl)
  36. {
  37. _dcePipe = (pipe.PipeType & SmbNamedPipe.PipeTypeDceTransact) != SmbNamedPipe
  38. .PipeTypeDceTransact;
  39. Lock = new object();
  40. }
  41. /// <exception cref="System.IO.IOException"></exception>
  42. public override int Read()
  43. {
  44. int result = -1;
  45. lock (Lock)
  46. {
  47. try
  48. {
  49. while (_used == 0)
  50. {
  51. Runtime.Wait(Lock);
  52. }
  53. }
  54. catch (Exception ie)
  55. {
  56. throw new IOException(ie.Message);
  57. }
  58. result = _pipeBuf[_begIdx] & unchecked(0xFF);
  59. _begIdx = (_begIdx + 1) % _pipeBuf.Length;
  60. }
  61. return result;
  62. }
  63. /// <exception cref="System.IO.IOException"></exception>
  64. public override int Read(byte[] b)
  65. {
  66. return Read(b, 0, b.Length);
  67. }
  68. /// <exception cref="System.IO.IOException"></exception>
  69. public override int Read(byte[] b, int off, int len)
  70. {
  71. int result = -1;
  72. int i;
  73. if (len <= 0)
  74. {
  75. return 0;
  76. }
  77. lock (Lock)
  78. {
  79. try
  80. {
  81. while (_used == 0)
  82. {
  83. Runtime.Wait(Lock);
  84. }
  85. }
  86. catch (Exception ie)
  87. {
  88. throw new IOException(ie.Message);
  89. }
  90. i = _pipeBuf.Length - _begIdx;
  91. result = len > _used ? _used : len;
  92. if (_used > i && result > i)
  93. {
  94. Array.Copy(_pipeBuf, _begIdx, b, off, i);
  95. off += i;
  96. Array.Copy(_pipeBuf, 0, b, off, result - i);
  97. }
  98. else
  99. {
  100. Array.Copy(_pipeBuf, _begIdx, b, off, result);
  101. }
  102. _used -= result;
  103. _begIdx = (_begIdx + result) % _pipeBuf.Length;
  104. }
  105. return result;
  106. }
  107. /// <exception cref="System.IO.IOException"></exception>
  108. public override int Available()
  109. {
  110. if (File.Log.Level >= 3)
  111. {
  112. File.Log.WriteLine("Named Pipe available() does not apply to TRANSACT Named Pipes"
  113. );
  114. }
  115. return 0;
  116. }
  117. internal virtual int Receive(byte[] b, int off, int len)
  118. {
  119. int i;
  120. if (len > (_pipeBuf.Length - _used))
  121. {
  122. byte[] tmp;
  123. int newSize;
  124. newSize = _pipeBuf.Length * 2;
  125. if (len > (newSize - _used))
  126. {
  127. newSize = len + _used;
  128. }
  129. tmp = _pipeBuf;
  130. _pipeBuf = new byte[newSize];
  131. i = tmp.Length - _begIdx;
  132. if (_used > i)
  133. {
  134. Array.Copy(tmp, _begIdx, _pipeBuf, 0, i);
  135. Array.Copy(tmp, 0, _pipeBuf, i, _used - i);
  136. }
  137. else
  138. {
  139. Array.Copy(tmp, _begIdx, _pipeBuf, 0, _used);
  140. }
  141. _begIdx = 0;
  142. _nxtIdx = _used;
  143. tmp = null;
  144. }
  145. i = _pipeBuf.Length - _nxtIdx;
  146. if (len > i)
  147. {
  148. Array.Copy(b, off, _pipeBuf, _nxtIdx, i);
  149. off += i;
  150. Array.Copy(b, off, _pipeBuf, 0, len - i);
  151. }
  152. else
  153. {
  154. Array.Copy(b, off, _pipeBuf, _nxtIdx, len);
  155. }
  156. _nxtIdx = (_nxtIdx + len) % _pipeBuf.Length;
  157. _used += len;
  158. return len;
  159. }
  160. /// <exception cref="System.IO.IOException"></exception>
  161. public virtual int Dce_read(byte[] b, int off, int len)
  162. {
  163. return base.Read(b, off, len);
  164. }
  165. }
  166. }