author | michaelm |
Wed, 30 Oct 2019 11:53:07 +0000 | |
branch | unixdomainchannels |
changeset 58856 | b2f0339a4cad |
parent 58847 | 692de65ab293 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54154
1caf2daef7cf
8220738: (sc) Move ServerSocketChannelImpl remaining native method to Net
alanb
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2000, 2019, 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 <io.h> |
|
29 |
#include "jni.h" |
|
30 |
#include "jni_util.h" |
|
31 |
#include "jvm.h" |
|
32 |
#include "jlong.h" |
|
33 |
||
34 |
#include "nio.h" |
|
35 |
#include "nio_util.h" |
|
22646
5fa3669fd35d
8025306: Inet[4|6]Address class and fieldID initialization in networking native code
chegar
parents:
22631
diff
changeset
|
36 |
#include "net_util.h" |
2 | 37 |
#include "sun_nio_ch_IOUtil.h" |
38 |
||
39 |
/* field id for jlong 'handle' in java.io.FileDescriptor used for file fds */ |
|
40 |
static jfieldID handle_fdID; |
|
41 |
||
42 |
/* field id for jint 'fd' in java.io.FileDescriptor used for socket fds */ |
|
43 |
static jfieldID fd_fdID; |
|
44 |
||
45 |
JNIEXPORT jboolean JNICALL |
|
46 |
Java_sun_security_provider_NativeSeedGenerator_nativeGenerateSeed |
|
47 |
(JNIEnv *env, jclass clazz, jbyteArray randArray); |
|
48 |
||
49 |
/************************************************************** |
|
50 |
* static method to store field IDs in initializers |
|
51 |
*/ |
|
52 |
||
53 |
JNIEXPORT void JNICALL |
|
54 |
Java_sun_nio_ch_IOUtil_initIDs(JNIEnv *env, jclass clazz) |
|
55 |
{ |
|
22631
ac85b05a53f4
8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents:
14342
diff
changeset
|
56 |
CHECK_NULL(clazz = (*env)->FindClass(env, "java/io/FileDescriptor")); |
ac85b05a53f4
8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents:
14342
diff
changeset
|
57 |
CHECK_NULL(fd_fdID = (*env)->GetFieldID(env, clazz, "fd", "I")); |
ac85b05a53f4
8028792: (ch) Channels native code needs to be checked for methods calling JNI with pending excepitons
alanb
parents:
14342
diff
changeset
|
58 |
CHECK_NULL(handle_fdID = (*env)->GetFieldID(env, clazz, "handle", "J")); |
2 | 59 |
} |
60 |
||
61 |
/************************************************************** |
|
62 |
* IOUtil.c |
|
63 |
*/ |
|
64 |
JNIEXPORT jboolean JNICALL |
|
65 |
Java_sun_nio_ch_IOUtil_randomBytes(JNIEnv *env, jclass clazz, |
|
66 |
jbyteArray randArray) |
|
67 |
{ |
|
68 |
return |
|
69 |
Java_sun_security_provider_NativeSeedGenerator_nativeGenerateSeed(env, |
|
70 |
clazz, |
|
71 |
randArray); |
|
72 |
} |
|
73 |
||
13024
ada1a7c54e84
7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents:
5506
diff
changeset
|
74 |
JNIEXPORT jint JNICALL |
ada1a7c54e84
7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents:
5506
diff
changeset
|
75 |
Java_sun_nio_ch_IOUtil_iovMax(JNIEnv *env, jclass this) |
ada1a7c54e84
7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents:
5506
diff
changeset
|
76 |
{ |
ada1a7c54e84
7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents:
5506
diff
changeset
|
77 |
return 16; |
ada1a7c54e84
7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents:
5506
diff
changeset
|
78 |
} |
ada1a7c54e84
7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents:
5506
diff
changeset
|
79 |
|
ada1a7c54e84
7176485: (bf) Allow temporary buffer cache to grow to IOV_MAX
alanb
parents:
5506
diff
changeset
|
80 |
|
2 | 81 |
jint |
82 |
convertReturnVal(JNIEnv *env, jint n, jboolean reading) |
|
83 |
{ |
|
84 |
if (n > 0) /* Number of bytes written */ |
|
85 |
return n; |
|
86 |
if (n == 0) { |
|
87 |
if (reading) { |
|
88 |
return IOS_EOF; /* EOF is -1 in javaland */ |
|
89 |
} else { |
|
90 |
return 0; |
|
91 |
} |
|
92 |
} |
|
93 |
JNU_ThrowIOExceptionWithLastError(env, "Read/write failed"); |
|
94 |
return IOS_THROWN; |
|
95 |
} |
|
96 |
||
97 |
jlong |
|
98 |
convertLongReturnVal(JNIEnv *env, jlong n, jboolean reading) |
|
99 |
{ |
|
100 |
if (n > 0) /* Number of bytes written */ |
|
101 |
return n; |
|
102 |
if (n == 0) { |
|
103 |
if (reading) { |
|
104 |
return IOS_EOF; /* EOF is -1 in javaland */ |
|
105 |
} else { |
|
106 |
return 0; |
|
107 |
} |
|
108 |
} |
|
109 |
JNU_ThrowIOExceptionWithLastError(env, "Read/write failed"); |
|
110 |
return IOS_THROWN; |
|
111 |
} |
|
112 |
||
113 |
JNIEXPORT jint JNICALL |
|
114 |
Java_sun_nio_ch_IOUtil_fdVal(JNIEnv *env, jclass clazz, jobject fdo) |
|
115 |
{ |
|
116 |
return fdval(env, fdo); |
|
117 |
} |
|
118 |
||
119 |
JNIEXPORT void JNICALL |
|
120 |
Java_sun_nio_ch_IOUtil_setfdVal(JNIEnv *env, jclass clazz, jobject fdo, jint val) |
|
121 |
{ |
|
54154
1caf2daef7cf
8220738: (sc) Move ServerSocketChannelImpl remaining native method to Net
alanb
parents:
47216
diff
changeset
|
122 |
setfdval(env, fdo, val); |
2 | 123 |
} |
124 |
||
125 |
||
126 |
#define SET_BLOCKING 0 |
|
127 |
#define SET_NONBLOCKING 1 |
|
128 |
||
129 |
JNIEXPORT void JNICALL |
|
130 |
Java_sun_nio_ch_IOUtil_configureBlocking(JNIEnv *env, jclass clazz, |
|
131 |
jobject fdo, jboolean blocking) |
|
132 |
{ |
|
133 |
u_long argp; |
|
134 |
int result = 0; |
|
135 |
jint fd = fdval(env, fdo); |
|
136 |
||
137 |
if (blocking == JNI_FALSE) { |
|
138 |
argp = SET_NONBLOCKING; |
|
139 |
} else { |
|
140 |
argp = SET_BLOCKING; |
|
141 |
/* Blocking fd cannot be registered with EventSelect */ |
|
142 |
WSAEventSelect(fd, NULL, 0); |
|
143 |
} |
|
144 |
result = ioctlsocket(fd, FIONBIO, &argp); |
|
145 |
if (result == SOCKET_ERROR) { |
|
146 |
int error = WSAGetLastError(); |
|
147 |
handleSocketError(env, (jint)error); |
|
148 |
} |
|
149 |
} |
|
150 |
||
151 |
/* Note: Drain uses the int fd value. It is currently not called |
|
152 |
on windows. |
|
153 |
*/ |
|
154 |
JNIEXPORT jboolean JNICALL |
|
155 |
Java_sun_nio_ch_IOUtil_drain(JNIEnv *env, jclass cl, jint fd) |
|
156 |
{ |
|
157 |
DWORD read = 0; |
|
158 |
int totalRead = 0; |
|
159 |
BOOL result = 0; |
|
160 |
HANDLE h = (HANDLE)_get_osfhandle(fd); |
|
161 |
char buf[128]; |
|
162 |
||
163 |
if (h == INVALID_HANDLE_VALUE) { |
|
164 |
JNU_ThrowIOExceptionWithLastError(env, "Read failed"); |
|
165 |
return JNI_FALSE; |
|
166 |
} |
|
167 |
||
168 |
for (;;) { |
|
169 |
result = ReadFile(h, /* File handle to read */ |
|
170 |
(LPVOID)&buf, /* address to put data */ |
|
171 |
128, /* number of bytes to read */ |
|
172 |
&read, /* number of bytes read */ |
|
173 |
NULL); /* no overlapped struct */ |
|
174 |
||
175 |
if (result == 0) { |
|
176 |
int error = GetLastError(); |
|
177 |
if (error == ERROR_NO_DATA) { |
|
178 |
return (totalRead > 0) ? JNI_TRUE : JNI_FALSE; |
|
179 |
} |
|
180 |
JNU_ThrowIOExceptionWithLastError(env, "Drain"); |
|
181 |
return JNI_FALSE; |
|
182 |
} |
|
183 |
if (read > 0) { |
|
184 |
totalRead += read; |
|
185 |
} else { |
|
186 |
break; |
|
187 |
} |
|
188 |
} |
|
189 |
return (totalRead > 0) ? JNI_TRUE : JNI_FALSE; |
|
190 |
} |
|
191 |
||
192 |
/* Note: This function returns the int fd value from file descriptor. |
|
193 |
It is mostly used for sockets which should use the int fd value. |
|
194 |
*/ |
|
58856 | 195 |
jint |
196 |
fdval(JNIEnv *env, jobject fdo) |
|
2 | 197 |
{ |
198 |
return (*env)->GetIntField(env, fdo, fd_fdID); |
|
199 |
} |
|
200 |
||
58856 | 201 |
void |
202 |
setfdval(JNIEnv *env, jobject fdo, jint val) |
|
54154
1caf2daef7cf
8220738: (sc) Move ServerSocketChannelImpl remaining native method to Net
alanb
parents:
47216
diff
changeset
|
203 |
{ |
1caf2daef7cf
8220738: (sc) Move ServerSocketChannelImpl remaining native method to Net
alanb
parents:
47216
diff
changeset
|
204 |
(*env)->SetIntField(env, fdo, fd_fdID, val); |
1caf2daef7cf
8220738: (sc) Move ServerSocketChannelImpl remaining native method to Net
alanb
parents:
47216
diff
changeset
|
205 |
} |
1caf2daef7cf
8220738: (sc) Move ServerSocketChannelImpl remaining native method to Net
alanb
parents:
47216
diff
changeset
|
206 |
|
2 | 207 |
jlong |
208 |
handleval(JNIEnv *env, jobject fdo) |
|
209 |
{ |
|
210 |
return (*env)->GetLongField(env, fdo, handle_fdID); |
|
211 |
} |