Session.SilverlightShared.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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.Collections.Generic;
  9. namespace Renci.SshNet
  10. {
  11. public partial class Session
  12. {
  13. private readonly AutoResetEvent _autoEvent = new AutoResetEvent(false);
  14. private readonly AutoResetEvent _sendEvent = new AutoResetEvent(false);
  15. private readonly AutoResetEvent _receiveEvent = new AutoResetEvent(false);
  16. private bool _isConnected;
  17. /// <summary>
  18. /// Gets a value indicating whether the socket is connected.
  19. /// </summary>
  20. /// <value>
  21. /// <c>true</c> if the socket is connected; otherwise, <c>false</c>.
  22. /// </value>
  23. partial void IsSocketConnected(ref bool isConnected)
  24. {
  25. isConnected = (this._socket != null && this._socket.Connected && this._isConnected);
  26. }
  27. partial void SocketConnect(string host, int port)
  28. {
  29. var ep = new DnsEndPoint(host, port);
  30. this._socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  31. var args = new SocketAsyncEventArgs();
  32. args.UserToken = this._socket;
  33. args.RemoteEndPoint = ep;
  34. args.Completed += OnConnect;
  35. this._socket.ConnectAsync(args);
  36. this._autoEvent.WaitOne(this.ConnectionInfo.Timeout);
  37. if (args.SocketError != SocketError.Success)
  38. throw new SocketException((int)args.SocketError);
  39. }
  40. partial void SocketDisconnect()
  41. {
  42. this._socket.Close(10000);
  43. }
  44. partial void SocketReadLine(ref string response)
  45. {
  46. var encoding = new ASCIIEncoding();
  47. // Read data one byte at a time to find end of line and leave any unhandled information in the buffer to be processed later
  48. var buffer = new List<byte>();
  49. var data = new byte[1];
  50. do
  51. {
  52. var args = new SocketAsyncEventArgs();
  53. args.SetBuffer(data, 0, data.Length);
  54. args.UserToken = this._socket;
  55. args.RemoteEndPoint = this._socket.RemoteEndPoint;
  56. args.Completed += OnReceive;
  57. this._socket.ReceiveAsync(args);
  58. if (!this._receiveEvent.WaitOne(this.ConnectionInfo.Timeout))
  59. throw new SshOperationTimeoutException("Socket read operation has timed out");
  60. // If zero bytes received then exit
  61. if (args.BytesTransferred == 0)
  62. break;
  63. buffer.Add(data[0]);
  64. }
  65. while (!(buffer.Count > 0 && (buffer[buffer.Count - 1] == 0x0A || buffer[buffer.Count - 1] == 0x00)));
  66. // Return an empty version string if the buffer consists of a 0x00 character.
  67. if (buffer.Count > 0 && buffer[buffer.Count - 1] == 0x00)
  68. {
  69. response = string.Empty;
  70. }
  71. else if (buffer.Count == 0)
  72. response = string.Empty;
  73. else if (buffer.Count > 1 && buffer[buffer.Count - 2] == 0x0D)
  74. response = encoding.GetString(buffer.ToArray(), 0, buffer.Count - 2);
  75. else
  76. response = encoding.GetString(buffer.ToArray(), 0, buffer.Count - 1);
  77. }
  78. partial void SocketRead(int length, ref byte[] buffer)
  79. {
  80. var receivedTotal = 0; // how many bytes is already received
  81. do
  82. {
  83. var args = new SocketAsyncEventArgs();
  84. args.SetBuffer(buffer, receivedTotal, length - receivedTotal);
  85. args.UserToken = this._socket;
  86. args.RemoteEndPoint = this._socket.RemoteEndPoint;
  87. args.Completed += OnReceive;
  88. this._socket.ReceiveAsync(args);
  89. this._receiveEvent.WaitOne(this.ConnectionInfo.Timeout);
  90. if (args.SocketError == SocketError.WouldBlock ||
  91. args.SocketError == SocketError.IOPending ||
  92. args.SocketError == SocketError.NoBufferSpaceAvailable)
  93. {
  94. // socket buffer is probably empty, wait and try again
  95. Thread.Sleep(30);
  96. continue;
  97. }
  98. if (args.SocketError != SocketError.Success)
  99. {
  100. throw new SocketException((int)args.SocketError);
  101. }
  102. var receivedBytes = args.BytesTransferred;
  103. if (receivedBytes > 0)
  104. {
  105. receivedTotal += receivedBytes;
  106. continue;
  107. }
  108. throw new SshConnectionException("An established connection was aborted by the software in your host machine.", DisconnectReason.ConnectionLost);
  109. } while (receivedTotal < length);
  110. }
  111. partial void SocketWrite(byte[] data)
  112. {
  113. if (this._isConnected)
  114. {
  115. var args = new SocketAsyncEventArgs();
  116. args.SetBuffer(data, 0, data.Length);
  117. args.UserToken = this._socket;
  118. args.RemoteEndPoint = this._socket.RemoteEndPoint;
  119. args.Completed += 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. }