Session.NET.cs 5.1 KB

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