Session.SilverlightShared.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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()
  27. {
  28. var ep = new DnsEndPoint(this.ConnectionInfo.Host, this.ConnectionInfo.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. 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. var buffer = new byte[1024];
  46. StringBuilder sb = new StringBuilder();
  47. do
  48. {
  49. SocketAsyncEventArgs args = new SocketAsyncEventArgs();
  50. args.SetBuffer(buffer, 0, buffer.Length);
  51. args.UserToken = this._socket;
  52. args.RemoteEndPoint = this._socket.RemoteEndPoint;
  53. args.Completed += new EventHandler<SocketAsyncEventArgs>(OnReceive);
  54. this._socket.ReceiveAsync(args);
  55. this.receiveEvent.WaitOne(this.ConnectionInfo.Timeout);
  56. sb.Append(Encoding.UTF8.GetString(buffer, 0, args.BytesTransferred));
  57. if (args.BytesTransferred < buffer.Length)
  58. break;
  59. } while (true);
  60. response = sb.ToString();
  61. }
  62. partial void SocketRead(int length, ref byte[] buffer)
  63. {
  64. var offset = 0;
  65. int receivedTotal = 0; // how many bytes is already received
  66. do
  67. {
  68. SocketAsyncEventArgs args = new SocketAsyncEventArgs();
  69. args.SetBuffer(buffer, offset + receivedTotal, length - receivedTotal);
  70. args.UserToken = this._socket;
  71. args.RemoteEndPoint = this._socket.RemoteEndPoint;
  72. args.Completed += new EventHandler<SocketAsyncEventArgs>(OnReceive);
  73. this._socket.ReceiveAsync(args);
  74. this.receiveEvent.WaitOne(this.ConnectionInfo.Timeout);
  75. if (args.SocketError == SocketError.WouldBlock ||
  76. args.SocketError == SocketError.IOPending ||
  77. args.SocketError == SocketError.NoBufferSpaceAvailable)
  78. {
  79. // socket buffer is probably empty, wait and try again
  80. Thread.Sleep(30);
  81. continue;
  82. }
  83. else if (args.SocketError != SocketError.Success)
  84. {
  85. throw new SocketException((int)args.SocketError);
  86. }
  87. var receivedBytes = args.BytesTransferred;
  88. if (receivedBytes > 0)
  89. {
  90. receivedTotal += receivedBytes;
  91. continue;
  92. }
  93. else
  94. {
  95. throw new SshConnectionException("An established connection was aborted by the software in your host machine.", DisconnectReason.ConnectionLost);
  96. }
  97. } while (receivedTotal < length);
  98. }
  99. partial void SocketWrite(byte[] data)
  100. {
  101. if (isConnected)
  102. {
  103. SocketAsyncEventArgs args = new SocketAsyncEventArgs();
  104. args.SetBuffer(data, 0, data.Length);
  105. args.UserToken = this._socket;
  106. args.RemoteEndPoint = this._socket.RemoteEndPoint;
  107. args.Completed += new EventHandler<SocketAsyncEventArgs>(OnSend);
  108. this._socket.SendAsync(args);
  109. }
  110. else
  111. throw new SocketException((int)SocketError.NotConnected);
  112. }
  113. private void OnConnect(object sender, SocketAsyncEventArgs e)
  114. {
  115. autoEvent.Set();
  116. isConnected = (e.SocketError == SocketError.Success);
  117. }
  118. private void OnSend(object sender, SocketAsyncEventArgs e)
  119. {
  120. this.sendEvent.Set();
  121. }
  122. private void OnReceive(object sender, SocketAsyncEventArgs e)
  123. {
  124. this.receiveEvent.Set();
  125. }
  126. partial void ExecuteThread(Action action)
  127. {
  128. ThreadPool.QueueUserWorkItem((o) => { action(); });
  129. }
  130. partial void InternalRegisterMessage(string messageName)
  131. {
  132. lock (this._messagesMetadata)
  133. {
  134. foreach (var item in from m in this._messagesMetadata where m.Name == messageName select m)
  135. {
  136. item.Enabled = true;
  137. item.Activated = true;
  138. }
  139. }
  140. }
  141. partial void InternalUnRegisterMessage(string messageName)
  142. {
  143. lock (this._messagesMetadata)
  144. {
  145. foreach (var item in from m in this._messagesMetadata where m.Name == messageName select m)
  146. {
  147. item.Enabled = false;
  148. item.Activated = false;
  149. }
  150. }
  151. }
  152. }
  153. }