Session.NET.cs 4.3 KB

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