100 if (sc == null && !ssc.isBlocking()) |
100 if (sc == null && !ssc.isBlocking()) |
101 throw new IllegalBlockingModeException(); |
101 throw new IllegalBlockingModeException(); |
102 return sc.socket(); |
102 return sc.socket(); |
103 } |
103 } |
104 |
104 |
105 // Implement timeout with a selector |
|
106 SelectionKey sk = null; |
|
107 Selector sel = null; |
|
108 ssc.configureBlocking(false); |
105 ssc.configureBlocking(false); |
109 try { |
106 try { |
110 SocketChannel sc; |
107 SocketChannel sc; |
111 if ((sc = ssc.accept()) != null) |
108 if ((sc = ssc.accept()) != null) |
112 return sc.socket(); |
109 return sc.socket(); |
113 sel = Util.getTemporarySelector(ssc); |
|
114 sk = ssc.register(sel, SelectionKey.OP_ACCEPT); |
|
115 long to = timeout; |
110 long to = timeout; |
116 for (;;) { |
111 for (;;) { |
117 if (!ssc.isOpen()) |
112 if (!ssc.isOpen()) |
118 throw new ClosedChannelException(); |
113 throw new ClosedChannelException(); |
119 long st = System.currentTimeMillis(); |
114 long st = System.currentTimeMillis(); |
120 int ns = sel.select(to); |
115 int result = ssc.poll(PollArrayWrapper.POLLIN, to); |
121 if (ns > 0 && |
116 if (result > 0 && ((sc = ssc.accept()) != null)) |
122 sk.isAcceptable() && ((sc = ssc.accept()) != null)) |
|
123 return sc.socket(); |
117 return sc.socket(); |
124 sel.selectedKeys().remove(sk); |
|
125 to -= System.currentTimeMillis() - st; |
118 to -= System.currentTimeMillis() - st; |
126 if (to <= 0) |
119 if (to <= 0) |
127 throw new SocketTimeoutException(); |
120 throw new SocketTimeoutException(); |
128 } |
121 } |
129 } finally { |
122 } finally { |
130 if (sk != null) |
|
131 sk.cancel(); |
|
132 if (ssc.isOpen()) |
123 if (ssc.isOpen()) |
133 ssc.configureBlocking(true); |
124 ssc.configureBlocking(true); |
134 if (sel != null) |
|
135 Util.releaseTemporarySelector(sel); |
|
136 } |
125 } |
137 |
126 |
138 } catch (Exception x) { |
127 } catch (Exception x) { |
139 Net.translateException(x); |
128 Net.translateException(x); |
140 assert false; |
129 assert false; |