Session.NET.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. namespace Renci.SshNet
  12. {
  13. public partial class Session
  14. {
  15. private TraceSource _log =
  16. #if DEBUG
  17. new TraceSource("SshNet.Logging", SourceLevels.All);
  18. #else
  19. new TraceSource("SshNet.Logging");
  20. #endif
  21. partial void SocketConnect()
  22. {
  23. var ep = new IPEndPoint(Dns.GetHostAddresses(this.ConnectionInfo.Host)[0], this.ConnectionInfo.Port);
  24. this._socket = new Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  25. var socketBufferSize = 2 * MAXIMUM_PACKET_SIZE;
  26. this._socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
  27. this._socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendBuffer, socketBufferSize);
  28. this._socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveBuffer, socketBufferSize);
  29. this.Log(string.Format("Initiating connect to '{0}:{1}'.", this.ConnectionInfo.Host, this.ConnectionInfo.Port));
  30. // Connect socket with specified timeout
  31. var connectResult = this._socket.BeginConnect(ep, null, null);
  32. connectResult.AsyncWaitHandle.WaitOne(this.ConnectionInfo.Timeout, false);
  33. this._socket.EndConnect(connectResult);
  34. }
  35. partial void SocketDisconnect()
  36. {
  37. this._socket.Disconnect(true);
  38. }
  39. partial void SocketReadLine(ref string response)
  40. {
  41. // Get server version from the server,
  42. // ignore text lines which are sent before if any
  43. using (var ns = new NetworkStream(this._socket))
  44. {
  45. using (var sr = new StreamReader(ns))
  46. {
  47. response = sr.ReadLine();
  48. }
  49. }
  50. }
  51. partial void SocketRead(int length, ref byte[] buffer)
  52. {
  53. var offset = 0;
  54. int receivedTotal = 0; // how many bytes is already received
  55. do
  56. {
  57. try
  58. {
  59. var receivedBytes = this._socket.Receive(buffer, offset + receivedTotal, length - receivedTotal, SocketFlags.None);
  60. if (receivedBytes > 0)
  61. {
  62. receivedTotal += receivedBytes;
  63. continue;
  64. }
  65. else
  66. {
  67. throw new SshConnectionException("An established connection was aborted by the software in your host machine.", DisconnectReason.ConnectionLost);
  68. }
  69. }
  70. catch (SocketException exp)
  71. {
  72. if (exp.SocketErrorCode == SocketError.WouldBlock ||
  73. exp.SocketErrorCode == SocketError.IOPending ||
  74. exp.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
  75. {
  76. // socket buffer is probably empty, wait and try again
  77. Thread.Sleep(30);
  78. }
  79. else
  80. throw; // any serious error occurred
  81. }
  82. } while (receivedTotal < length);
  83. }
  84. partial void SocketWrite(byte[] data)
  85. {
  86. int sent = 0; // how many bytes is already sent
  87. int length = data.Length;
  88. do
  89. {
  90. try
  91. {
  92. sent += this._socket.Send(data, sent, length - sent, SocketFlags.None);
  93. }
  94. catch (SocketException ex)
  95. {
  96. if (ex.SocketErrorCode == SocketError.WouldBlock ||
  97. ex.SocketErrorCode == SocketError.IOPending ||
  98. ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
  99. {
  100. // socket buffer is probably full, wait and try again
  101. Thread.Sleep(30);
  102. }
  103. else
  104. throw; // any serious error occurr
  105. }
  106. } while (sent < length);
  107. }
  108. partial void Log(string text)
  109. {
  110. this._log.TraceEvent(System.Diagnostics.TraceEventType.Verbose, 1, text);
  111. }
  112. }
  113. }