Session.SilverlightShared.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. 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. partial void IsSocketConnected(ref bool isConnected)
  18. {
  19. isConnected = (this._socket != null && this._socket.Connected && _isConnected);
  20. }
  21. partial void SocketConnect(string host, int port)
  22. {
  23. var ep = new DnsEndPoint(host, port);
  24. this._socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  25. var args = new SocketAsyncEventArgs();
  26. args.UserToken = this._socket;
  27. args.RemoteEndPoint = ep;
  28. args.Completed += OnConnect;
  29. this._socket.ConnectAsync(args);
  30. this._autoEvent.WaitOne(this.ConnectionInfo.Timeout);
  31. if (args.SocketError != SocketError.Success)
  32. throw new SocketException((int)args.SocketError);
  33. }
  34. partial void SocketDisconnect()
  35. {
  36. this._socket.Close(10000);
  37. }
  38. partial void SocketReadLine(ref string response)
  39. {
  40. // TODO: Improve this function, currently will not work with server that send multiple lines as a first string
  41. var buffer = new byte[1024];
  42. var result = new StringBuilder();
  43. do
  44. {
  45. var args = new SocketAsyncEventArgs();
  46. args.SetBuffer(buffer, 0, buffer.Length);
  47. args.UserToken = this._socket;
  48. args.RemoteEndPoint = this._socket.RemoteEndPoint;
  49. args.Completed += OnReceive;
  50. this._socket.ReceiveAsync(args);
  51. this._receiveEvent.WaitOne(this.ConnectionInfo.Timeout);
  52. var lastChar = (char)buffer[0];
  53. for (var i = 1; i < args.BytesTransferred; i++)
  54. {
  55. var newChar = (char)buffer[i];
  56. if (lastChar == '\r' && newChar == '\n')
  57. break;
  58. result.Append(lastChar);
  59. lastChar = newChar;
  60. }
  61. if (args.BytesTransferred < buffer.Length)
  62. break;
  63. } while (true);
  64. response = result.ToString();
  65. }
  66. partial void SocketRead(int length, ref byte[] buffer)
  67. {
  68. var receivedTotal = 0; // how many bytes is already received
  69. do
  70. {
  71. var args = new SocketAsyncEventArgs();
  72. args.SetBuffer(buffer, receivedTotal, length - receivedTotal);
  73. args.UserToken = this._socket;
  74. args.RemoteEndPoint = this._socket.RemoteEndPoint;
  75. args.Completed += OnReceive;
  76. this._socket.ReceiveAsync(args);
  77. this._receiveEvent.WaitOne(this.ConnectionInfo.Timeout);
  78. if (args.SocketError == SocketError.WouldBlock ||
  79. args.SocketError == SocketError.IOPending ||
  80. args.SocketError == SocketError.NoBufferSpaceAvailable)
  81. {
  82. // socket buffer is probably empty, wait and try again
  83. Thread.Sleep(30);
  84. continue;
  85. }
  86. else if (args.SocketError != SocketError.Success)
  87. {
  88. throw new SocketException((int)args.SocketError);
  89. }
  90. var receivedBytes = args.BytesTransferred;
  91. if (receivedBytes > 0)
  92. {
  93. receivedTotal += receivedBytes;
  94. continue;
  95. }
  96. throw new SshConnectionException(
  97. "An established connection was aborted by the software in your host machine.",
  98. DisconnectReason.ConnectionLost);
  99. } while (receivedTotal < length);
  100. }
  101. partial void SocketWrite(byte[] data)
  102. {
  103. if (this._isConnected)
  104. {
  105. var args = new SocketAsyncEventArgs();
  106. args.SetBuffer(data, 0, data.Length);
  107. args.UserToken = this._socket;
  108. args.RemoteEndPoint = this._socket.RemoteEndPoint;
  109. args.Completed += OnSend;
  110. this._socket.SendAsync(args);
  111. }
  112. else
  113. throw new SocketException((int)SocketError.NotConnected);
  114. }
  115. private void OnConnect(object sender, SocketAsyncEventArgs e)
  116. {
  117. this._autoEvent.Set();
  118. this._isConnected = (e.SocketError == SocketError.Success);
  119. }
  120. private void OnSend(object sender, SocketAsyncEventArgs e)
  121. {
  122. this._sendEvent.Set();
  123. }
  124. private void OnReceive(object sender, SocketAsyncEventArgs e)
  125. {
  126. this._receiveEvent.Set();
  127. }
  128. partial void ExecuteThread(Action action)
  129. {
  130. ThreadPool.QueueUserWorkItem(o => action());
  131. }
  132. partial void InternalRegisterMessage(string messageName)
  133. {
  134. lock (this._messagesMetadata)
  135. {
  136. foreach (var item in from m in this._messagesMetadata where m.Name == messageName select m)
  137. {
  138. item.Enabled = true;
  139. item.Activated = true;
  140. }
  141. }
  142. }
  143. partial void InternalUnRegisterMessage(string messageName)
  144. {
  145. lock (this._messagesMetadata)
  146. {
  147. foreach (var item in from m in this._messagesMetadata where m.Name == messageName select m)
  148. {
  149. item.Enabled = false;
  150. item.Activated = false;
  151. }
  152. }
  153. }
  154. }
  155. }