Session.SilverlightShared.cs 6.8 KB

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