4 namespace System.Net.Sockets
\r
7 /// Summary description for GHSocketFactory.
\r
9 public class GHSocketFactory
\r
11 internal static GHSocket Socket_internal(AddressFamily family,
\r
16 if ( family == AddressFamily.InterNetwork &&
\r
17 //(family == AddressFamily.InterNetwork || family == AddressFamily.InterNetworkV6) &&
\r
18 (type == SocketType.Stream || type == SocketType.Unknown) &&
\r
19 (proto == ProtocolType.Tcp || proto == ProtocolType.Unknown || proto == ProtocolType.Unspecified) )
\r
22 return new GHStreamSocket();
\r
25 error = 10044; //WSAESOCKTNOSUPPORT (Socket type not supported)
\r
29 internal static void Select_internal (ref Socket [] sockets, int microSeconds, out int error)
\r
33 java.nio.channels.Selector selector = java.nio.channels.Selector.open();
\r
36 int count = sockets.Length;
\r
37 for (int i = 0; i < count; i++)
\r
39 if (sockets [i] == null)
\r
45 GHSocket sock = sockets [i].GHHandle;
\r
48 throw new ArgumentNullException ("GHSocket handle is null");
\r
51 sock.RegisterSelector(selector, mode, sockets [i], out error);
\r
56 CloseSelector(selector);
\r
63 long timeOutMillis = 1;
\r
64 if (microSeconds < 0)
\r
68 else if (microSeconds > 999)
\r
70 timeOutMillis = (long)(microSeconds / 1000);
\r
76 readyCount = selector.select(timeOutMillis);
\r
80 error = 10022; //WSAEINVAL (Invalid argument)
\r
82 Console.WriteLine("Caught exception during Select_internal selector.select - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
90 sockets = new Socket[readyCount+2];
\r
91 Socket[] writeList = new Socket[readyCount];
\r
92 Socket[] errorList = new Socket[readyCount];
\r
94 int readListCount = 0;
\r
95 int writeListCount = 0;
\r
96 int errorListCount = 0;
\r
98 java.util.Set readyKeys = selector.selectedKeys();
\r
99 java.util.Iterator it = readyKeys.iterator();
\r
101 while (it.hasNext())
\r
103 java.nio.channels.SelectionKey key = (java.nio.channels.SelectionKey)it.next();
\r
105 if (key.isAcceptable() || key.isReadable())
\r
107 sockets[readListCount] = (Socket)key.attachment();
\r
110 if (key.isWritable())
\r
112 writeList[writeListCount] = (Socket)key.attachment();
\r
115 if (key.isConnectable())
\r
117 Socket source = (Socket)key.attachment();
\r
118 if (source.GHHandle.CheckConnectionFinished())
\r
120 writeList[writeListCount] = source;
\r
125 errorList[errorListCount] = source;
\r
131 sockets[readListCount] = null;
\r
133 for (int i = 0; i < writeListCount; i++, readListCount++)
\r
135 sockets[readListCount] = writeList[i];
\r
137 sockets[readListCount] = null;
\r
139 for (int i = 0; i < errorListCount; i++, readListCount++)
\r
141 sockets[readListCount] = errorList[i];
\r
144 catch (Exception e)
\r
146 error = 10022; //WSAEINVAL (Invalid argument)
\r
148 Console.WriteLine("Caught exception during Select_internal iterate selected keys - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
153 CloseSelector(selector);
\r
156 internal static void CloseSelector (java.nio.channels.Selector selector)
\r
158 java.util.Set keys = selector.keys();
\r
159 java.util.Iterator it = keys.iterator();
\r
165 catch (Exception e)
\r
168 Console.WriteLine("Caught exception during CloseSelector selector.close - {0}: {1}\n{2}", e.GetType(), e.Message, e.StackTrace);
\r
172 while (it.hasNext())
\r
174 java.nio.channels.SelectionKey key = (java.nio.channels.SelectionKey)it.next();
\r
175 Socket source = (Socket)key.attachment();
\r
178 if (source.Blocking)
\r
181 A channel must be placed into non-blocking mode before being registered
\r
182 with a selector, and may not be returned to blocking mode until it has been
\r
183 deregistered. So, I need set the channel back to the blocking mode, if it was
\r
184 in blocking mode before select operation
\r
186 source.Blocking = true;
\r
189 catch (Exception be)
\r
192 Console.WriteLine("Caught exception during CloseSelector source.Blocking - {0}: {1}\n{2}", be.GetType(), be.Message, be.StackTrace);
\r