Session.NET35.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System.Linq;
  2. using System;
  3. using System.Net.Sockets;
  4. using System.Net;
  5. using Renci.SshNet.Messages;
  6. using Renci.SshNet.Common;
  7. using System.Threading;
  8. using Renci.SshNet.Messages.Transport;
  9. namespace Renci.SshNet
  10. {
  11. /// <summary>
  12. /// Provides functionality to connect and interact with SSH server.
  13. /// </summary>
  14. public partial class Session
  15. {
  16. partial void ExecuteThread(Action action)
  17. {
  18. ThreadPool.QueueUserWorkItem((o) => { action(); });
  19. }
  20. partial void InternalRegisterMessage(string messageName)
  21. {
  22. lock (this._messagesMetadata)
  23. {
  24. foreach (var m in from m in this._messagesMetadata where m.Name == messageName select m)
  25. {
  26. m.Enabled = true;
  27. m.Activated = true;
  28. }
  29. }
  30. }
  31. partial void InternalUnRegisterMessage(string messageName)
  32. {
  33. lock (this._messagesMetadata)
  34. {
  35. foreach (var m in from m in this._messagesMetadata where m.Name == messageName select m)
  36. {
  37. m.Enabled = false;
  38. m.Activated = false;
  39. }
  40. }
  41. }
  42. partial void OpenSocket()
  43. {
  44. var ep = new IPEndPoint(Dns.GetHostAddresses(this.ConnectionInfo.Host)[0], this.ConnectionInfo.Port);
  45. this._socket = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  46. var socketBufferSize = 2 * MAXIMUM_PACKET_SIZE;
  47. this._socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
  48. this._socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, socketBufferSize);
  49. this._socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, socketBufferSize);
  50. // Connect socket with 5 seconds timeout
  51. var connectResult = this._socket.BeginConnect(ep, null, null);
  52. connectResult.AsyncWaitHandle.WaitOne(this.ConnectionInfo.Timeout);
  53. // Build list of available messages while connecting
  54. this._messagesMetadata = (from type in this.GetType().Assembly.GetTypes()
  55. from messageAttribute in type.GetCustomAttributes(false).OfType<MessageAttribute>()
  56. select new MessageMetadata
  57. {
  58. Name = messageAttribute.Name,
  59. Number = messageAttribute.Number,
  60. Enabled = false,
  61. Activated = false,
  62. Type = type,
  63. }).ToList();
  64. this._socket.EndConnect(connectResult);
  65. }
  66. partial void InternalRead(int length, ref byte[] buffer)
  67. {
  68. var offset = 0;
  69. int receivedTotal = 0; // how many bytes is already received
  70. do
  71. {
  72. try
  73. {
  74. var receivedBytes = this._socket.Receive(buffer, offset + receivedTotal, length - receivedTotal, SocketFlags.None);
  75. if (receivedBytes > 0)
  76. {
  77. receivedTotal += receivedBytes;
  78. continue;
  79. }
  80. else
  81. {
  82. throw new SshConnectionException("An established connection was aborted by the software in your host machine.", DisconnectReason.ConnectionLost);
  83. }
  84. }
  85. catch (SocketException exp)
  86. {
  87. if (exp.SocketErrorCode == SocketError.WouldBlock ||
  88. exp.SocketErrorCode == SocketError.IOPending ||
  89. exp.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
  90. {
  91. // socket buffer is probably empty, wait and try again
  92. Thread.Sleep(30);
  93. }
  94. else
  95. throw; // any serious error occurred
  96. }
  97. } while (receivedTotal < length);
  98. }
  99. partial void Write(byte[] data)
  100. {
  101. int sent = 0; // how many bytes is already sent
  102. int length = data.Length;
  103. do
  104. {
  105. try
  106. {
  107. sent += this._socket.Send(data, sent, length - sent, SocketFlags.None);
  108. }
  109. catch (SocketException ex)
  110. {
  111. if (ex.SocketErrorCode == SocketError.WouldBlock ||
  112. ex.SocketErrorCode == SocketError.IOPending ||
  113. ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
  114. {
  115. // socket buffer is probably full, wait and try again
  116. Thread.Sleep(30);
  117. }
  118. else
  119. throw; // any serious error occurr
  120. }
  121. } while (sent < length);
  122. }
  123. }
  124. }