author | clanger |
Thu, 27 Oct 2016 11:00:28 +0200 | |
changeset 41771 | 18c9669e76ca |
parent 41210 | 73593ed418ff |
permissions | -rw-r--r-- |
2 | 1 |
/* |
41210
73593ed418ff
8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 1997, 2016, 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 |
*/ |
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41210
diff
changeset
|
25 |
#include <malloc.h> |
2 | 26 |
|
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
41210
diff
changeset
|
27 |
#include "net_util.h" |
2 | 28 |
|
29 |
#include "java_net_SocketInputStream.h" |
|
30 |
||
31 |
/************************************************************************* |
|
32 |
* SocketInputStream |
|
33 |
*/ |
|
34 |
static jfieldID IO_fd_fdID; |
|
35 |
||
36 |
/* |
|
37 |
* Class: java_net_SocketInputStream |
|
38 |
* Method: init |
|
39 |
* Signature: ()V |
|
40 |
*/ |
|
41 |
JNIEXPORT void JNICALL |
|
42 |
Java_java_net_SocketInputStream_init(JNIEnv *env, jclass cls) { |
|
43 |
IO_fd_fdID = NET_GetFileDescriptorID(env); |
|
44 |
} |
|
45 |
||
46 |
/* |
|
47 |
* Class: java_net_SocketInputStream |
|
48 |
* Method: socketRead |
|
49 |
* Signature: (Ljava/io/FileDescriptor;[BIII)I |
|
50 |
*/ |
|
51 |
JNIEXPORT jint JNICALL |
|
52 |
Java_java_net_SocketInputStream_socketRead0(JNIEnv *env, jobject this, |
|
53 |
jobject fdObj, jbyteArray data, |
|
54 |
jint off, jint len, jint timeout) |
|
55 |
{ |
|
41210
73593ed418ff
8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents:
25859
diff
changeset
|
56 |
char BUF[MAX_BUFFER_LEN]; |
2 | 57 |
char *bufP; |
41210
73593ed418ff
8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents:
25859
diff
changeset
|
58 |
jint fd, newfd, nread; |
2 | 59 |
|
60 |
if (IS_NULL(fdObj)) { |
|
41210
73593ed418ff
8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents:
25859
diff
changeset
|
61 |
JNU_ThrowByName(env, "java/net/SocketException", |
73593ed418ff
8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents:
25859
diff
changeset
|
62 |
"Socket closed"); |
2 | 63 |
return -1; |
64 |
} |
|
65 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
66 |
if (fd == -1) { |
|
41210
73593ed418ff
8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents:
25859
diff
changeset
|
67 |
JNU_ThrowByName(env, "java/net/SocketException", "Socket closed"); |
2 | 68 |
return -1; |
69 |
} |
|
70 |
||
71 |
/* |
|
72 |
* If the caller buffer is large than our stack buffer then we allocate |
|
73 |
* from the heap (up to a limit). If memory is exhausted we always use |
|
74 |
* the stack buffer. |
|
75 |
*/ |
|
76 |
if (len <= MAX_BUFFER_LEN) { |
|
77 |
bufP = BUF; |
|
78 |
} else { |
|
79 |
if (len > MAX_HEAP_BUFFER_LEN) { |
|
80 |
len = MAX_HEAP_BUFFER_LEN; |
|
81 |
} |
|
82 |
bufP = (char *)malloc((size_t)len); |
|
83 |
if (bufP == NULL) { |
|
84 |
/* allocation failed so use stack buffer */ |
|
85 |
bufP = BUF; |
|
86 |
len = MAX_BUFFER_LEN; |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
||
91 |
if (timeout) { |
|
92 |
if (timeout <= 5000 || !isRcvTimeoutSupported) { |
|
93 |
int ret = NET_Timeout (fd, timeout); |
|
94 |
||
95 |
if (ret <= 0) { |
|
96 |
if (ret == 0) { |
|
41210
73593ed418ff
8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents:
25859
diff
changeset
|
97 |
JNU_ThrowByName(env, "java/net/SocketTimeoutException", |
2 | 98 |
"Read timed out"); |
23015
73b21ab36615
8034174: Remove use of JVM_* functions from java.net code
chegar
parents:
23010
diff
changeset
|
99 |
} else if (ret == -1) { |
41210
73593ed418ff
8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents:
25859
diff
changeset
|
100 |
JNU_ThrowByName(env, "java/net/SocketException", "socket closed"); |
2 | 101 |
} |
102 |
if (bufP != BUF) { |
|
103 |
free(bufP); |
|
104 |
} |
|
105 |
return -1; |
|
106 |
} |
|
107 |
||
108 |
/*check if the socket has been closed while we were in timeout*/ |
|
109 |
newfd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
110 |
if (newfd == -1) { |
|
41210
73593ed418ff
8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents:
25859
diff
changeset
|
111 |
JNU_ThrowByName(env, "java/net/SocketException", "Socket closed"); |
4508
6675f4c35817
6909089: Memory leak occurs by lack of free for read buffer in SocketInputStream#read()
chegar
parents:
2
diff
changeset
|
112 |
if (bufP != BUF) { |
6675f4c35817
6909089: Memory leak occurs by lack of free for read buffer in SocketInputStream#read()
chegar
parents:
2
diff
changeset
|
113 |
free(bufP); |
6675f4c35817
6909089: Memory leak occurs by lack of free for read buffer in SocketInputStream#read()
chegar
parents:
2
diff
changeset
|
114 |
} |
2 | 115 |
return -1; |
116 |
} |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
nread = recv(fd, bufP, len, 0); |
|
121 |
if (nread > 0) { |
|
122 |
(*env)->SetByteArrayRegion(env, data, off, nread, (jbyte *)bufP); |
|
123 |
} else { |
|
124 |
if (nread < 0) { |
|
16866
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
125 |
// Check if the socket has been closed since we last checked. |
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
126 |
// This could be a reason for recv failing. |
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
127 |
if ((*env)->GetIntField(env, fdObj, IO_fd_fdID) == -1) { |
41210
73593ed418ff
8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents:
25859
diff
changeset
|
128 |
JNU_ThrowByName(env, "java/net/SocketException", "Socket closed"); |
16866
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
129 |
} else { |
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
130 |
switch (WSAGetLastError()) { |
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
131 |
case WSAEINTR: |
41210
73593ed418ff
8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents:
25859
diff
changeset
|
132 |
JNU_ThrowByName(env, "java/net/SocketException", |
16866
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
133 |
"socket closed"); |
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
134 |
break; |
2 | 135 |
|
16866
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
136 |
case WSAECONNRESET: |
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
137 |
case WSAESHUTDOWN: |
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
138 |
/* |
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
139 |
* Connection has been reset - Windows sometimes reports |
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
140 |
* the reset as a shutdown error. |
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
141 |
*/ |
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
142 |
JNU_ThrowByName(env, "sun/net/ConnectionResetException", |
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
143 |
""); |
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
144 |
break; |
2 | 145 |
|
16866
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
146 |
case WSAETIMEDOUT : |
41210
73593ed418ff
8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents:
25859
diff
changeset
|
147 |
JNU_ThrowByName(env, "java/net/SocketTimeoutException", |
16866
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
148 |
"Read timed out"); |
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
149 |
break; |
2 | 150 |
|
16866
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
151 |
default: |
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
152 |
NET_ThrowCurrent(env, "recv failed"); |
e659009ee08d
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents:
5506
diff
changeset
|
153 |
} |
2 | 154 |
} |
155 |
} |
|
156 |
} |
|
157 |
if (bufP != BUF) { |
|
158 |
free(bufP); |
|
159 |
} |
|
160 |
return nread; |
|
161 |
} |