author | alanb |
Tue, 06 Feb 2018 16:04:46 +0000 | |
changeset 48750 | ffbb784a8873 |
parent 47216 | 71c04702a3d5 |
child 49141 | ac95c7a76132 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
#include <windows.h> |
|
27 |
#include <winsock2.h> |
|
28 |
#include <ctype.h> |
|
29 |
#include "jni.h" |
|
30 |
#include "jni_util.h" |
|
31 |
#include "jvm.h" |
|
32 |
#include "jlong.h" |
|
33 |
#include "sun_nio_ch_SocketChannelImpl.h" |
|
34 |
||
35 |
#include "nio.h" |
|
36 |
#include "nio_util.h" |
|
37 |
#include "net_util.h" |
|
38 |
||
39 |
||
40 |
static jfieldID ia_addrID; /* java.net.InetAddress.address */ |
|
41 |
||
42 |
JNIEXPORT void JNICALL |
|
43 |
Java_sun_nio_ch_SocketChannelImpl_initIDs(JNIEnv *env, jclass cls) |
|
44 |
{ |
|
22631
ac85b05a53f4
8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents:
21278
diff
changeset
|
45 |
CHECK_NULL(cls = (*env)->FindClass(env, "java/net/InetAddress")); |
ac85b05a53f4
8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents:
21278
diff
changeset
|
46 |
CHECK_NULL(ia_addrID = (*env)->GetFieldID(env, cls, "address", "I")); |
2 | 47 |
} |
48 |
||
49 |
jint |
|
50 |
handleSocketError(JNIEnv *env, int errorValue) |
|
51 |
{ |
|
52 |
NET_ThrowNew(env, errorValue, NULL); |
|
53 |
return IOS_THROWN; |
|
54 |
} |
|
55 |
||
56 |
JNIEXPORT jint JNICALL |
|
57 |
Java_sun_nio_ch_SocketChannelImpl_checkConnect(JNIEnv *env, jobject this, |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
58 |
jobject fdo, jboolean block) |
2 | 59 |
{ |
60 |
int optError = 0; |
|
61 |
int lastError = 0; |
|
62 |
int result = 0; |
|
63 |
int retry = 0; |
|
64 |
int n = sizeof(int); |
|
65 |
jint fd = fdval(env, fdo); |
|
66 |
fd_set wr, ex; |
|
67 |
struct timeval t = { 0, 0 }; |
|
68 |
||
69 |
FD_ZERO(&wr); |
|
70 |
FD_ZERO(&ex); |
|
71 |
FD_SET((u_int)fd, &wr); |
|
72 |
FD_SET((u_int)fd, &ex); |
|
73 |
||
74 |
result = select(fd+1, 0, &wr, &ex, block ? NULL : &t); |
|
75 |
||
76 |
/* save last winsock error */ |
|
77 |
if (result == SOCKET_ERROR) { |
|
78 |
lastError = WSAGetLastError(); |
|
79 |
} |
|
80 |
||
81 |
if (block) { /* must configure socket back to blocking state */ |
|
82 |
u_long argp = 0; |
|
83 |
int r = ioctlsocket(fd, FIONBIO, &argp); |
|
84 |
if (r == SOCKET_ERROR) { |
|
85 |
handleSocketError(env, WSAGetLastError()); |
|
86 |
} |
|
87 |
} |
|
88 |
||
89 |
if (result == 0) { /* timeout */ |
|
90 |
return block ? 0 : IOS_UNAVAILABLE; |
|
91 |
} else { |
|
92 |
if (result == SOCKET_ERROR) { /* select failed */ |
|
93 |
handleSocketError(env, lastError); |
|
94 |
return IOS_THROWN; |
|
95 |
} |
|
96 |
} |
|
97 |
||
98 |
/* |
|
21278 | 99 |
* Socket is writable or error occurred. On some Windows editions |
2 | 100 |
* the socket will appear writable when the connect fails so we |
101 |
* check for error rather than writable. |
|
102 |
*/ |
|
103 |
if (!FD_ISSET(fd, &ex)) { |
|
104 |
return 1; /* connection established */ |
|
105 |
} |
|
106 |
||
107 |
/* |
|
108 |
* A getsockopt( SO_ERROR ) may indicate success on NT4 even |
|
109 |
* though the connection has failed. The workaround is to allow |
|
110 |
* winsock to be scheduled and this is done via by yielding. |
|
111 |
* As the yield approach is problematic in heavy load situations |
|
112 |
* we attempt up to 3 times to get the failure reason. |
|
113 |
*/ |
|
114 |
for (retry=0; retry<3; retry++) { |
|
115 |
result = getsockopt((SOCKET)fd, |
|
116 |
SOL_SOCKET, |
|
117 |
SO_ERROR, |
|
118 |
(char *)&optError, |
|
119 |
&n); |
|
120 |
if (result == SOCKET_ERROR) { |
|
121 |
int lastError = WSAGetLastError(); |
|
122 |
if (lastError == WSAEINPROGRESS) { |
|
123 |
return IOS_UNAVAILABLE; |
|
124 |
} |
|
125 |
NET_ThrowNew(env, lastError, "getsockopt"); |
|
126 |
return IOS_THROWN; |
|
127 |
} |
|
128 |
if (optError) { |
|
129 |
break; |
|
130 |
} |
|
131 |
Sleep(0); |
|
132 |
} |
|
133 |
||
134 |
if (optError != NO_ERROR) { |
|
135 |
handleSocketError(env, optError); |
|
136 |
return IOS_THROWN; |
|
137 |
} |
|
138 |
||
139 |
return 0; |
|
140 |
} |
|
6117
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
141 |
|
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
142 |
JNIEXPORT jint JNICALL |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
143 |
Java_sun_nio_ch_SocketChannelImpl_sendOutOfBandData(JNIEnv* env, jclass this, |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
144 |
jobject fdo, jbyte b) |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
145 |
{ |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
146 |
int n = send(fdval(env, fdo), (const char*)&b, 1, MSG_OOB); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
147 |
if (n == SOCKET_ERROR) { |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
148 |
handleSocketError(env, WSAGetLastError()); |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
149 |
return IOS_THROWN; |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
150 |
} else { |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
151 |
return n; |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
152 |
} |
471ae95609d5
6963907: (so) Socket adapter need to implement sendUrgentData
alanb
parents:
5506
diff
changeset
|
153 |
} |