Produced by Araxis Merge on 9/25/2018 2:13:13 PM Central Daylight Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.
# | Location | File | Last Modified |
---|---|---|---|
1 | build 3.zip\build 3\MHLTH_YS_137_Source\JavaScript\resources\javaJDF-1.8.0\src\sun\nio\ch | DatagramChannelImpl.java | Mon Jan 22 14:46:54 2018 UTC |
2 | build 3.zip\build 3\MHLTH_YS_137_Source\JavaScript\resources\javaJDF-1.8.0\src\sun\nio\ch | DatagramChannelImpl.java | Wed Sep 12 17:45:41 2018 UTC |
Description | Between Files 1 and 2 |
|
---|---|---|
Text Blocks | Lines | |
Unchanged | 2 | 2304 |
Changed | 1 | 2 |
Inserted | 0 | 0 |
Removed | 0 | 0 |
Whitespace | |
---|---|
Character case | Differences in character case are significant |
Line endings | Differences in line endings (CR and LF characters) are ignored |
CR/LF characters | Not shown in the comparison detail |
No regular expressions were active.
1 | /* | |
2 | * Copyrig ht (c) 200 1, 2013, O racle and/ or its aff iliates. A ll rights reserved. | |
3 | * DO NOT ALTER OR R EMOVE COPY RIGHT NOTI CES OR THI S FILE HEA DER. | |
4 | * | |
5 | * This co de is free software; you can r edistribut e it and/o r modify i t | |
6 | * under t he terms o f the GNU General Pu blic Licen se version 2 only, a s | |
7 | * publish ed by the Free Softw are Founda tion. Ora cle design ates this | |
8 | * particu lar file a s subject to the "Cl asspath" e xception a s provided | |
9 | * by Orac le in the LICENSE fi le that ac companied this code. | |
10 | * | |
11 | * This co de is dist ributed in the hope that it wi ll be usef ul, but WI THOUT | |
12 | * ANY WAR RANTY; wit hout even the implie d warranty of MERCHA NTABILITY or | |
13 | * FITNESS FOR A PAR TICULAR PU RPOSE. Se e the GNU General Pu blic Licen se | |
14 | * version 2 for mor e details (a copy is included in the LIC ENSE file that | |
15 | * accompa nied this code). | |
16 | * | |
17 | * You sho uld have r eceived a copy of th e GNU Gene ral Public License v ersion | |
18 | * 2 along with this work; if not, write to the Fr ee Softwar e Foundati on, | |
19 | * Inc., 5 1 Franklin St, Fifth Floor, Bo ston, MA 0 2110-1301 USA. | |
20 | * | |
21 | * Please contact Or acle, 500 Oracle Par kway, Redw ood Shores , CA 94065 USA | |
22 | * or visi t www.orac le.com if you need a dditional informatio n or have any | |
23 | * questio ns. | |
24 | */ | |
25 | ||
26 | package su n.nio.ch; | |
27 | ||
28 | import jav a.io.FileD escriptor; | |
29 | import jav a.io.IOExc eption; | |
30 | import jav a.net.*; | |
31 | import jav a.nio.Byte Buffer; | |
32 | import jav a.nio.chan nels.*; | |
33 | import jav a.nio.chan nels.spi.* ; | |
34 | import jav a.util.*; | |
35 | import sun .net.Resou rceManager ; | |
36 | import sun .net.Exten dedOptions Impl; | |
37 | ||
38 | /** | |
39 | * An impl ementation of Datagr amChannels . | |
40 | */ | |
41 | ||
42 | class Data gramChanne lImpl | |
43 | extend s Datagram Channel | |
44 | implem ents SelCh Impl | |
45 | { | |
46 | ||
47 | // Use d to make native rea d and writ e calls | |
48 | privat e static N ativeDispa tcher nd = new Datag ramDispatc her(); | |
49 | ||
50 | // Our file desc riptor | |
51 | privat e final Fi leDescript or fd; | |
52 | ||
53 | // fd value need ed for dev /poll. Thi s value wi ll remain valid | |
54 | // eve n after th e value in the file descriptor object ha s been set to -1 | |
55 | privat e final in t fdVal; | |
56 | ||
57 | // The protocol family of the socket | |
58 | privat e final Pr otocolFami ly family; | |
59 | ||
60 | // IDs of native threads d oing reads and write s, for sig nalling | |
61 | privat e volatile long read erThread = 0; | |
62 | privat e volatile long writ erThread = 0; | |
63 | ||
64 | // Cac hed InetAd dress and port for u nconnected DatagramC hannels | |
65 | // use d by recei ve0 | |
66 | privat e InetAddr ess cached SenderInet Address; | |
67 | privat e int cach edSenderPo rt; | |
68 | ||
69 | // Loc k held by current re ading or c onnecting thread | |
70 | privat e final Ob ject readL ock = new Object(); | |
71 | ||
72 | // Loc k held by current wr iting or c onnecting thread | |
73 | privat e final Ob ject write Lock = new Object(); | |
74 | ||
75 | // Loc k held by any thread that modi fies the s tate field s declared below | |
76 | // DO NOT invoke a blockin g I/O oper ation whil e holding this lock! | |
77 | privat e final Ob ject state Lock = new Object(); | |
78 | ||
79 | // -- The follow ing fields are prote cted by st ateLock | |
80 | ||
81 | // Sta te (does n ot necessa rily incre ase monoto nically) | |
82 | privat e static f inal int S T_UNINITIA LIZED = -1 ; | |
83 | privat e static f inal int S T_UNCONNEC TED = 0; | |
84 | privat e static f inal int S T_CONNECTE D = 1; | |
85 | privat e static f inal int S T_KILLED = 2; | |
86 | privat e int stat e = ST_UNI NITIALIZED ; | |
87 | ||
88 | // Bin ding | |
89 | privat e InetSock etAddress localAddre ss; | |
90 | privat e InetSock etAddress remoteAddr ess; | |
91 | ||
92 | // Our socket ad aptor, if any | |
93 | privat e Datagram Socket soc ket; | |
94 | ||
95 | // Mul ticast sup port | |
96 | privat e Membersh ipRegistry registry; | |
97 | ||
98 | // set true when socket is bound and SO_REUSEA DDRESS is emulated | |
99 | privat e boolean reuseAddre ssEmulated ; | |
100 | ||
101 | // set true/fals e when soc ket is alr eady bound and SO_RE USEADDR is emulated | |
102 | privat e boolean isReuseAdd ress; | |
103 | ||
104 | // -- End of fie lds protec ted by sta teLock | |
105 | ||
106 | ||
107 | public DatagramC hannelImpl (SelectorP rovider sp ) | |
108 | th rows IOExc eption | |
109 | { | |
110 | su per(sp); | |
111 | Re sourceMana ger.before UdpCreate( ); | |
112 | tr y { | |
113 | this.fam ily = Net. isIPv6Avai lable() ? | |
114 | Stan dardProtoc olFamily.I NET6 : Sta ndardProto colFamily. INET; | |
115 | this.fd = Net.sock et(family, false); | |
116 | this.fdV al = IOUti l.fdVal(fd ); | |
117 | this.sta te = ST_UN CONNECTED; | |
118 | } catch (IOE xception i oe) { | |
119 | Resource Manager.af terUdpClos e(); | |
120 | throw io e; | |
121 | } | |
122 | } | |
123 | ||
124 | public DatagramC hannelImpl (SelectorP rovider sp , Protocol Family fam ily) | |
125 | th rows IOExc eption | |
126 | { | |
127 | su per(sp); | |
128 | if ((family != Standar dProtocolF amily.INET ) && | |
129 | (family != Standar dProtocolF amily.INET 6)) | |
130 | { | |
131 | if (fami ly == null ) | |
132 | thro w new Null PointerExc eption("'f amily' is null"); | |
133 | else | |
134 | thro w new Unsu pportedOpe rationExce ption("Pro tocol fami ly not sup ported"); | |
135 | } | |
136 | if (family = = Standard ProtocolFa mily.INET6 ) { | |
137 | if (!Net .isIPv6Ava ilable()) { | |
138 | thro w new Unsu pportedOpe rationExce ption("IPv 6 not avai lable"); | |
139 | } | |
140 | } | |
141 | th is.family = family; | |
142 | th is.fd = Ne t.socket(f amily, fal se); | |
143 | th is.fdVal = IOUtil.fd Val(fd); | |
144 | th is.state = ST_UNCONN ECTED; | |
145 | } | |
146 | ||
147 | public DatagramC hannelImpl (SelectorP rovider sp , FileDesc riptor fd) | |
148 | th rows IOExc eption | |
149 | { | |
150 | su per(sp); | |
151 | th is.family = Net.isIP v6Availabl e() ? | |
152 | Standard ProtocolFa mily.INET6 : Standar dProtocolF amily.INET ; | |
153 | th is.fd = fd ; | |
154 | th is.fdVal = IOUtil.fd Val(fd); | |
155 | th is.state = ST_UNCONN ECTED; | |
156 | th is.localAd dress = Ne t.localAdd ress(fd); | |
157 | } | |
158 | ||
159 | public DatagramS ocket sock et() { | |
160 | sy nchronized (stateLoc k) { | |
161 | if (sock et == null ) | |
162 | sock et = Datag ramSocketA daptor.cre ate(this); | |
163 | return s ocket; | |
164 | } | |
165 | } | |
166 | ||
167 | @Overr ide | |
168 | public SocketAdd ress getLo calAddress () throws IOExceptio n { | |
169 | sy nchronized (stateLoc k) { | |
170 | if (!isO pen()) | |
171 | thro w new Clos edChannelE xception() ; | |
172 | // Perfo rm securit y check be fore retur ning addre ss | |
173 | return N et.getReve aledLocalA ddress(loc alAddress) ; | |
174 | } | |
175 | } | |
176 | ||
177 | @Overr ide | |
178 | public SocketAdd ress getRe moteAddres s() throws IOExcepti on { | |
179 | sy nchronized (stateLoc k) { | |
180 | if (!isO pen()) | |
181 | thro w new Clos edChannelE xception() ; | |
182 | return r emoteAddre ss; | |
183 | } | |
184 | } | |
185 | ||
186 | @Overr ide | |
187 | public <T> Datag ramChannel setOption (SocketOpt ion<T> nam e, T value ) | |
188 | th rows IOExc eption | |
189 | { | |
190 | if (name == null) | |
191 | throw ne w NullPoin terExcepti on(); | |
192 | if (!support edOptions( ).contains (name)) | |
193 | throw ne w Unsuppor tedOperati onExceptio n("'" + na me + "' no t supporte d"); | |
194 | ||
195 | sy nchronized (stateLoc k) { | |
196 | ensureOp en(); | |
197 | ||
198 | if (name == Standa rdSocketOp tions.IP_T OS || | |
199 | name == Standa rdSocketOp tions.IP_M ULTICAST_T TL || | |
200 | name == Standa rdSocketOp tions.IP_M ULTICAST_L OOP) | |
201 | { | |
202 | // o ptions are protocol dependent | |
203 | Net. setSocketO ption(fd, family, na me, value) ; | |
204 | retu rn this; | |
205 | } | |
206 | ||
207 | if (name == Standa rdSocketOp tions.IP_M ULTICAST_I F) { | |
208 | if ( value == n ull) | |
209 | throw new IllegalArg umentExcep tion("Cann ot set IP_ MULTICAST_ IF to 'nul l'"); | |
210 | Netw orkInterfa ce interf = (Network Interface) value; | |
211 | if ( family == StandardPr otocolFami ly.INET6) { | |
212 | int index = interf.g etIndex(); | |
213 | if (index == -1) | |
214 | throw new IOExce ption("Net work inter face canno t be ident ified"); | |
215 | Net.setInt erface6(fd , index); | |
216 | } el se { | |
217 | // need IP v4 address to identi fy interfa ce | |
218 | Inet4Addre ss target = Net.anyI net4Addres s(interf); | |
219 | if (target == null) | |
220 | throw new IOExce ption("Net work inter face not c onfigured for IPv4") ; | |
221 | int target Address = Net.inet4A sInt(targe t); | |
222 | Net.setInt erface4(fd , targetAd dress); | |
223 | } | |
224 | retu rn this; | |
225 | } | |
226 | if (name == Standa rdSocketOp tions.SO_R EUSEADDR & & | |
227 | Net.useExc lusiveBind () && loca lAddress ! = null) | |
228 | { | |
229 | reus eAddressEm ulated = t rue; | |
230 | this .isReuseAd dress = (B oolean)val ue; | |
231 | } | |
232 | ||
233 | // remai ning optio ns don't n eed any sp ecial hand ling | |
234 | Net.setS ocketOptio n(fd, Net. UNSPEC, na me, value) ; | |
235 | return t his; | |
236 | } | |
237 | } | |
238 | ||
239 | @Overr ide | |
240 | @Suppr essWarning s("uncheck ed") | |
241 | public <T> T get Option(Soc ketOption< T> name) | |
242 | th rows IOExc eption | |
243 | { | |
244 | if (name == null) | |
245 | throw ne w NullPoin terExcepti on(); | |
246 | if (!support edOptions( ).contains (name)) | |
247 | throw ne w Unsuppor tedOperati onExceptio n("'" + na me + "' no t supporte d"); | |
248 | ||
249 | sy nchronized (stateLoc k) { | |
250 | ensureOp en(); | |
251 | ||
252 | if (name == Standa rdSocketOp tions.IP_T OS || | |
253 | name == Standa rdSocketOp tions.IP_M ULTICAST_T TL || | |
254 | name == Standa rdSocketOp tions.IP_M ULTICAST_L OOP) | |
255 | { | |
256 | retu rn (T) Net .getSocket Option(fd, family, n ame); | |
257 | } | |
258 | ||
259 | if (name == Standa rdSocketOp tions.IP_M ULTICAST_I F) { | |
260 | if ( family == StandardPr otocolFami ly.INET) { | |
261 | int addres s = Net.ge tInterface 4(fd); | |
262 | if (addres s == 0) | |
263 | return null; // default interface | |
264 | ||
265 | InetAddres s ia = Net .inet4From Int(addres s); | |
266 | NetworkInt erface ni = NetworkI nterface.g etByInetAd dress(ia); | |
267 | if (ni == null) | |
268 | throw new IOExce ption("Una ble to map address t o interfac e"); | |
269 | return (T) ni; | |
270 | } el se { | |
271 | int index = Net.getI nterface6( fd); | |
272 | if (index == 0) | |
273 | return null; // default interface | |
274 | ||
275 | NetworkInt erface ni = NetworkI nterface.g etByIndex( index); | |
276 | if (ni == null) | |
277 | throw new IOExce ption("Una ble to map index to interface" ); | |
278 | return (T) ni; | |
279 | } | |
280 | } | |
281 | ||
282 | if (name == Standa rdSocketOp tions.SO_R EUSEADDR & & | |
283 | reuseAddre ssEmulated ) | |
284 | { | |
285 | retu rn (T)Bool ean.valueO f(isReuseA ddress); | |
286 | } | |
287 | ||
288 | // no sp ecial hand ling | |
289 | return ( T) Net.get SocketOpti on(fd, Net .UNSPEC, n ame); | |
290 | } | |
291 | } | |
292 | ||
293 | privat e static c lass Defau ltOptionsH older { | |
294 | st atic final Set<Socke tOption<?> > defaultO ptions = d efaultOpti ons(); | |
295 | ||
296 | pr ivate stat ic Set<Soc ketOption< ?>> defaul tOptions() { | |
297 | HashSet< SocketOpti on<?>> set = new Has hSet<Socke tOption<?> >(8); | |
298 | set.add( StandardSo cketOption s.SO_SNDBU F); | |
299 | set.add( StandardSo cketOption s.SO_RCVBU F); | |
300 | set.add( StandardSo cketOption s.SO_REUSE ADDR); | |
301 | set.add( StandardSo cketOption s.SO_BROAD CAST); | |
302 | set.add( StandardSo cketOption s.IP_TOS); | |
303 | set.add( StandardSo cketOption s.IP_MULTI CAST_IF); | |
304 | set.add( StandardSo cketOption s.IP_MULTI CAST_TTL); | |
305 | set.add( StandardSo cketOption s.IP_MULTI CAST_LOOP) ; | |
306 | if (Exte ndedOption sImpl.flow Supported( )) { | |
307 | set. add(jdk.ne t.Extended SocketOpti ons.SO_FLO W_SLA); | |
308 | } | |
309 | return C ollections .unmodifia bleSet(set ); | |
310 | } | |
311 | } | |
312 | ||
313 | @Overr ide | |
314 | public final Set <SocketOpt ion<?>> su pportedOpt ions() { | |
315 | re turn Defau ltOptionsH older.defa ultOptions ; | |
316 | } | |
317 | ||
318 | privat e void ens ureOpen() throws Clo sedChannel Exception { | |
319 | if (!isOpen( )) | |
320 | throw ne w ClosedCh annelExcep tion(); | |
321 | } | |
322 | ||
323 | privat e SocketAd dress send er; // Set by receive0 ( ## ugh) | |
324 | ||
325 | public SocketAdd ress recei ve(ByteBuf fer dst) t hrows IOEx ception { | |
326 | if (dst.isRe adOnly()) | |
327 | throw ne w IllegalA rgumentExc eption("Re ad-only bu ffer"); | |
328 | if (dst == n ull) | |
329 | throw ne w NullPoin terExcepti on(); | |
330 | sy nchronized (readLock ) { | |
331 | ensureOp en(); | |
332 | // Socke t was not bound befo re attempt ing receiv e | |
333 | if (loca lAddress() == null) | |
334 | bind (null); | |
335 | int n = 0; | |
336 | ByteBuff er bb = nu ll; | |
337 | try { | |
338 | begi n(); | |
339 | if ( !isOpen()) | |
340 | return nul l; | |
341 | Secu rityManage r security = System. getSecurit yManager() ; | |
342 | read erThread = NativeThr ead.curren t(); | |
343 | if ( isConnecte d() || (se curity == null)) { | |
344 | do { | |
345 | n = re ceive(fd, dst); | |
346 | } while (( n == IOSta tus.INTERR UPTED) && isOpen()); | |
347 | if (n == I OStatus.UN AVAILABLE) | |
348 | return null; | |
349 | } el se { | |
350 | bb = Util. getTempora ryDirectBu ffer(dst.r emaining() ); | |
351 | for (;;) { | |
352 | do { | |
353 | n = receive( fd, bb); | |
354 | } whil e ((n == I OStatus.IN TERRUPTED) && isOpen ()); | |
355 | if (n == IOStatu s.UNAVAILA BLE) | |
356 | re turn null; | |
357 | InetSo cketAddres s isa = (I netSocketA ddress)sen der; | |
358 | try { | |
359 | se curity.che ckAccept( | |
360 | isa.getA ddress().g etHostAddr ess(), | |
361 | isa.getP ort()); | |
362 | } catc h (Securit yException se) { | |
363 | // Ignore pa cket | |
364 | bb .clear(); | |
365 | n = 0; | |
366 | co ntinue; | |
367 | } | |
368 | bb.fli p(); | |
369 | dst.pu t(bb); | |
370 | break; | |
371 | } | |
372 | } | |
373 | retu rn sender; | |
374 | } finall y { | |
375 | if ( bb != null ) | |
376 | Util.relea seTemporar yDirectBuf fer(bb); | |
377 | read erThread = 0; | |
378 | end( (n > 0) || (n == IOS tatus.UNAV AILABLE)); | |
379 | asse rt IOStatu s.check(n) ; | |
380 | } | |
381 | } | |
382 | } | |
383 | ||
384 | privat e int rece ive(FileDe scriptor f d, ByteBuf fer dst) | |
385 | th rows IOExc eption | |
386 | { | |
387 | in t pos = ds t.position (); | |
388 | in t lim = ds t.limit(); | |
389 | as sert (pos <= lim); | |
390 | in t rem = (p os <= lim ? lim - po s : 0); | |
391 | if (dst inst anceof Dir ectBuffer && rem > 0 ) | |
392 | return r eceiveInto NativeBuff er(fd, dst , rem, pos ); | |
393 | ||
394 | // Substitut e a native buffer. I f the supp lied buffe r is empty | |
395 | // we must i nstead use a nonempt y buffer, otherwise the call | |
396 | // will not block wait ing for a datagram o n some pla tforms. | |
397 | in t newSize = Math.max (rem, 1); | |
398 | By teBuffer b b = Util.g etTemporar yDirectBuf fer(newSiz e); | |
399 | tr y { | |
400 | int n = receiveInt oNativeBuf fer(fd, bb , newSize, 0); | |
401 | bb.flip( ); | |
402 | if (n > 0 && rem > 0) | |
403 | dst. put(bb); | |
404 | return n ; | |
405 | } finally { | |
406 | Util.rel easeTempor aryDirectB uffer(bb); | |
407 | } | |
408 | } | |
409 | ||
410 | privat e int rece iveIntoNat iveBuffer( FileDescri ptor fd, B yteBuffer bb, | |
411 | int rem, i nt pos) | |
412 | th rows IOExc eption | |
413 | { | |
414 | in t n = rece ive0(fd, ( (DirectBuf fer)bb).ad dress() + pos, rem, | |
415 | isCon nected()); | |
416 | if (n > 0) | |
417 | bb.posit ion(pos + n); | |
418 | re turn n; | |
419 | } | |
420 | ||
421 | public int send( ByteBuffer src, Sock etAddress target) | |
422 | th rows IOExc eption | |
423 | { | |
424 | if (src == n ull) | |
425 | throw ne w NullPoin terExcepti on(); | |
426 | ||
427 | sy nchronized (writeLoc k) { | |
428 | ensureOp en(); | |
429 | InetSock etAddress isa = Net. checkAddre ss(target) ; | |
430 | InetAddr ess ia = i sa.getAddr ess(); | |
431 | if (ia = = null) | |
432 | thro w new IOEx ception("T arget addr ess not re solved"); | |
433 | synchron ized (stat eLock) { | |
434 | if ( !isConnect ed()) { | |
435 | if (target == null) | |
436 | throw new NullPo interExcep tion(); | |
437 | SecurityMa nager sm = System.ge tSecurityM anager(); | |
438 | if (sm != null) { | |
439 | if (ia .isMultica stAddress( )) { | |
440 | sm .checkMult icast(ia); | |
441 | } else { | |
442 | sm .checkConn ect(ia.get HostAddres s(), | |
443 | isa.ge tPort()); | |
444 | } | |
445 | } | |
446 | } el se { // Co nnected ca se; Check address th en write | |
447 | if (!targe t.equals(r emoteAddre ss)) { | |
448 | throw new Illega lArgumentE xception( | |
449 | "C onnected a ddress not equal to target add ress"); | |
450 | } | |
451 | return wri te(src); | |
452 | } | |
453 | } | |
454 | ||
455 | int n = 0; | |
456 | try { | |
457 | begi n(); | |
458 | if ( !isOpen()) | |
459 | return 0; | |
460 | writ erThread = NativeThr ead.curren t(); | |
461 | do { | |
462 | n = send(f d, src, is a); | |
463 | } wh ile ((n == IOStatus. INTERRUPTE D) && isOp en()); | |
464 | ||
465 | sync hronized ( stateLock) { | |
466 | if (isOpen () && (loc alAddress == null)) { | |
467 | localA ddress = N et.localAd dress(fd); | |
468 | } | |
469 | } | |
470 | retu rn IOStatu s.normaliz e(n); | |
471 | } finall y { | |
472 | writ erThread = 0; | |
473 | end( (n > 0) || (n == IOS tatus.UNAV AILABLE)); | |
474 | asse rt IOStatu s.check(n) ; | |
475 | } | |
476 | } | |
477 | } | |
478 | ||
479 | privat e int send (FileDescr iptor fd, ByteBuffer src, Inet SocketAddr ess target ) | |
480 | th rows IOExc eption | |
481 | { | |
482 | if (src inst anceof Dir ectBuffer) | |
483 | return s endFromNat iveBuffer( fd, src, t arget); | |
484 | ||
485 | // Substitut e a native buffer | |
486 | in t pos = sr c.position (); | |
487 | in t lim = sr c.limit(); | |
488 | as sert (pos <= lim); | |
489 | in t rem = (p os <= lim ? lim - po s : 0); | |
490 | ||
491 | By teBuffer b b = Util.g etTemporar yDirectBuf fer(rem); | |
492 | tr y { | |
493 | bb.put(s rc); | |
494 | bb.flip( ); | |
495 | // Do no t update s rc until w e see how many bytes were writ ten | |
496 | src.posi tion(pos); | |
497 | ||
498 | int n = sendFromNa tiveBuffer (fd, bb, t arget); | |
499 | if (n > 0) { | |
500 | // n ow update src | |
501 | src. position(p os + n); | |
502 | } | |
503 | return n ; | |
504 | } finally { | |
505 | Util.rel easeTempor aryDirectB uffer(bb); | |
506 | } | |
507 | } | |
508 | ||
509 | privat e int send FromNative Buffer(Fil eDescripto r fd, Byte Buffer bb, | |
510 | Ine tSocketAdd ress targe t) | |
511 | th rows IOExc eption | |
512 | { | |
513 | in t pos = bb .position( ); | |
514 | in t lim = bb .limit(); | |
515 | as sert (pos <= lim); | |
516 | in t rem = (p os <= lim ? lim - po s : 0); | |
517 | ||
518 | bo olean pref erIPv6 = ( family != StandardPr otocolFami ly.INET); | |
519 | in t written; | |
520 | tr y { | |
521 | written = send0(pr eferIPv6, fd, ((Dire ctBuffer)b b).address () + pos, | |
522 | re m, target. getAddress (), target .getPort() ); | |
523 | } catch (Por tUnreachab leExceptio n pue) { | |
524 | if (isCo nnected()) | |
525 | thro w pue; | |
526 | written = rem; | |
527 | } | |
528 | if (written > 0) | |
529 | bb.posit ion(pos + written); | |
530 | re turn writt en; | |
531 | } | |
532 | ||
533 | public int read( ByteBuffer buf) thro ws IOExcep tion { | |
534 | if (buf == n ull) | |
535 | throw ne w NullPoin terExcepti on(); | |
536 | sy nchronized (readLock ) { | |
537 | synchron ized (stat eLock) { | |
538 | ensu reOpen(); | |
539 | if ( !isConnect ed()) | |
540 | throw new NotYetConn ectedExcep tion(); | |
541 | } | |
542 | int n = 0; | |
543 | try { | |
544 | begi n(); | |
545 | if ( !isOpen()) | |
546 | return 0; | |
547 | read erThread = NativeThr ead.curren t(); | |
548 | do { | |
549 | n = IOUtil .read(fd, buf, -1, n d); | |
550 | } wh ile ((n == IOStatus. INTERRUPTE D) && isOp en()); | |
551 | retu rn IOStatu s.normaliz e(n); | |
552 | } finall y { | |
553 | read erThread = 0; | |
554 | end( (n > 0) || (n == IOS tatus.UNAV AILABLE)); | |
555 | asse rt IOStatu s.check(n) ; | |
556 | } | |
557 | } | |
558 | } | |
559 | ||
560 | public long read (ByteBuffe r[] dsts, int offset , int leng th) | |
561 | th rows IOExc eption | |
562 | { | |
563 | if ((offset < 0) || (l ength < 0) || (offse t > dsts.l ength - le ngth)) | |
564 | throw ne w IndexOut OfBoundsEx ception(); | |
565 | sy nchronized (readLock ) { | |
566 | synchron ized (stat eLock) { | |
567 | ensu reOpen(); | |
568 | if ( !isConnect ed()) | |
569 | throw new NotYetConn ectedExcep tion(); | |
570 | } | |
571 | long n = 0; | |
572 | try { | |
573 | begi n(); | |
574 | if ( !isOpen()) | |
575 | return 0; | |
576 | read erThread = NativeThr ead.curren t(); | |
577 | do { | |
578 | n = IOUtil .read(fd, dsts, offs et, length , nd); | |
579 | } wh ile ((n == IOStatus. INTERRUPTE D) && isOp en()); | |
580 | retu rn IOStatu s.normaliz e(n); | |
581 | } finall y { | |
582 | read erThread = 0; | |
583 | end( (n > 0) || (n == IOS tatus.UNAV AILABLE)); | |
584 | asse rt IOStatu s.check(n) ; | |
585 | } | |
586 | } | |
587 | } | |
588 | ||
589 | public int write (ByteBuffe r buf) thr ows IOExce ption { | |
590 | if (buf == n ull) | |
591 | throw ne w NullPoin terExcepti on(); | |
592 | sy nchronized (writeLoc k) { | |
593 | synchron ized (stat eLock) { | |
594 | ensu reOpen(); | |
595 | if ( !isConnect ed()) | |
596 | throw new NotYetConn ectedExcep tion(); | |
597 | } | |
598 | int n = 0; | |
599 | try { | |
600 | begi n(); | |
601 | if ( !isOpen()) | |
602 | return 0; | |
603 | writ erThread = NativeThr ead.curren t(); | |
604 | do { | |
605 | n = IOUtil .write(fd, buf, -1, nd); | |
606 | } wh ile ((n == IOStatus. INTERRUPTE D) && isOp en()); | |
607 | retu rn IOStatu s.normaliz e(n); | |
608 | } finall y { | |
609 | writ erThread = 0; | |
610 | end( (n > 0) || (n == IOS tatus.UNAV AILABLE)); | |
611 | asse rt IOStatu s.check(n) ; | |
612 | } | |
613 | } | |
614 | } | |
615 | ||
616 | public long writ e(ByteBuff er[] srcs, int offse t, int len gth) | |
617 | th rows IOExc eption | |
618 | { | |
619 | if ((offset < 0) || (l ength < 0) || (offse t > srcs.l ength - le ngth)) | |
620 | throw ne w IndexOut OfBoundsEx ception(); | |
621 | sy nchronized (writeLoc k) { | |
622 | synchron ized (stat eLock) { | |
623 | ensu reOpen(); | |
624 | if ( !isConnect ed()) | |
625 | throw new NotYetConn ectedExcep tion(); | |
626 | } | |
627 | long n = 0; | |
628 | try { | |
629 | begi n(); | |
630 | if ( !isOpen()) | |
631 | return 0; | |
632 | writ erThread = NativeThr ead.curren t(); | |
633 | do { | |
634 | n = IOUtil .write(fd, srcs, off set, lengt h, nd); | |
635 | } wh ile ((n == IOStatus. INTERRUPTE D) && isOp en()); | |
636 | retu rn IOStatu s.normaliz e(n); | |
637 | } finall y { | |
638 | writ erThread = 0; | |
639 | end( (n > 0) || (n == IOS tatus.UNAV AILABLE)); | |
640 | asse rt IOStatu s.check(n) ; | |
641 | } | |
642 | } | |
643 | } | |
644 | ||
645 | protec ted void i mplConfigu reBlocking (boolean b lock) thro ws IOExcep tion { | |
646 | IO Util.confi gureBlocki ng(fd, blo ck); | |
647 | } | |
648 | ||
649 | public SocketAdd ress local Address() { | |
650 | sy nchronized (stateLoc k) { | |
651 | return l ocalAddres s; | |
652 | } | |
653 | } | |
654 | ||
655 | public SocketAdd ress remot eAddress() { | |
656 | sy nchronized (stateLoc k) { | |
657 | return r emoteAddre ss; | |
658 | } | |
659 | } | |
660 | ||
661 | @Overr ide | |
662 | public DatagramC hannel bin d(SocketAd dress loca l) throws IOExceptio n { | |
663 | sy nchronized (readLock ) { | |
664 | synchron ized (writ eLock) { | |
665 | sync hronized ( stateLock) { | |
666 | ensureOpen (); | |
667 | if (localA ddress != null) | |
668 | throw new Alread yBoundExce ption(); | |
669 | InetSocket Address is a; | |
670 | if (local == null) { | |
671 | // onl y Inet4Add ress allow ed with IP v4 socket | |
672 | if (fa mily == St andardProt ocolFamily .INET) { | |
673 | is a = new In etSocketAd dress(Inet Address.ge tByName("0 .0.0.0"), 0); | |
674 | } else { | |
675 | is a = new In etSocketAd dress(0); | |
676 | } | |
677 | } else { | |
678 | isa = Net.checkA ddress(loc al); | |
679 | ||
680 | // onl y Inet4Add ress allow ed with IP v4 socket | |
681 | if (fa mily == St andardProt ocolFamily .INET) { | |
682 | In etAddress addr = isa .getAddres s(); | |
683 | if (!(addr i nstanceof Inet4Addre ss)) | |
684 | throw ne w Unsuppor tedAddress TypeExcept ion(); | |
685 | } | |
686 | } | |
687 | SecurityMa nager sm = System.ge tSecurityM anager(); | |
688 | if (sm != null) { | |
689 | sm.che ckListen(i sa.getPort ()); | |
690 | } | |
691 | Net.bind(f amily, fd, isa.getAd dress(), i sa.getPort ()); | |
692 | localAddre ss = Net.l ocalAddres s(fd); | |
693 | } | |
694 | } | |
695 | } | |
696 | re turn this; | |
697 | } | |
698 | ||
699 | public boolean i sConnected () { | |
700 | sy nchronized (stateLoc k) { | |
701 | return ( state == S T_CONNECTE D); | |
702 | } | |
703 | } | |
704 | ||
705 | void e nsureOpenA ndUnconnec ted() thro ws IOExcep tion { // package-pr ivate | |
706 | sy nchronized (stateLoc k) { | |
707 | if (!isO pen()) | |
708 | thro w new Clos edChannelE xception() ; | |
709 | if (stat e != ST_UN CONNECTED) | |
710 | thro w new Ille galStateEx ception("C onnect alr eady invok ed"); | |
711 | } | |
712 | } | |
713 | ||
714 | @Overr ide | |
715 | public DatagramC hannel con nect(Socke tAddress s a) throws IOExceptio n { | |
716 | int localP ORT
|
|
717 | ||
718 | sy nchronized (readLock) { | |
719 | synchron ized(write Lock) { | |
720 | sync hronized ( stateLock) { | |
721 | ensureOpen AndUnconne cted(); | |
722 | InetSocket Address is a = Net.ch eckAddress (sa); | |
723 | SecurityMa nager sm = System.ge tSecurityM anager(); | |
724 | if (sm != null) | |
725 | sm.che ckConnect( isa.getAdd ress().get HostAddres s(), | |
726 | isa.getPor t()); | |
727 | int n = Ne t.connect( family, | |
728 | fd, | |
729 | isa.getAdd ress(), | |
730 | isa.getPor t()); | |
731 | if (n <= 0 ) | |
732 | throw new Error( ); // Can't hap pen | |
733 | ||
734 | // Connect ion succee ded; disal low furthe r invocati on | |
735 | state = ST _CONNECTED ; | |
736 | remoteAddr ess = isa; | |
737 | sender = i sa; | |
738 | cachedSend erInetAddr ess = isa. getAddress (); | |
739 | cachedSend erPort = i sa.getPort (); | |
740 | ||
741 | // set or refresh lo cal addres s | |
742 | localAddre ss = Net.l ocalAddres s(fd); | |
743 | ||
744 | // flush a ny packets already r eceived. | |
745 | boolean bl ocking = f alse; | |
746 | synchroniz ed (blocki ngLock()) { | |
747 | try { | |
748 | bl ocking = i sBlocking( ); | |
749 | // remainder of each p acket thro wn away | |
750 | By teBuffer t mpBuf = By teBuffer.a llocate(1) ; | |
751 | if (blocking ) { | |
752 | configur eBlocking( false); | |
753 | } | |
754 | do { | |
755 | tmpBuf.c lear(); | |
756 | } while (rec eive(tmpBu f) != null ); | |
757 | } fina lly { | |
758 | if (blocking ) { | |
759 | configur eBlocking( true); | |
760 | } | |
761 | } | |
762 | } | |
763 | } | |
764 | } | |
765 | } | |
766 | re turn this; | |
767 | } | |
768 | ||
769 | public DatagramC hannel dis connect() throws IOE xception { | |
770 | sy nchronized (readLock) { | |
771 | synchron ized(write Lock) { | |
772 | sync hronized ( stateLock) { | |
773 | if (!isCon nected() | | !isOpen( )) | |
774 | return this; | |
775 | InetSocket Address is a = remote Address; | |
776 | SecurityMa nager sm = System.ge tSecurityM anager(); | |
777 | if (sm != null) | |
778 | sm.che ckConnect( isa.getAdd ress().get HostAddres s(), | |
779 | isa.getPor t()); | |
780 | boolean is IPv6 = (fa mily == St andardProt ocolFamily .INET6); | |
781 | disconnect 0(fd, isIP v6); | |
782 | remoteAddr ess = null ; | |
783 | state = ST _UNCONNECT ED; | |
784 | ||
785 | // refresh local add ress | |
786 | localAddre ss = Net.l ocalAddres s(fd); | |
787 | } | |
788 | } | |
789 | } | |
790 | re turn this; | |
791 | } | |
792 | ||
793 | /** | |
794 | * Joi ns channel 's socket to the giv en group/i nterface a nd | |
795 | * opt ional sour ce address . | |
796 | */ | |
797 | privat e Membersh ipKey inne rJoin(Inet Address gr oup, | |
798 | Netw orkInterfa ce interf, | |
799 | Inet Address so urce) | |
800 | th rows IOExc eption | |
801 | { | |
802 | if (!group.i sMulticast Address()) | |
803 | throw ne w IllegalA rgumentExc eption("Gr oup not a multicast address"); | |
804 | ||
805 | // check mul ticast add ress is co mpatible w ith this s ocket | |
806 | if (group in stanceof I net4Addres s) { | |
807 | if (fami ly == Stan dardProtoc olFamily.I NET6 && !N et.canIPv6 SocketJoin IPv4Group( )) | |
808 | thro w new Ille galArgumen tException ("IPv6 soc ket cannot join IPv4 multicast group"); | |
809 | } else if (g roup insta nceof Inet 6Address) { | |
810 | if (fami ly != Stan dardProtoc olFamily.I NET6) | |
811 | thro w new Ille galArgumen tException ("Only IPv 6 sockets can join I Pv6 multic ast group" ); | |
812 | } else { | |
813 | throw ne w IllegalA rgumentExc eption("Ad dress type not suppo rted"); | |
814 | } | |
815 | ||
816 | // check sou rce addres s | |
817 | if (source ! = null) { | |
818 | if (sour ce.isAnyLo calAddress ()) | |
819 | thro w new Ille galArgumen tException ("Source a ddress is a wildcard address") ; | |
820 | if (sour ce.isMulti castAddres s()) | |
821 | thro w new Ille galArgumen tException ("Source a ddress is multicast address"); | |
822 | if (sour ce.getClas s() != gro up.getClas s()) | |
823 | thro w new Ille galArgumen tException ("Source a ddress is different type to gr oup"); | |
824 | } | |
825 | ||
826 | Se curityMana ger sm = S ystem.getS ecurityMan ager(); | |
827 | if (sm != nu ll) | |
828 | sm.check Multicast( group); | |
829 | ||
830 | sy nchronized (stateLoc k) { | |
831 | if (!isO pen()) | |
832 | thro w new Clos edChannelE xception() ; | |
833 | ||
834 | // check the regis try to see if we are already a member of the group | |
835 | if (regi stry == nu ll) { | |
836 | regi stry = new Membershi pRegistry( ); | |
837 | } else { | |
838 | // r eturn exis ting membe rship key | |
839 | Memb ershipKey key = regi stry.check Membership (group, in terf, sour ce); | |
840 | if ( key != nul l) | |
841 | return key ; | |
842 | } | |
843 | ||
844 | Membersh ipKeyImpl key; | |
845 | if ((fam ily == Sta ndardProto colFamily. INET6) && | |
846 | ((gr oup instan ceof Inet6 Address) | | Net.canJ oin6WithIP v4Group()) ) | |
847 | { | |
848 | int index = in terf.getIn dex(); | |
849 | if ( index == - 1) | |
850 | throw new IOExceptio n("Network interface cannot be identifie d"); | |
851 | ||
852 | // n eed multic ast and so urce addre ss as byte arrays | |
853 | byte [] groupAd dress = Ne t.inet6AsB yteArray(g roup); | |
854 | byte [] sourceA ddress = ( source == null) ? nu ll : | |
855 | Net.inet6A sByteArray (source); | |
856 | ||
857 | // j oin the gr oup | |
858 | int n = Net.jo in6(fd, gr oupAddress , index, s ourceAddre ss); | |
859 | if ( n == IOSta tus.UNAVAI LABLE) | |
860 | throw new Unsupporte dOperation Exception( ); | |
861 | ||
862 | key = new Memb ershipKeyI mpl.Type6( this, grou p, interf, source, | |
863 | groupAddre ss, index, sourceAdd ress); | |
864 | ||
865 | } else { | |
866 | // n eed IPv4 a ddress to identify i nterface | |
867 | Inet 4Address t arget = Ne t.anyInet4 Address(in terf); | |
868 | if ( target == null) | |
869 | throw new IOExceptio n("Network interface not confi gured for IPv4"); | |
870 | ||
871 | int groupAddre ss = Net.i net4AsInt( group); | |
872 | int targetAddr ess = Net. inet4AsInt (target); | |
873 | int sourceAddr ess = (sou rce == nul l) ? 0 : N et.inet4As Int(source ); | |
874 | ||
875 | // j oin the gr oup | |
876 | int n = Net.jo in4(fd, gr oupAddress , targetAd dress, sou rceAddress ); | |
877 | if ( n == IOSta tus.UNAVAI LABLE) | |
878 | throw new Unsupporte dOperation Exception( ); | |
879 | ||
880 | key = new Memb ershipKeyI mpl.Type4( this, grou p, interf, source, | |
881 | groupAddre ss, target Address, s ourceAddre ss); | |
882 | } | |
883 | ||
884 | registry .add(key); | |
885 | return k ey; | |
886 | } | |
887 | } | |
888 | ||
889 | @Overr ide | |
890 | public Membershi pKey join( InetAddres s group, | |
891 | NetworkInt erface int erf) | |
892 | th rows IOExc eption | |
893 | { | |
894 | re turn inner Join(group , interf, null); | |
895 | } | |
896 | ||
897 | @Overr ide | |
898 | public Membershi pKey join( InetAddres s group, | |
899 | NetworkInt erface int erf, | |
900 | InetAddres s source) | |
901 | th rows IOExc eption | |
902 | { | |
903 | if (source = = null) | |
904 | throw ne w NullPoin terExcepti on("source address i s null"); | |
905 | re turn inner Join(group , interf, source); | |
906 | } | |
907 | ||
908 | // pac kage-priva te | |
909 | void d rop(Member shipKeyImp l key) { | |
910 | as sert key.c hannel() = = this; | |
911 | ||
912 | sy nchronized (stateLoc k) { | |
913 | if (!key .isValid() ) | |
914 | retu rn; | |
915 | ||
916 | try { | |
917 | if ( key instan ceof Membe rshipKeyIm pl.Type6) { | |
918 | Membership KeyImpl.Ty pe6 key6 = | |
919 | (Membe rshipKeyIm pl.Type6)k ey; | |
920 | Net.drop6( fd, key6.g roupAddres s(), key6. index(), k ey6.source ()); | |
921 | } el se { | |
922 | Membership KeyImpl.Ty pe4 key4 = (Membersh ipKeyImpl. Type4)key; | |
923 | Net.drop4( fd, key4.g roupAddres s(), key4. interfaceA ddress(), | |
924 | key4.s ource()); | |
925 | } | |
926 | } catch (IOExcepti on ioe) { | |
927 | // s hould not happen | |
928 | thro w new Asse rtionError (ioe); | |
929 | } | |
930 | ||
931 | key.inva lidate(); | |
932 | registry .remove(ke y); | |
933 | } | |
934 | } | |
935 | ||
936 | /** | |
937 | * Blo ck datagra ms from gi ven source if a memo ry to rece ive all | |
938 | * dat agrams. | |
939 | */ | |
940 | void b lock(Membe rshipKeyIm pl key, In etAddress source) | |
941 | th rows IOExc eption | |
942 | { | |
943 | as sert key.c hannel() = = this; | |
944 | as sert key.s ourceAddre ss() == nu ll; | |
945 | ||
946 | sy nchronized (stateLoc k) { | |
947 | if (!key .isValid() ) | |
948 | thro w new Ille galStateEx ception("k ey is no l onger vali d"); | |
949 | if (sour ce.isAnyLo calAddress ()) | |
950 | thro w new Ille galArgumen tException ("Source a ddress is a wildcard address") ; | |
951 | if (sour ce.isMulti castAddres s()) | |
952 | thro w new Ille galArgumen tException ("Source a ddress is multicast address"); | |
953 | if (sour ce.getClas s() != key .group().g etClass()) | |
954 | thro w new Ille galArgumen tException ("Source a ddress is different type to gr oup"); | |
955 | ||
956 | int n; | |
957 | if (key instanceof Membershi pKeyImpl.T ype6) { | |
958 | Mem bershipKey Impl.Type6 key6 = | |
959 | (Membershi pKeyImpl.T ype6)key; | |
960 | n = Net.block6 (fd, key6. groupAddre ss(), key6 .index(), | |
961 | Net.inet6 AsByteArra y(source)) ; | |
962 | } else { | |
963 | Memb ershipKeyI mpl.Type4 key4 = | |
964 | (Membershi pKeyImpl.T ype4)key; | |
965 | n = Net.block4 (fd, key4. groupAddre ss(), key4 .interface Address(), | |
966 | Net.inet4 AsInt(sour ce)); | |
967 | } | |
968 | if (n == IOStatus. UNAVAILABL E) { | |
969 | // a ncient ker nel | |
970 | thro w new Unsu pportedOpe rationExce ption(); | |
971 | } | |
972 | } | |
973 | } | |
974 | ||
975 | /** | |
976 | * Unb lock given source. | |
977 | */ | |
978 | void u nblock(Mem bershipKey Impl key, InetAddres s source) { | |
979 | as sert key.c hannel() = = this; | |
980 | as sert key.s ourceAddre ss() == nu ll; | |
981 | ||
982 | sy nchronized (stateLoc k) { | |
983 | if (!key .isValid() ) | |
984 | thro w new Ille galStateEx ception("k ey is no l onger vali d"); | |
985 | ||
986 | try { | |
987 | if ( key instan ceof Membe rshipKeyIm pl.Type6) { | |
988 | Membership KeyImpl.Ty pe6 key6 = | |
989 | (Membe rshipKeyIm pl.Type6)k ey; | |
990 | Net.unbloc k6(fd, key 6.groupAdd ress(), ke y6.index() , | |
991 | Net.ine t6AsByteAr ray(source )); | |
992 | } el se { | |
993 | Membership KeyImpl.Ty pe4 key4 = | |
994 | (Membe rshipKeyIm pl.Type4)k ey; | |
995 | Net.unbloc k4(fd, key 4.groupAdd ress(), ke y4.interfa ceAddress( ), | |
996 | Net.ine t4AsInt(so urce)); | |
997 | } | |
998 | } catch (IOExcepti on ioe) { | |
999 | // s hould not happen | |
1000 | thro w new Asse rtionError (ioe); | |
1001 | } | |
1002 | } | |
1003 | } | |
1004 | ||
1005 | protec ted void i mplCloseSe lectableCh annel() th rows IOExc eption { | |
1006 | sy nchronized (stateLoc k) { | |
1007 | if (stat e != ST_KI LLED) | |
1008 | nd.p reClose(fd ); | |
1009 | Resource Manager.af terUdpClos e(); | |
1010 | ||
1011 | // if me mber of mu litcast gr oup then i nvalidate all keys | |
1012 | if (regi stry != nu ll) | |
1013 | regi stry.inval idateAll() ; | |
1014 | ||
1015 | long th; | |
1016 | if ((th = readerTh read) != 0 ) | |
1017 | Nati veThread.s ignal(th); | |
1018 | if ((th = writerTh read) != 0 ) | |
1019 | Nati veThread.s ignal(th); | |
1020 | if (!isR egistered( )) | |
1021 | kill (); | |
1022 | } | |
1023 | } | |
1024 | ||
1025 | public void kill () throws IOExceptio n { | |
1026 | sy nchronized (stateLoc k) { | |
1027 | if (stat e == ST_KI LLED) | |
1028 | retu rn; | |
1029 | if (stat e == ST_UN INITIALIZE D) { | |
1030 | stat e = ST_KIL LED; | |
1031 | retu rn; | |
1032 | } | |
1033 | assert ! isOpen() & & !isRegis tered(); | |
1034 | nd.close (fd); | |
1035 | state = ST_KILLED; | |
1036 | } | |
1037 | } | |
1038 | ||
1039 | protec ted void f inalize() throws IOE xception { | |
1040 | // fd is nul l if const ructor thr ew excepti on | |
1041 | if (fd != nu ll) | |
1042 | close(); | |
1043 | } | |
1044 | ||
1045 | /** | |
1046 | * Tra nslates na tive poll revent set into a re ady operat ion set | |
1047 | */ | |
1048 | public boolean t ranslateRe adyOps(int ops, int initialOps , | |
1049 | Sel ectionKeyI mpl sk) { | |
1050 | in t intOps = sk.nioInt erestOps() ; // Do th is just on ce, it syn chronizes | |
1051 | in t oldOps = sk.nioRea dyOps(); | |
1052 | in t newOps = initialOp s; | |
1053 | ||
1054 | if ((ops & N et.POLLNVA L) != 0) { | |
1055 | // This should onl y happen i f this cha nnel is pr e-closed w hile a | |
1056 | // selec tion opera tion is in progress | |
1057 | // ## Th row an err or if this channel h as not bee n pre-clos ed | |
1058 | return f alse; | |
1059 | } | |
1060 | ||
1061 | if ((ops & ( Net.POLLER R | Net.PO LLHUP)) != 0) { | |
1062 | newOps = intOps; | |
1063 | sk.nioRe adyOps(new Ops); | |
1064 | return ( newOps & ~ oldOps) != 0; | |
1065 | } | |
1066 | ||
1067 | if (((ops & Net.POLLIN ) != 0) && | |
1068 | ((intOps & Selecti onKey.OP_R EAD) != 0) ) | |
1069 | newOps | = Selectio nKey.OP_RE AD; | |
1070 | ||
1071 | if (((ops & Net.POLLOU T) != 0) & & | |
1072 | ((intOps & Selecti onKey.OP_W RITE) != 0 )) | |
1073 | newOps | = Selectio nKey.OP_WR ITE; | |
1074 | ||
1075 | sk .nioReadyO ps(newOps) ; | |
1076 | re turn (newO ps & ~oldO ps) != 0; | |
1077 | } | |
1078 | ||
1079 | public boolean t ranslateAn dUpdateRea dyOps(int ops, Selec tionKeyImp l sk) { | |
1080 | re turn trans lateReadyO ps(ops, sk .nioReadyO ps(), sk); | |
1081 | } | |
1082 | ||
1083 | public boolean t ranslateAn dSetReadyO ps(int ops , Selectio nKeyImpl s k) { | |
1084 | re turn trans lateReadyO ps(ops, 0, sk); | |
1085 | } | |
1086 | ||
1087 | // pac kage-priva te | |
1088 | int po ll(int eve nts, long timeout) t hrows IOEx ception { | |
1089 | as sert Threa d.holdsLoc k(blocking Lock()) && !isBlocki ng(); | |
1090 | ||
1091 | sy nchronized (readLock ) { | |
1092 | int n = 0; | |
1093 | try { | |
1094 | begi n(); | |
1095 | sync hronized ( stateLock) { | |
1096 | if (!isOpe n()) | |
1097 | return 0; | |
1098 | readerThre ad = Nativ eThread.cu rrent(); | |
1099 | } | |
1100 | n = Net.poll(f d, events, timeout); | |
1101 | } finall y { | |
1102 | read erThread = 0; | |
1103 | end( n > 0); | |
1104 | } | |
1105 | return n ; | |
1106 | } | |
1107 | } | |
1108 | ||
1109 | /** | |
1110 | * Tra nslates an interest operation set into a native po ll event s et | |
1111 | */ | |
1112 | public void tran slateAndSe tInterestO ps(int ops , Selectio nKeyImpl s k) { | |
1113 | in t newOps = 0; | |
1114 | ||
1115 | if ((ops & S electionKe y.OP_READ) != 0) | |
1116 | newOps | = Net.POLL IN; | |
1117 | if ((ops & S electionKe y.OP_WRITE ) != 0) | |
1118 | newOps | = Net.POLL OUT; | |
1119 | if ((ops & S electionKe y.OP_CONNE CT) != 0) | |
1120 | newOps | = Net.POLL IN; | |
1121 | sk .selector. putEventOp s(sk, newO ps); | |
1122 | } | |
1123 | ||
1124 | public FileDescr iptor getF D() { | |
1125 | re turn fd; | |
1126 | } | |
1127 | ||
1128 | public int getFD Val() { | |
1129 | re turn fdVal ; | |
1130 | } | |
1131 | ||
1132 | ||
1133 | // -- Native met hods -- | |
1134 | ||
1135 | privat e static n ative void initIDs() ; | |
1136 | ||
1137 | privat e static n ative void disconnec t0(FileDes criptor fd , boolean isIPv6) | |
1138 | th rows IOExc eption; | |
1139 | ||
1140 | privat e native i nt receive 0(FileDesc riptor fd, long addr ess, int l en, | |
1141 | boolean connected) | |
1142 | th rows IOExc eption; | |
1143 | ||
1144 | privat e native i nt send0(b oolean pre ferIPv6, F ileDescrip tor fd, lo ng address , | |
1145 | i nt len, In etAddress addr, int port) | |
1146 | th rows IOExc eption; | |
1147 | ||
1148 | static { | |
1149 | IO Util.load( ); | |
1150 | in itIDs(); | |
1151 | } | |
1152 | ||
1153 | } |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.