Session.SilverlightShared.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System;
  2. using System.Linq;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Threading;
  6. using Renci.SshNet.Common;
  7. using Renci.SshNet.Messages.Transport;
  8. using System.Text;
  9. using System.Collections.Generic;
  10. namespace Renci.SshNet
  11. {
  12. public partial class Session
  13. {
  14. private readonly AutoResetEvent _autoEvent = new AutoResetEvent(false);
  15. private readonly AutoResetEvent _sendEvent = new AutoResetEvent(false);
  16. private readonly AutoResetEvent _receiveEvent = new AutoResetEvent(false);
  17. private bool _isConnected = false;
  18. partial void IsSocketConnected(ref bool isConnected)
  19. {
  20. isConnected = (!this._isDisconnecting && this._socket != null && this._socket.Connected && this._isAuthenticated && this._messageListenerCompleted != null && this._isConnected);
  21. }
  22. partial void SocketConnect(string host, int port)
  23. {
  24. var ep = new DnsEndPoint(host, port);
  25. this._socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  26. var args = new SocketAsyncEventArgs();
  27. args.UserToken = this._socket;
  28. args.RemoteEndPoint = ep;
  29. args.Completed += new EventHandler<SocketAsyncEventArgs>(OnConnect);
  30. this._socket.ConnectAsync(args);
  31. this._autoEvent.WaitOne(this.ConnectionInfo.Timeout);
  32. if (args.SocketError != SocketError.Success)
  33. throw new SocketException((int)args.SocketError);
  34. }
  35. partial void SocketDisconnect()
  36. {
  37. this._socket.Close(10000);
  38. }
  39. partial void SocketReadLine(ref string response)
  40. {
  41. var encoding = new Renci.SshNet.Common.ASCIIEncoding();
  42. var line = new StringBuilder();
  43. // Read data one byte at a time to find end of line and leave any unhandled information in the buffer to be processed later
  44. var buffer = new List<byte>();
  45. var data = new byte[1];
  46. do
  47. {
  48. SocketAsyncEventArgs args = new SocketAsyncEventArgs();
  49. args.SetBuffer(data, 0, data.Length);
  50. args.UserToken = this._socket;
  51. args.RemoteEndPoint = this._socket.RemoteEndPoint;
  52. args.Completed += new EventHandler<SocketAsyncEventArgs>(OnReceive);
  53. this._socket.ReceiveAsync(args);
  54. if (!this._receiveEvent.WaitOne(this.ConnectionInfo.Timeout))
  55. throw new SshOperationTimeoutException("Socket read operation has timed out");
  56. // If zero bytes received then exit
  57. if (args.BytesTransferred == 0)
  58. break;
  59. buffer.Add(data[0]);
  60. }
  61. while (!(buffer.Count > 0 && (buffer[buffer.Count - 1] == 0x0A || buffer[buffer.Count - 1] == 0x00)));
  62. // Return an empty version string if the buffer consists of a 0x00 character.
  63. if (buffer.Count > 0 && buffer[buffer.Count - 1] == 0x00)
  64. {
  65. response = string.Empty;
  66. }
  67. else if (buffer.Count == 0)
  68. response = string.Empty;
  69. else if (buffer.Count > 1 && buffer[buffer.Count - 2] == 0x0D)
  70. response = encoding.GetString(buffer.ToArray(), 0, buffer.Count - 2);
  71. else
  72. response = encoding.GetString(buffer.ToArray(), 0, buffer.Count - 1);
  73. }
  74. partial void SocketRead(int length, ref byte[] buffer)
  75. {
  76. var offset = 0;
  77. int receivedTotal = 0; // how many bytes is already received
  78. do
  79. {
  80. SocketAsyncEventArgs args = new SocketAsyncEventArgs();
  81. args.SetBuffer(buffer, offset + receivedTotal, length - receivedTotal);
  82. args.UserToken = this._socket;
  83. args.RemoteEndPoint = this._socket.RemoteEndPoint;
  84. args.Completed += new EventHandler<SocketAsyncEventArgs>(OnReceive);
  85. this._socket.ReceiveAsync(args);
  86. this._receiveEvent.WaitOne(this.ConnectionInfo.Timeout);
  87. if (args.SocketError == SocketError.WouldBlock ||
  88. args.SocketError == SocketError.IOPending ||
  89. args.SocketError == SocketError.NoBufferSpaceAvailable)
  90. {
  91. // socket buffer is probably empty, wait and try again
  92. Thread.Sleep(30);
  93. continue;
  94. }
  95. else if (args.SocketError != SocketError.Success)
  96. {
  97. throw new SocketException((int)args.SocketError);
  98. }
  99. var receivedBytes = args.BytesTransferred;
  100. if (receivedBytes > 0)
  101. {
  102. receivedTotal += receivedBytes;
  103. continue;
  104. }
  105. else
  106. {
  107. throw new SshConnectionException("An established connection was aborted by the software in your host machine.", DisconnectReason.ConnectionLost);
  108. }
  109. } while (receivedTotal < length);
  110. }
  111. partial void SocketWrite(byte[] data)
  112. {
  113. if (this._isConnected)
  114. {
  115. SocketAsyncEventArgs args = new SocketAsyncEventArgs();
  116. args.SetBuffer(data, 0, data.Length);
  117. args.UserToken = this._socket;
  118. args.RemoteEndPoint = this._socket.RemoteEndPoint;
  119. args.Completed += new EventHandler<SocketAsyncEventArgs>(OnSend);
  120. this._socket.SendAsync(args);
  121. }
  122. else
  123. throw new SocketException((int)SocketError.NotConnected);
  124. }
  125. private void OnConnect(object sender, SocketAsyncEventArgs e)
  126. {
  127. this._autoEvent.Set();
  128. this._isConnected = (e.SocketError == SocketError.Success);
  129. }
  130. private void OnSend(object sender, SocketAsyncEventArgs e)
  131. {
  132. this._sendEvent.Set();
  133. }
  134. private void OnReceive(object sender, SocketAsyncEventArgs e)
  135. {
  136. this._receiveEvent.Set();
  137. }
  138. partial void ExecuteThread(Action action)
  139. {
  140. ThreadPool.QueueUserWorkItem((o) => { action(); });
  141. }
  142. partial void InternalRegisterMessage(string messageName)
  143. {
  144. lock (this._messagesMetadata)
  145. {
  146. foreach (var item in from m in this._messagesMetadata where m.Name == messageName select m)
  147. {
  148. item.Enabled = true;
  149. item.Activated = true;
  150. }
  151. }
  152. }
  153. partial void InternalUnRegisterMessage(string messageName)
  154. {
  155. lock (this._messagesMetadata)
  156. {
  157. foreach (var item in from m in this._messagesMetadata where m.Name == messageName select m)
  158. {
  159. item.Enabled = false;
  160. item.Activated = false;
  161. }
  162. }
  163. }
  164. }
  165. }