UdsSession.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. using System;
  2. using System.Net.Sockets;
  3. using System.Text;
  4. using System.Threading;
  5. namespace NetCoreServer
  6. {
  7. /// <summary>
  8. /// Unix Domain Socket session is used to read and write data from the connected Unix Domain Socket client
  9. /// </summary>
  10. /// <remarks>Thread-safe</remarks>
  11. public class UdsSession : IDisposable
  12. {
  13. /// <summary>
  14. /// Initialize the session with a given server
  15. /// </summary>
  16. /// <param name="server">Unix Domain Socket server</param>
  17. public UdsSession(UdsServer server)
  18. {
  19. Id = Guid.NewGuid();
  20. Server = server;
  21. OptionReceiveBufferSize = server.OptionReceiveBufferSize;
  22. OptionSendBufferSize = server.OptionSendBufferSize;
  23. }
  24. /// <summary>
  25. /// Session Id
  26. /// </summary>
  27. public Guid Id { get; }
  28. /// <summary>
  29. /// Server
  30. /// </summary>
  31. public UdsServer Server { get; }
  32. /// <summary>
  33. /// Socket
  34. /// </summary>
  35. public Socket Socket { get; private set; }
  36. /// <summary>
  37. /// Number of bytes pending sent by the session
  38. /// </summary>
  39. public long BytesPending { get; private set; }
  40. /// <summary>
  41. /// Number of bytes sending by the session
  42. /// </summary>
  43. public long BytesSending { get; private set; }
  44. /// <summary>
  45. /// Number of bytes sent by the session
  46. /// </summary>
  47. public long BytesSent { get; private set; }
  48. /// <summary>
  49. /// Number of bytes received by the session
  50. /// </summary>
  51. public long BytesReceived { get; private set; }
  52. /// <summary>
  53. /// Option: receive buffer limit
  54. /// </summary>
  55. public int OptionReceiveBufferLimit { get; set; } = 0;
  56. /// <summary>
  57. /// Option: receive buffer size
  58. /// </summary>
  59. public int OptionReceiveBufferSize { get; set; } = 8192;
  60. /// <summary>
  61. /// Option: send buffer limit
  62. /// </summary>
  63. public int OptionSendBufferLimit { get; set; } = 0;
  64. /// <summary>
  65. /// Option: send buffer size
  66. /// </summary>
  67. public int OptionSendBufferSize { get; set; } = 8192;
  68. #region Connect/Disconnect session
  69. /// <summary>
  70. /// Is the session connected?
  71. /// </summary>
  72. public bool IsConnected { get; private set; }
  73. /// <summary>
  74. /// Connect the session
  75. /// </summary>
  76. /// <param name="socket">Session socket</param>
  77. internal void Connect(Socket socket)
  78. {
  79. Socket = socket;
  80. // Update the session socket disposed flag
  81. IsSocketDisposed = false;
  82. // Setup buffers
  83. _receiveBuffer = new Buffer();
  84. _sendBufferMain = new Buffer();
  85. _sendBufferFlush = new Buffer();
  86. // Setup event args
  87. _receiveEventArg = new SocketAsyncEventArgs();
  88. _receiveEventArg.Completed += OnAsyncCompleted;
  89. _sendEventArg = new SocketAsyncEventArgs();
  90. _sendEventArg.Completed += OnAsyncCompleted;
  91. // Prepare receive & send buffers
  92. _receiveBuffer.Reserve(OptionReceiveBufferSize);
  93. _sendBufferMain.Reserve(OptionSendBufferSize);
  94. _sendBufferFlush.Reserve(OptionSendBufferSize);
  95. // Reset statistic
  96. BytesPending = 0;
  97. BytesSending = 0;
  98. BytesSent = 0;
  99. BytesReceived = 0;
  100. // Call the session connecting handler
  101. OnConnecting();
  102. // Call the session connecting handler in the server
  103. Server.OnConnectingInternal(this);
  104. // Update the connected flag
  105. IsConnected = true;
  106. // Try to receive something from the client
  107. TryReceive();
  108. // Check the socket disposed state: in some rare cases it might be disconnected while receiving!
  109. if (IsSocketDisposed)
  110. return;
  111. // Call the session connected handler
  112. OnConnected();
  113. // Call the session connected handler in the server
  114. Server.OnConnectedInternal(this);
  115. // Call the empty send buffer handler
  116. if (_sendBufferMain.IsEmpty)
  117. OnEmpty();
  118. }
  119. /// <summary>
  120. /// Disconnect the session
  121. /// </summary>
  122. /// <returns>'true' if the section was successfully disconnected, 'false' if the section is already disconnected</returns>
  123. public virtual bool Disconnect()
  124. {
  125. if (!IsConnected)
  126. return false;
  127. // Reset event args
  128. _receiveEventArg.Completed -= OnAsyncCompleted;
  129. _sendEventArg.Completed -= OnAsyncCompleted;
  130. // Call the session disconnecting handler
  131. OnDisconnecting();
  132. // Call the session disconnecting handler in the server
  133. Server.OnDisconnectingInternal(this);
  134. try
  135. {
  136. try
  137. {
  138. // Shutdown the socket associated with the client
  139. Socket.Shutdown(SocketShutdown.Both);
  140. }
  141. catch (SocketException) {}
  142. // Close the session socket
  143. Socket.Close();
  144. // Dispose the session socket
  145. Socket.Dispose();
  146. // Dispose event arguments
  147. _receiveEventArg.Dispose();
  148. _sendEventArg.Dispose();
  149. // Update the session socket disposed flag
  150. IsSocketDisposed = true;
  151. }
  152. catch (ObjectDisposedException) {}
  153. // Update the connected flag
  154. IsConnected = false;
  155. // Update sending/receiving flags
  156. _receiving = false;
  157. _sending = false;
  158. // Clear send/receive buffers
  159. ClearBuffers();
  160. // Call the session disconnected handler
  161. OnDisconnected();
  162. // Call the session disconnected handler in the server
  163. Server.OnDisconnectedInternal(this);
  164. // Unregister session
  165. Server.UnregisterSession(Id);
  166. return true;
  167. }
  168. #endregion
  169. #region Send/Receive data
  170. // Receive buffer
  171. private bool _receiving;
  172. private Buffer _receiveBuffer;
  173. private SocketAsyncEventArgs _receiveEventArg;
  174. // Send buffer
  175. private readonly object _sendLock = new object();
  176. private bool _sending;
  177. private Buffer _sendBufferMain;
  178. private Buffer _sendBufferFlush;
  179. private SocketAsyncEventArgs _sendEventArg;
  180. private long _sendBufferFlushOffset;
  181. /// <summary>
  182. /// Send data to the client (synchronous)
  183. /// </summary>
  184. /// <param name="buffer">Buffer to send</param>
  185. /// <returns>Size of sent data</returns>
  186. public virtual long Send(byte[] buffer) => Send(buffer.AsSpan());
  187. /// <summary>
  188. /// Send data to the client (synchronous)
  189. /// </summary>
  190. /// <param name="buffer">Buffer to send</param>
  191. /// <param name="offset">Buffer offset</param>
  192. /// <param name="size">Buffer size</param>
  193. /// <returns>Size of sent data</returns>
  194. public virtual long Send(byte[] buffer, long offset, long size) => Send(buffer.AsSpan((int)offset, (int)size));
  195. /// <summary>
  196. /// Send data to the client (synchronous)
  197. /// </summary>
  198. /// <param name="buffer">Buffer to send as a span of bytes</param>
  199. /// <returns>Size of sent data</returns>
  200. public virtual long Send(ReadOnlySpan<byte> buffer)
  201. {
  202. if (!IsConnected)
  203. return 0;
  204. if (buffer.IsEmpty)
  205. return 0;
  206. // Sent data to the client
  207. long sent = Socket.Send(buffer, SocketFlags.None, out SocketError ec);
  208. if (sent > 0)
  209. {
  210. // Update statistic
  211. BytesSent += sent;
  212. Interlocked.Add(ref Server._bytesSent, sent);
  213. // Call the buffer sent handler
  214. OnSent(sent, BytesPending + BytesSending);
  215. }
  216. // Check for socket error
  217. if (ec != SocketError.Success)
  218. {
  219. SendError(ec);
  220. Disconnect();
  221. }
  222. return sent;
  223. }
  224. /// <summary>
  225. /// Send text to the client (synchronous)
  226. /// </summary>
  227. /// <param name="text">Text string to send</param>
  228. /// <returns>Size of sent data</returns>
  229. public virtual long Send(string text) => Send(Encoding.UTF8.GetBytes(text));
  230. /// <summary>
  231. /// Send data to the client (asynchronous)
  232. /// </summary>
  233. /// <param name="buffer">Buffer to send</param>
  234. /// <returns>'true' if the data was successfully sent, 'false' if the session is not connected</returns>
  235. public virtual bool SendAsync(byte[] buffer) => SendAsync(buffer.AsSpan());
  236. /// <summary>
  237. /// Send data to the client (asynchronous)
  238. /// </summary>
  239. /// <param name="buffer">Buffer to send</param>
  240. /// <param name="offset">Buffer offset</param>
  241. /// <param name="size">Buffer size</param>
  242. /// <returns>'true' if the data was successfully sent, 'false' if the session is not connected</returns>
  243. public virtual bool SendAsync(byte[] buffer, long offset, long size) => SendAsync(buffer.AsSpan((int)offset, (int)size));
  244. /// <summary>
  245. /// Send data to the client (asynchronous)
  246. /// </summary>
  247. /// <param name="buffer">Buffer to send as a span of bytes</param>
  248. /// <returns>'true' if the data was successfully sent, 'false' if the session is not connected</returns>
  249. public virtual bool SendAsync(ReadOnlySpan<byte> buffer)
  250. {
  251. if (!IsConnected)
  252. return false;
  253. if (buffer.IsEmpty)
  254. return true;
  255. lock (_sendLock)
  256. {
  257. // Check the send buffer limit
  258. if (((_sendBufferMain.Size + buffer.Length) > OptionSendBufferLimit) && (OptionSendBufferLimit > 0))
  259. {
  260. SendError(SocketError.NoBufferSpaceAvailable);
  261. return false;
  262. }
  263. // Fill the main send buffer
  264. _sendBufferMain.Append(buffer);
  265. // Update statistic
  266. BytesPending = _sendBufferMain.Size;
  267. // Avoid multiple send handlers
  268. if (_sending)
  269. return true;
  270. else
  271. _sending = true;
  272. // Try to send the main buffer
  273. TrySend();
  274. }
  275. return true;
  276. }
  277. /// <summary>
  278. /// Send text to the client (asynchronous)
  279. /// </summary>
  280. /// <param name="text">Text string to send</param>
  281. /// <returns>'true' if the text was successfully sent, 'false' if the session is not connected</returns>
  282. public virtual bool SendAsync(string text) => SendAsync(Encoding.UTF8.GetBytes(text));
  283. /// <summary>
  284. /// Send text to the client (asynchronous)
  285. /// </summary>
  286. /// <param name="text">Text to send as a span of characters</param>
  287. /// <returns>'true' if the text was successfully sent, 'false' if the session is not connected</returns>
  288. public virtual bool SendAsync(ReadOnlySpan<char> text) => SendAsync(Encoding.UTF8.GetBytes(text.ToArray()));
  289. /// <summary>
  290. /// Receive data from the client (synchronous)
  291. /// </summary>
  292. /// <param name="buffer">Buffer to receive</param>
  293. /// <returns>Size of received data</returns>
  294. public virtual long Receive(byte[] buffer) { return Receive(buffer, 0, buffer.Length); }
  295. /// <summary>
  296. /// Receive data from the client (synchronous)
  297. /// </summary>
  298. /// <param name="buffer">Buffer to receive</param>
  299. /// <param name="offset">Buffer offset</param>
  300. /// <param name="size">Buffer size</param>
  301. /// <returns>Size of received data</returns>
  302. public virtual long Receive(byte[] buffer, long offset, long size)
  303. {
  304. if (!IsConnected)
  305. return 0;
  306. if (size == 0)
  307. return 0;
  308. // Receive data from the client
  309. long received = Socket.Receive(buffer, (int)offset, (int)size, SocketFlags.None, out SocketError ec);
  310. if (received > 0)
  311. {
  312. // Update statistic
  313. BytesReceived += received;
  314. Interlocked.Add(ref Server._bytesReceived, received);
  315. // Call the buffer received handler
  316. OnReceived(buffer, 0, received);
  317. }
  318. // Check for socket error
  319. if (ec != SocketError.Success)
  320. {
  321. SendError(ec);
  322. Disconnect();
  323. }
  324. return received;
  325. }
  326. /// <summary>
  327. /// Receive text from the client (synchronous)
  328. /// </summary>
  329. /// <param name="size">Text size to receive</param>
  330. /// <returns>Received text</returns>
  331. public virtual string Receive(long size)
  332. {
  333. var buffer = new byte[size];
  334. var length = Receive(buffer);
  335. return Encoding.UTF8.GetString(buffer, 0, (int)length);
  336. }
  337. /// <summary>
  338. /// Receive data from the client (asynchronous)
  339. /// </summary>
  340. public virtual void ReceiveAsync()
  341. {
  342. // Try to receive data from the client
  343. TryReceive();
  344. }
  345. /// <summary>
  346. /// Try to receive new data
  347. /// </summary>
  348. private void TryReceive()
  349. {
  350. if (_receiving)
  351. return;
  352. if (!IsConnected)
  353. return;
  354. bool process = true;
  355. while (process)
  356. {
  357. process = false;
  358. try
  359. {
  360. // Async receive with the receive handler
  361. _receiving = true;
  362. _receiveEventArg.SetBuffer(_receiveBuffer.Data, 0, (int)_receiveBuffer.Capacity);
  363. if (!Socket.ReceiveAsync(_receiveEventArg))
  364. process = ProcessReceive(_receiveEventArg);
  365. }
  366. catch (ObjectDisposedException) {}
  367. }
  368. }
  369. /// <summary>
  370. /// Try to send pending data
  371. /// </summary>
  372. private void TrySend()
  373. {
  374. if (!IsConnected)
  375. return;
  376. bool empty = false;
  377. bool process = true;
  378. while (process)
  379. {
  380. process = false;
  381. lock (_sendLock)
  382. {
  383. // Is previous socket send in progress?
  384. if (_sendBufferFlush.IsEmpty)
  385. {
  386. // Swap flush and main buffers
  387. _sendBufferFlush = Interlocked.Exchange(ref _sendBufferMain, _sendBufferFlush);
  388. _sendBufferFlushOffset = 0;
  389. // Update statistic
  390. BytesPending = 0;
  391. BytesSending += _sendBufferFlush.Size;
  392. // Check if the flush buffer is empty
  393. if (_sendBufferFlush.IsEmpty)
  394. {
  395. // Need to call empty send buffer handler
  396. empty = true;
  397. // End sending process
  398. _sending = false;
  399. }
  400. }
  401. else
  402. return;
  403. }
  404. // Call the empty send buffer handler
  405. if (empty)
  406. {
  407. OnEmpty();
  408. return;
  409. }
  410. try
  411. {
  412. // Async write with the write handler
  413. _sendEventArg.SetBuffer(_sendBufferFlush.Data, (int)_sendBufferFlushOffset, (int)(_sendBufferFlush.Size - _sendBufferFlushOffset));
  414. if (!Socket.SendAsync(_sendEventArg))
  415. process = ProcessSend(_sendEventArg);
  416. }
  417. catch (ObjectDisposedException) {}
  418. }
  419. }
  420. /// <summary>
  421. /// Clear send/receive buffers
  422. /// </summary>
  423. private void ClearBuffers()
  424. {
  425. lock (_sendLock)
  426. {
  427. // Clear send buffers
  428. _sendBufferMain.Clear();
  429. _sendBufferFlush.Clear();
  430. _sendBufferFlushOffset= 0;
  431. // Update statistic
  432. BytesPending = 0;
  433. BytesSending = 0;
  434. }
  435. }
  436. #endregion
  437. #region IO processing
  438. /// <summary>
  439. /// This method is called whenever a receive or send operation is completed on a socket
  440. /// </summary>
  441. private void OnAsyncCompleted(object sender, SocketAsyncEventArgs e)
  442. {
  443. if (IsSocketDisposed)
  444. return;
  445. // Determine which type of operation just completed and call the associated handler
  446. switch (e.LastOperation)
  447. {
  448. case SocketAsyncOperation.Receive:
  449. if (ProcessReceive(e))
  450. TryReceive();
  451. break;
  452. case SocketAsyncOperation.Send:
  453. if (ProcessSend(e))
  454. TrySend();
  455. break;
  456. default:
  457. throw new ArgumentException("The last operation completed on the socket was not a receive or send");
  458. }
  459. }
  460. /// <summary>
  461. /// This method is invoked when an asynchronous receive operation completes
  462. /// </summary>
  463. private bool ProcessReceive(SocketAsyncEventArgs e)
  464. {
  465. if (!IsConnected)
  466. return false;
  467. long size = e.BytesTransferred;
  468. // Received some data from the client
  469. if (size > 0)
  470. {
  471. // Update statistic
  472. BytesReceived += size;
  473. Interlocked.Add(ref Server._bytesReceived, size);
  474. // Call the buffer received handler
  475. OnReceived(_receiveBuffer.Data, 0, size);
  476. // If the receive buffer is full increase its size
  477. if (_receiveBuffer.Capacity == size)
  478. {
  479. // Check the receive buffer limit
  480. if (((2 * size) > OptionReceiveBufferLimit) && (OptionReceiveBufferLimit > 0))
  481. {
  482. SendError(SocketError.NoBufferSpaceAvailable);
  483. Disconnect();
  484. return false;
  485. }
  486. _receiveBuffer.Reserve(2 * size);
  487. }
  488. }
  489. _receiving = false;
  490. // Try to receive again if the session is valid
  491. if (e.SocketError == SocketError.Success)
  492. {
  493. // If zero is returned from a read operation, the remote end has closed the connection
  494. if (size > 0)
  495. return true;
  496. else
  497. Disconnect();
  498. }
  499. else
  500. {
  501. SendError(e.SocketError);
  502. Disconnect();
  503. }
  504. return false;
  505. }
  506. /// <summary>
  507. /// This method is invoked when an asynchronous send operation completes
  508. /// </summary>
  509. private bool ProcessSend(SocketAsyncEventArgs e)
  510. {
  511. if (!IsConnected)
  512. return false;
  513. long size = e.BytesTransferred;
  514. // Send some data to the client
  515. if (size > 0)
  516. {
  517. // Update statistic
  518. BytesSending -= size;
  519. BytesSent += size;
  520. Interlocked.Add(ref Server._bytesSent, size);
  521. // Increase the flush buffer offset
  522. _sendBufferFlushOffset += size;
  523. // Successfully send the whole flush buffer
  524. if (_sendBufferFlushOffset == _sendBufferFlush.Size)
  525. {
  526. // Clear the flush buffer
  527. _sendBufferFlush.Clear();
  528. _sendBufferFlushOffset = 0;
  529. }
  530. // Call the buffer sent handler
  531. OnSent(size, BytesPending + BytesSending);
  532. }
  533. // Try to send again if the session is valid
  534. if (e.SocketError == SocketError.Success)
  535. return true;
  536. else
  537. {
  538. SendError(e.SocketError);
  539. Disconnect();
  540. return false;
  541. }
  542. }
  543. #endregion
  544. #region Session handlers
  545. /// <summary>
  546. /// Handle client connecting notification
  547. /// </summary>
  548. protected virtual void OnConnecting() {}
  549. /// <summary>
  550. /// Handle client connected notification
  551. /// </summary>
  552. protected virtual void OnConnected() {}
  553. /// <summary>
  554. /// Handle client disconnecting notification
  555. /// </summary>
  556. protected virtual void OnDisconnecting() {}
  557. /// <summary>
  558. /// Handle client disconnected notification
  559. /// </summary>
  560. protected virtual void OnDisconnected() {}
  561. /// <summary>
  562. /// Handle buffer received notification
  563. /// </summary>
  564. /// <param name="buffer">Received buffer</param>
  565. /// <param name="offset">Received buffer offset</param>
  566. /// <param name="size">Received buffer size</param>
  567. /// <remarks>
  568. /// Notification is called when another part of buffer was received from the client
  569. /// </remarks>
  570. protected virtual void OnReceived(byte[] buffer, long offset, long size) {}
  571. /// <summary>
  572. /// Handle buffer sent notification
  573. /// </summary>
  574. /// <param name="sent">Size of sent buffer</param>
  575. /// <param name="pending">Size of pending buffer</param>
  576. /// <remarks>
  577. /// Notification is called when another part of buffer was sent to the client.
  578. /// This handler could be used to send another buffer to the client for instance when the pending size is zero.
  579. /// </remarks>
  580. protected virtual void OnSent(long sent, long pending) {}
  581. /// <summary>
  582. /// Handle empty send buffer notification
  583. /// </summary>
  584. /// <remarks>
  585. /// Notification is called when the send buffer is empty and ready for a new data to send.
  586. /// This handler could be used to send another buffer to the client.
  587. /// </remarks>
  588. protected virtual void OnEmpty() {}
  589. /// <summary>
  590. /// Handle error notification
  591. /// </summary>
  592. /// <param name="error">Socket error code</param>
  593. protected virtual void OnError(SocketError error) {}
  594. #endregion
  595. #region Error handling
  596. /// <summary>
  597. /// Send error notification
  598. /// </summary>
  599. /// <param name="error">Socket error code</param>
  600. private void SendError(SocketError error)
  601. {
  602. // Skip disconnect errors
  603. if ((error == SocketError.ConnectionAborted) ||
  604. (error == SocketError.ConnectionRefused) ||
  605. (error == SocketError.ConnectionReset) ||
  606. (error == SocketError.OperationAborted) ||
  607. (error == SocketError.Shutdown))
  608. return;
  609. OnError(error);
  610. }
  611. #endregion
  612. #region IDisposable implementation
  613. /// <summary>
  614. /// Disposed flag
  615. /// </summary>
  616. public bool IsDisposed { get; private set; }
  617. /// <summary>
  618. /// Session socket disposed flag
  619. /// </summary>
  620. public bool IsSocketDisposed { get; private set; } = true;
  621. // Implement IDisposable.
  622. public void Dispose()
  623. {
  624. Dispose(true);
  625. GC.SuppressFinalize(this);
  626. }
  627. protected virtual void Dispose(bool disposingManagedResources)
  628. {
  629. // The idea here is that Dispose(Boolean) knows whether it is
  630. // being called to do explicit cleanup (the Boolean is true)
  631. // versus being called due to a garbage collection (the Boolean
  632. // is false). This distinction is useful because, when being
  633. // disposed explicitly, the Dispose(Boolean) method can safely
  634. // execute code using reference type fields that refer to other
  635. // objects knowing for sure that these other objects have not been
  636. // finalized or disposed of yet. When the Boolean is false,
  637. // the Dispose(Boolean) method should not execute code that
  638. // refer to reference type fields because those objects may
  639. // have already been finalized."
  640. if (!IsDisposed)
  641. {
  642. if (disposingManagedResources)
  643. {
  644. // Dispose managed resources here...
  645. Disconnect();
  646. }
  647. // Dispose unmanaged resources here...
  648. // Set large fields to null here...
  649. // Mark as disposed.
  650. IsDisposed = true;
  651. }
  652. }
  653. #endregion
  654. }
  655. }