Session.NET.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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));
  57. if (buffer.Count > 1 && buffer[buffer.Count - 2] == 0x0D)
  58. response = encoding.GetString(buffer.Take(buffer.Count - 2).ToArray());
  59. else
  60. response = encoding.GetString(buffer.Take(buffer.Count - 1).ToArray());
  61. }
  62. partial void SocketRead(int length, ref byte[] buffer)
  63. {
  64. var offset = 0;
  65. int receivedTotal = 0; // how many bytes is already received
  66. do
  67. {
  68. try
  69. {
  70. var receivedBytes = this._socket.Receive(buffer, offset + receivedTotal, length - receivedTotal, SocketFlags.None);
  71. if (receivedBytes > 0)
  72. {
  73. receivedTotal += receivedBytes;
  74. continue;
  75. }
  76. else
  77. {
  78. throw new SshConnectionException("An established connection was aborted by the software in your host machine.", DisconnectReason.ConnectionLost);
  79. }
  80. }
  81. catch (SocketException exp)
  82. {
  83. if (exp.SocketErrorCode == SocketError.ConnectionAborted)
  84. {
  85. buffer = new byte[length];
  86. this.Disconnect();
  87. return;
  88. }
  89. else if (exp.SocketErrorCode == SocketError.WouldBlock ||
  90. exp.SocketErrorCode == SocketError.IOPending ||
  91. exp.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
  92. {
  93. // socket buffer is probably empty, wait and try again
  94. Thread.Sleep(30);
  95. }
  96. else
  97. throw; // any serious error occurred
  98. }
  99. } while (receivedTotal < length);
  100. }
  101. partial void SocketWrite(byte[] data)
  102. {
  103. int sent = 0; // how many bytes is already sent
  104. int length = data.Length;
  105. do
  106. {
  107. try
  108. {
  109. sent += this._socket.Send(data, sent, length - sent, SocketFlags.None);
  110. }
  111. catch (SocketException ex)
  112. {
  113. if (ex.SocketErrorCode == SocketError.WouldBlock ||
  114. ex.SocketErrorCode == SocketError.IOPending ||
  115. ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
  116. {
  117. // socket buffer is probably full, wait and try again
  118. Thread.Sleep(30);
  119. }
  120. else
  121. throw; // any serious error occurr
  122. }
  123. } while (sent < length);
  124. }
  125. partial void Log(string text)
  126. {
  127. this._log.TraceEvent(System.Diagnostics.TraceEventType.Verbose, 1, text);
  128. }
  129. }
  130. }