GHSocketFactory.jvm.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Net;
  3. namespace System.Net.Sockets
  4. {
  5. /// <summary>
  6. /// Summary description for GHSocketFactory.
  7. /// </summary>
  8. public class GHSocketFactory
  9. {
  10. internal static GHSocket Socket_internal(AddressFamily family,
  11. SocketType type,
  12. ProtocolType proto,
  13. out int error)
  14. {
  15. if ( family == AddressFamily.InterNetwork &&
  16. //(family == AddressFamily.InterNetwork || family == AddressFamily.InterNetworkV6) &&
  17. (type == SocketType.Stream || type == SocketType.Unknown) &&
  18. (proto == ProtocolType.Tcp || proto == ProtocolType.Unknown || proto == ProtocolType.Unspecified) )
  19. {
  20. error = 0;
  21. return new GHStreamSocket();
  22. }
  23. error = 10044; //WSAESOCKTNOSUPPORT (Socket type not supported)
  24. return null;
  25. }
  26. internal static void Select_internal (ref Socket [] sockets, int microSeconds, out int error)
  27. {
  28. error = 0;
  29. java.nio.channels.Selector selector = java.nio.channels.Selector.open();
  30. int mode = 0;
  31. int count = sockets.Length;
  32. for (int i = 0; i < count; i++)
  33. {
  34. if (sockets [i] == null)
  35. { // separator
  36. mode++;
  37. continue;
  38. }
  39. GHSocket sock = sockets [i].GHHandle;
  40. if (sock == null)
  41. {
  42. throw new ArgumentNullException ("GHSocket handle is null");
  43. }
  44. sock.RegisterSelector(selector, mode, sockets [i], out error);
  45. if (error != 0)
  46. {
  47. error = 0;
  48. sockets = null;
  49. CloseSelector(selector);
  50. return;
  51. }
  52. }
  53. sockets = null;
  54. long timeOutMillis = 1;
  55. if (microSeconds < 0)
  56. {
  57. timeOutMillis = 0;
  58. }
  59. else if (microSeconds > 999)
  60. {
  61. timeOutMillis = (long)(microSeconds / 1000);
  62. }
  63. int readyCount = 0;
  64. try
  65. {
  66. readyCount = selector.select(timeOutMillis);
  67. }
  68. catch (Exception e)
  69. {
  70. error = 10022; //WSAEINVAL (Invalid argument)
  71. #if DEBUG
  72. Console.WriteLine("Caught exception during Select_internal selector.select - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
  73. #endif
  74. }
  75. if (readyCount > 0)
  76. {
  77. try
  78. {
  79. sockets = new Socket[readyCount+2];
  80. Socket[] writeList = new Socket[readyCount];
  81. Socket[] errorList = new Socket[readyCount];
  82. int readListCount = 0;
  83. int writeListCount = 0;
  84. int errorListCount = 0;
  85. java.util.Set readyKeys = selector.selectedKeys();
  86. java.util.Iterator it = readyKeys.iterator();
  87. while (it.hasNext())
  88. {
  89. java.nio.channels.SelectionKey key = (java.nio.channels.SelectionKey)it.next();
  90. if (key.isAcceptable() || key.isReadable())
  91. {
  92. sockets[readListCount] = (Socket)key.attachment();
  93. readListCount++;
  94. }
  95. if (key.isWritable())
  96. {
  97. writeList[writeListCount] = (Socket)key.attachment();
  98. writeListCount++;
  99. }
  100. if (key.isConnectable())
  101. {
  102. Socket source = (Socket)key.attachment();
  103. if (source.GHHandle.CheckConnectionFinished())
  104. {
  105. writeList[writeListCount] = source;
  106. writeListCount++;
  107. }
  108. else
  109. {
  110. errorList[errorListCount] = source;
  111. errorListCount++;
  112. }
  113. }
  114. }
  115. sockets[readListCount] = null;
  116. readListCount++;
  117. for (int i = 0; i < writeListCount; i++, readListCount++)
  118. {
  119. sockets[readListCount] = writeList[i];
  120. }
  121. sockets[readListCount] = null;
  122. readListCount++;
  123. for (int i = 0; i < errorListCount; i++, readListCount++)
  124. {
  125. sockets[readListCount] = errorList[i];
  126. }
  127. }
  128. catch (Exception e)
  129. {
  130. error = 10022; //WSAEINVAL (Invalid argument)
  131. #if DEBUG
  132. Console.WriteLine("Caught exception during Select_internal iterate selected keys - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
  133. #endif
  134. }
  135. }
  136. CloseSelector(selector);
  137. }
  138. internal static void CloseSelector (java.nio.channels.Selector selector)
  139. {
  140. java.util.Set keys = selector.keys();
  141. java.util.Iterator it = keys.iterator();
  142. try
  143. {
  144. selector.close();
  145. }
  146. catch (Exception e)
  147. {
  148. #if DEBUG
  149. Console.WriteLine("Caught exception during CloseSelector selector.close - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
  150. #endif
  151. }
  152. while (it.hasNext())
  153. {
  154. java.nio.channels.SelectionKey key = (java.nio.channels.SelectionKey)it.next();
  155. Socket source = (Socket)key.attachment();
  156. key.cancel ();
  157. try
  158. {
  159. if (source.Blocking)
  160. {
  161. /*
  162. A channel must be placed into non-blocking mode before being registered
  163. with a selector, and may not be returned to blocking mode until it has been
  164. deregistered. So, I need set the channel back to the blocking mode, if it was
  165. in blocking mode before select operation
  166. */
  167. source.Blocking = true;
  168. }
  169. }
  170. catch (Exception be)
  171. {
  172. #if DEBUG
  173. Console.WriteLine("Caught exception during CloseSelector source.Blocking - {0}: {1}\n{2}", be.GetType(), be.Message, be.StackTrace);
  174. #endif
  175. }
  176. }
  177. try
  178. {
  179. selector.close();
  180. }
  181. catch (Exception e)
  182. {
  183. #if DEBUG
  184. Console.WriteLine("Caught exception during CloseSelector selector.close - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
  185. #endif
  186. }
  187. }
  188. }
  189. }