Session.NET.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. using System.IO;
  10. using System.Diagnostics;
  11. using System.Text;
  12. using System.Collections.Generic;
  13. namespace Renci.SshNet
  14. {
  15. public partial class Session
  16. {
  17. private TraceSource _log =
  18. #if DEBUG
  19. new TraceSource("SshNet.Logging", SourceLevels.All);
  20. #else
  21. new TraceSource("SshNet.Logging");
  22. #endif
  23. partial void SocketConnect(string host, int port)
  24. {
  25. var ep = new IPEndPoint(Dns.GetHostAddresses(host)[0], port);
  26. this._socket = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  27. var socketBufferSize = 2 * MAXIMUM_PACKET_SIZE;
  28. this._socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
  29. this._socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, socketBufferSize);
  30. this._socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, socketBufferSize);
  31. this.Log(string.Format("Initiating connect to '{0}:{1}'.", this.ConnectionInfo.Host, this.ConnectionInfo.Port));
  32. // Connect socket with specified timeout
  33. var connectResult = this._socket.BeginConnect(ep, null, null);
  34. connectResult.AsyncWaitHandle.WaitOne(this.ConnectionInfo.Timeout, false);
  35. this._socket.EndConnect(connectResult);
  36. }
  37. partial void SocketDisconnect()
  38. {
  39. this._socket.Disconnect(true);
  40. }
  41. partial void SocketReadLine(ref string response)
  42. {
  43. var encoding = new Renci.SshNet.Common.ASCIIEncoding();
  44. var line = new StringBuilder();
  45. // Read data one byte at a time to find end of line and leave any unhandled information in the buffer to be processed later
  46. var buffer = new List<byte>();
  47. var data = new byte[1];
  48. do
  49. {
  50. var received = this._socket.Receive(data);
  51. // If zero bytes received then exit
  52. if (received == 0)
  53. break;
  54. buffer.Add(data[0]);
  55. }
  56. while (!(buffer.Count > 1 && (buffer[buffer.Count - 1] == 0x0A || buffer[buffer.Count - 1] == 0x00)));
  57. // Return an empty version string if the buffer consists of a 0x00 character.
  58. if (buffer[buffer.Count - 1] == 0x00)
  59. {
  60. response = string.Empty;
  61. }
  62. else if (buffer.Count > 1 && buffer[buffer.Count - 2] == 0x0D)
  63. response = encoding.GetString(buffer.Take(buffer.Count - 2).ToArray());
  64. else
  65. response = encoding.GetString(buffer.Take(buffer.Count - 1).ToArray());
  66. }
  67. partial void SocketRead(int length, ref byte[] buffer)
  68. {
  69. var offset = 0;
  70. int receivedTotal = 0; // how many bytes is already received
  71. do
  72. {
  73. try
  74. {
  75. var receivedBytes = this._socket.Receive(buffer, offset + receivedTotal, length - receivedTotal, SocketFlags.None);
  76. if (receivedBytes > 0)
  77. {
  78. receivedTotal += receivedBytes;
  79. continue;
  80. }
  81. else
  82. {
  83. throw new SshConnectionException("An established connection was aborted by the software in your host machine.", DisconnectReason.ConnectionLost);
  84. }
  85. }
  86. catch (SocketException exp)
  87. {
  88. if (exp.SocketErrorCode == SocketError.ConnectionAborted)
  89. {
  90. buffer = new byte[length];
  91. this.Disconnect();
  92. return;
  93. }
  94. else if (exp.SocketErrorCode == SocketError.WouldBlock ||
  95. exp.SocketErrorCode == SocketError.IOPending ||
  96. exp.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
  97. {
  98. // socket buffer is probably empty, wait and try again
  99. Thread.Sleep(30);
  100. }
  101. else
  102. throw; // any serious error occurred
  103. }
  104. } while (receivedTotal < length);
  105. }
  106. partial void SocketWrite(byte[] data)
  107. {
  108. int sent = 0; // how many bytes is already sent
  109. int length = data.Length;
  110. do
  111. {
  112. try
  113. {
  114. sent += this._socket.Send(data, sent, length - sent, SocketFlags.None);
  115. }
  116. catch (SocketException ex)
  117. {
  118. if (ex.SocketErrorCode == SocketError.WouldBlock ||
  119. ex.SocketErrorCode == SocketError.IOPending ||
  120. ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
  121. {
  122. // socket buffer is probably full, wait and try again
  123. Thread.Sleep(30);
  124. }
  125. else
  126. throw; // any serious error occurr
  127. }
  128. } while (sent < length);
  129. }
  130. partial void Log(string text)
  131. {
  132. this._log.TraceEvent(System.Diagnostics.TraceEventType.Verbose, 1, text);
  133. }
  134. }
  135. }