Transport.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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.Smb;
  20. using SharpCifs.Util.Sharpen;
  21. using System.Threading.Tasks;
  22. namespace SharpCifs.Util.Transport
  23. {
  24. /// <summary>
  25. /// This class simplifies communication for protocols that support
  26. /// multiplexing requests.
  27. /// </summary>
  28. /// <remarks>
  29. /// This class simplifies communication for protocols that support
  30. /// multiplexing requests. It encapsulates a stream and some protocol
  31. /// knowledge (provided by a concrete subclass) so that connecting,
  32. /// disconnecting, sending, and receiving can be syncronized
  33. /// properly. Apparatus is provided to send and receive requests
  34. /// concurrently.
  35. /// </remarks>
  36. public abstract class Transport : IRunnable
  37. {
  38. internal static int Id;
  39. //internal static LogStream log = LogStream.GetInstance();
  40. public LogStream Log
  41. {
  42. get
  43. {
  44. return LogStream.GetInstance();
  45. }
  46. }
  47. /// <exception cref="System.IO.IOException"></exception>
  48. public static int Readn(InputStream @in, byte[] b, int off, int len)
  49. {
  50. int i = 0;
  51. int n = -5;
  52. while (i < len)
  53. {
  54. n = @in.Read(b, off + i, len - i);
  55. if (n <= 0)
  56. {
  57. break;
  58. }
  59. i += n;
  60. }
  61. return i;
  62. }
  63. internal int State;
  64. internal string Name = "Transport" + Id++;
  65. internal Thread Thread;
  66. internal TransportException Te;
  67. protected internal Hashtable ResponseMap = new Hashtable();
  68. /// <exception cref="System.IO.IOException"></exception>
  69. protected internal abstract void MakeKey(ServerMessageBlock request);
  70. /// <exception cref="System.IO.IOException"></exception>
  71. protected internal abstract ServerMessageBlock PeekKey();
  72. /// <exception cref="System.IO.IOException"></exception>
  73. protected internal abstract void DoSend(ServerMessageBlock request);
  74. /// <exception cref="System.IO.IOException"></exception>
  75. protected internal abstract void DoRecv(Response response);
  76. /// <exception cref="System.IO.IOException"></exception>
  77. protected internal abstract void DoSkip();
  78. /// <exception cref="System.IO.IOException"></exception>
  79. public virtual void Sendrecv(ServerMessageBlock request, Response response, long timeout)
  80. {
  81. lock (this)
  82. {
  83. MakeKey(request);
  84. response.IsReceived = false;
  85. try
  86. {
  87. ResponseMap.Put(request, response);
  88. DoSend(request);
  89. response.Expiration = Runtime.CurrentTimeMillis() + timeout;
  90. while (!response.IsReceived)
  91. {
  92. Runtime.Wait(this, timeout);
  93. timeout = response.Expiration - Runtime.CurrentTimeMillis();
  94. if (timeout <= 0)
  95. {
  96. throw new TransportException(
  97. Name + " timedout waiting for response to " + request);
  98. }
  99. }
  100. }
  101. catch (IOException ioe)
  102. {
  103. if (Log.Level > 2)
  104. {
  105. Runtime.PrintStackTrace(ioe, Log);
  106. }
  107. try
  108. {
  109. Disconnect(true);
  110. }
  111. catch (IOException ioe2)
  112. {
  113. Runtime.PrintStackTrace(ioe2, Log);
  114. }
  115. throw;
  116. }
  117. catch (Exception ie)
  118. {
  119. throw new TransportException(ie);
  120. }
  121. finally
  122. {
  123. //Sharpen.Collections.Remove(response_map, request);
  124. ResponseMap.Remove(request);
  125. }
  126. }
  127. }
  128. private void Loop()
  129. {
  130. while (Thread.CurrentThread().Equals(Thread))
  131. {
  132. if (Thread.IsCanceled)
  133. break;
  134. try
  135. {
  136. ServerMessageBlock key = PeekKey();
  137. if (key == null)
  138. {
  139. throw new IOException("end of stream");
  140. }
  141. lock (this)
  142. {
  143. if (Thread.IsCanceled)
  144. break;
  145. Response response = (Response)ResponseMap.Get(key);
  146. if (response == null)
  147. {
  148. if (Log.Level >= 4)
  149. {
  150. Log.WriteLine("Invalid key, skipping message");
  151. }
  152. DoSkip();
  153. }
  154. else
  155. {
  156. DoRecv(response);
  157. if (Thread.IsCanceled)
  158. break;
  159. response.IsReceived = true;
  160. Runtime.NotifyAll(this);
  161. }
  162. }
  163. }
  164. catch (Exception ex)
  165. {
  166. string msg = ex.Message;
  167. bool timeout = msg != null && msg.Equals("Read timed out");
  168. bool hard = timeout == false;
  169. if (!timeout && Log.Level >= 3)
  170. {
  171. Runtime.PrintStackTrace(ex, Log);
  172. }
  173. try
  174. {
  175. Disconnect(hard);
  176. }
  177. catch (IOException ioe)
  178. {
  179. Runtime.PrintStackTrace(ioe, Log);
  180. }
  181. }
  182. }
  183. }
  184. /// <exception cref="System.Exception"></exception>
  185. protected internal abstract void DoConnect();
  186. /// <exception cref="System.IO.IOException"></exception>
  187. protected internal abstract void DoDisconnect(bool hard);
  188. /// <exception cref="SharpCifs.Util.Transport.TransportException"></exception>
  189. public virtual void Connect(long timeout)
  190. {
  191. lock (this)
  192. {
  193. try
  194. {
  195. switch (State)
  196. {
  197. case 0:
  198. {
  199. break;
  200. }
  201. case 3:
  202. {
  203. return;
  204. }
  205. case 4:
  206. {
  207. // already connected
  208. State = 0;
  209. throw new TransportException("Connection in error", Te);
  210. }
  211. default:
  212. {
  213. //TransportException te = new TransportException("Invalid state: " + state);
  214. State = 0;
  215. throw new TransportException("Invalid state: " + State);
  216. }
  217. }
  218. State = 1;
  219. Te = null;
  220. if (Thread != null)
  221. {
  222. Thread.Cancel(true);
  223. Thread.Dispose();
  224. }
  225. Thread = new Thread(this);
  226. Thread.SetDaemon(true);
  227. lock (Thread)
  228. {
  229. Thread.Start(true);
  230. Runtime.Wait(Thread, timeout);
  231. switch (State)
  232. {
  233. case 1:
  234. {
  235. State = 0;
  236. Thread?.Cancel();
  237. Thread?.Dispose();
  238. Thread = null;
  239. throw new TransportException("Connection timeout");
  240. }
  241. case 2:
  242. {
  243. if (Te != null)
  244. {
  245. State = 4;
  246. Thread?.Cancel();
  247. Thread?.Dispose();
  248. Thread = null;
  249. throw Te;
  250. }
  251. State = 3;
  252. return;
  253. }
  254. }
  255. }
  256. }
  257. catch (Exception ie)
  258. {
  259. State = 0;
  260. Thread?.Cancel();
  261. Thread?.Dispose();
  262. Thread = null;
  263. throw new TransportException(ie);
  264. }
  265. finally
  266. {
  267. if (State != 0 && State != 3 && State != 4)
  268. {
  269. if (Log.Level >= 1)
  270. {
  271. Log.WriteLine("Invalid state: " + State);
  272. }
  273. State = 0;
  274. Thread?.Cancel();
  275. Thread?.Dispose();
  276. Thread = null;
  277. }
  278. }
  279. }
  280. }
  281. /// <exception cref="System.IO.IOException"></exception>
  282. public virtual void Disconnect(bool hard)
  283. {
  284. if (hard)
  285. {
  286. IOException ioe = null;
  287. switch (State)
  288. {
  289. case 0:
  290. {
  291. return;
  292. }
  293. case 2:
  294. {
  295. hard = true;
  296. goto case 3;
  297. }
  298. case 3:
  299. {
  300. if (ResponseMap.Count != 0 && !hard)
  301. {
  302. break;
  303. }
  304. try
  305. {
  306. DoDisconnect(hard);
  307. }
  308. catch (IOException ioe0)
  309. {
  310. ioe = ioe0;
  311. }
  312. goto case 4;
  313. }
  314. case 4:
  315. {
  316. Thread?.Cancel();
  317. Thread?.Dispose();
  318. Thread = null;
  319. State = 0;
  320. break;
  321. }
  322. default:
  323. {
  324. if (Log.Level >= 1)
  325. {
  326. Log.WriteLine("Invalid state: " + State);
  327. }
  328. Thread?.Cancel();
  329. Thread?.Dispose();
  330. Thread = null;
  331. State = 0;
  332. break;
  333. }
  334. }
  335. if (ioe != null)
  336. {
  337. throw ioe;
  338. }
  339. return;
  340. }
  341. lock (this)
  342. {
  343. IOException ioe = null;
  344. switch (State)
  345. {
  346. case 0:
  347. {
  348. return;
  349. }
  350. case 2:
  351. {
  352. hard = true;
  353. goto case 3;
  354. }
  355. case 3:
  356. {
  357. if (ResponseMap.Count != 0 && !hard)
  358. {
  359. break;
  360. }
  361. try
  362. {
  363. DoDisconnect(hard);
  364. }
  365. catch (IOException ioe0)
  366. {
  367. ioe = ioe0;
  368. }
  369. goto case 4;
  370. }
  371. case 4:
  372. {
  373. Thread?.Cancel();
  374. Thread?.Dispose();
  375. Thread = null;
  376. State = 0;
  377. break;
  378. }
  379. default:
  380. {
  381. if (Log.Level >= 1)
  382. {
  383. Log.WriteLine("Invalid state: " + State);
  384. }
  385. Thread?.Cancel();
  386. Thread?.Dispose();
  387. Thread = null;
  388. State = 0;
  389. break;
  390. }
  391. }
  392. if (ioe != null)
  393. {
  394. throw ioe;
  395. }
  396. }
  397. }
  398. public virtual void Run()
  399. {
  400. Thread runThread = Thread.CurrentThread();
  401. if (runThread.IsCanceled)
  402. return;
  403. Exception ex0 = null;
  404. try
  405. {
  406. DoConnect();
  407. }
  408. catch (Exception ex)
  409. {
  410. ex0 = ex;
  411. // Defer to below where we're locked
  412. return;
  413. }
  414. finally
  415. {
  416. lock (runThread)
  417. {
  418. if (!runThread.IsCanceled)
  419. {
  420. if (!runThread.Equals(Thread))
  421. {
  422. if (ex0 != null)
  423. {
  424. if (Log.Level >= 2)
  425. {
  426. Runtime.PrintStackTrace(ex0, Log);
  427. }
  428. }
  429. //return;
  430. }
  431. if (ex0 != null)
  432. {
  433. Te = new TransportException(ex0);
  434. }
  435. State = 2;
  436. // run connected
  437. Runtime.Notify(runThread);
  438. }
  439. }
  440. }
  441. if (runThread.IsCanceled)
  442. return;
  443. Loop();
  444. }
  445. public override string ToString()
  446. {
  447. return Name;
  448. }
  449. }
  450. }