Session.NET.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // Connect socket with specified timeout
  23. var connectResult = this._socket.BeginConnect(ep, null, null);
  24. connectResult.AsyncWaitHandle.WaitOne(this.ConnectionInfo.Timeout);
  25. this._socket.EndConnect(connectResult);
  26. }
  27. partial void SocketDisconnect()
  28. {
  29. this._socket.Disconnect(true);
  30. }
  31. partial void SocketReadLine(ref string response)
  32. {
  33. // Get server version from the server,
  34. // ignore text lines which are sent before if any
  35. using (var ns = new NetworkStream(this._socket))
  36. {
  37. using (var sr = new StreamReader(ns))
  38. {
  39. response = sr.ReadLine();
  40. }
  41. }
  42. }
  43. partial void SocketRead(int length, ref byte[] buffer)
  44. {
  45. var offset = 0;
  46. int receivedTotal = 0; // how many bytes is already received
  47. do
  48. {
  49. try
  50. {
  51. var receivedBytes = this._socket.Receive(buffer, offset + receivedTotal, length - receivedTotal, SocketFlags.None);
  52. if (receivedBytes > 0)
  53. {
  54. receivedTotal += receivedBytes;
  55. continue;
  56. }
  57. else
  58. {
  59. throw new SshConnectionException("An established connection was aborted by the software in your host machine.", DisconnectReason.ConnectionLost);
  60. }
  61. }
  62. catch (SocketException exp)
  63. {
  64. if (exp.SocketErrorCode == SocketError.WouldBlock ||
  65. exp.SocketErrorCode == SocketError.IOPending ||
  66. exp.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
  67. {
  68. // socket buffer is probably empty, wait and try again
  69. Thread.Sleep(30);
  70. }
  71. else
  72. throw; // any serious error occurred
  73. }
  74. } while (receivedTotal < length);
  75. }
  76. partial void SocketWrite(byte[] data)
  77. {
  78. int sent = 0; // how many bytes is already sent
  79. int length = data.Length;
  80. do
  81. {
  82. try
  83. {
  84. sent += this._socket.Send(data, sent, length - sent, SocketFlags.None);
  85. }
  86. catch (SocketException ex)
  87. {
  88. if (ex.SocketErrorCode == SocketError.WouldBlock ||
  89. ex.SocketErrorCode == SocketError.IOPending ||
  90. ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable)
  91. {
  92. // socket buffer is probably full, wait and try again
  93. Thread.Sleep(30);
  94. }
  95. else
  96. throw; // any serious error occurr
  97. }
  98. } while (sent < length);
  99. }
  100. }
  101. }