author | clanger |
Thu, 27 Oct 2016 11:00:28 +0200 | |
changeset 41771 | 18c9669e76ca |
parent 25859 | 3317bb8137f4 |
child 43202 | 29180c8db039 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
41771
18c9669e76ca
8167481: cleanup of headers and includes for native 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:
25859
diff
changeset
|
25 |
#include <malloc.h> |
2 | 26 |
|
41771
18c9669e76ca
8167481: cleanup of headers and includes for native libnet
clanger
parents:
25859
diff
changeset
|
27 |
#include "net_util.h" |
2 | 28 |
|
29 |
#include "java_net_SocketOutputStream.h" |
|
30 |
||
31 |
/************************************************************************ |
|
32 |
* SocketOutputStream |
|
33 |
*/ |
|
34 |
static jfieldID IO_fd_fdID; |
|
35 |
||
36 |
/* |
|
37 |
* Class: java_net_SocketOutputStream |
|
38 |
* Method: init |
|
39 |
* Signature: ()V |
|
40 |
*/ |
|
41 |
JNIEXPORT void JNICALL |
|
42 |
Java_java_net_SocketOutputStream_init(JNIEnv *env, jclass cls) { |
|
43 |
IO_fd_fdID = NET_GetFileDescriptorID(env); |
|
44 |
} |
|
45 |
||
46 |
/* |
|
47 |
* Class: java_net_SocketOutputStream |
|
48 |
* Method: socketWrite |
|
49 |
* Signature: (Ljava/io/FileDescriptor;[BII)V |
|
50 |
*/ |
|
51 |
JNIEXPORT void JNICALL |
|
52 |
Java_java_net_SocketOutputStream_socketWrite0(JNIEnv *env, jobject this, |
|
53 |
jobject fdObj, jbyteArray data, |
|
54 |
jint off, jint len) { |
|
55 |
char *bufP; |
|
56 |
char BUF[MAX_BUFFER_LEN]; |
|
57 |
int buflen; |
|
58 |
int fd; |
|
59 |
||
60 |
if (IS_NULL(fdObj)) { |
|
61 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); |
|
62 |
return; |
|
63 |
} else { |
|
64 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID); |
|
65 |
} |
|
66 |
if (IS_NULL(data)) { |
|
67 |
JNU_ThrowNullPointerException(env, "data argument"); |
|
68 |
return; |
|
69 |
} |
|
70 |
||
71 |
/* |
|
72 |
* Use stack allocate buffer if possible. For large sizes we allocate |
|
73 |
* an intermediate buffer from the heap (up to a maximum). If heap is |
|
74 |
* unavailable just use our stack buffer. |
|
75 |
*/ |
|
76 |
if (len <= MAX_BUFFER_LEN) { |
|
77 |
bufP = BUF; |
|
78 |
buflen = MAX_BUFFER_LEN; |
|
79 |
} else { |
|
80 |
buflen = min(MAX_HEAP_BUFFER_LEN, len); |
|
81 |
bufP = (char *)malloc((size_t)buflen); |
|
82 |
if (bufP == NULL) { |
|
83 |
bufP = BUF; |
|
84 |
buflen = MAX_BUFFER_LEN; |
|
85 |
} |
|
86 |
} |
|
87 |
||
88 |
while(len > 0) { |
|
89 |
int loff = 0; |
|
90 |
int chunkLen = min(buflen, len); |
|
91 |
int llen = chunkLen; |
|
92 |
int retry = 0; |
|
93 |
||
94 |
(*env)->GetByteArrayRegion(env, data, off, chunkLen, (jbyte *)bufP); |
|
95 |
||
96 |
while(llen > 0) { |
|
97 |
int n = send(fd, bufP + loff, llen, 0); |
|
98 |
if (n > 0) { |
|
99 |
llen -= n; |
|
100 |
loff += n; |
|
101 |
continue; |
|
102 |
} |
|
103 |
||
104 |
/* |
|
105 |
* Due to a bug in Windows Sockets (observed on NT and Windows |
|
106 |
* 2000) it may be necessary to retry the send. The issue is that |
|
107 |
* on blocking sockets send/WSASend is supposed to block if there |
|
108 |
* is insufficient buffer space available. If there are a large |
|
109 |
* number of threads blocked on write due to congestion then it's |
|
110 |
* possile to hit the NT/2000 bug whereby send returns WSAENOBUFS. |
|
111 |
* The workaround we use is to retry the send. If we have a |
|
112 |
* large buffer to send (>2k) then we retry with a maximum of |
|
113 |
* 2k buffer. If we hit the issue with <=2k buffer then we backoff |
|
114 |
* for 1 second and retry again. We repeat this up to a reasonable |
|
115 |
* limit before bailing out and throwing an exception. In load |
|
116 |
* conditions we've observed that the send will succeed after 2-3 |
|
117 |
* attempts but this depends on network buffers associated with |
|
118 |
* other sockets draining. |
|
119 |
*/ |
|
120 |
if (WSAGetLastError() == WSAENOBUFS) { |
|
121 |
if (llen > MAX_BUFFER_LEN) { |
|
122 |
buflen = MAX_BUFFER_LEN; |
|
123 |
chunkLen = MAX_BUFFER_LEN; |
|
124 |
llen = MAX_BUFFER_LEN; |
|
125 |
continue; |
|
126 |
} |
|
127 |
if (retry >= 30) { |
|
128 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", |
|
129 |
"No buffer space available - exhausted attempts to queue buffer"); |
|
130 |
if (bufP != BUF) { |
|
131 |
free(bufP); |
|
132 |
} |
|
133 |
return; |
|
134 |
} |
|
135 |
Sleep(1000); |
|
136 |
retry++; |
|
137 |
continue; |
|
138 |
} |
|
139 |
||
140 |
/* |
|
141 |
* Send failed - can be caused by close or write error. |
|
142 |
*/ |
|
143 |
if (WSAGetLastError() == WSAENOTSOCK) { |
|
144 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed"); |
|
145 |
} else { |
|
146 |
NET_ThrowCurrent(env, "socket write error"); |
|
147 |
} |
|
148 |
if (bufP != BUF) { |
|
149 |
free(bufP); |
|
150 |
} |
|
151 |
return; |
|
152 |
} |
|
153 |
len -= chunkLen; |
|
154 |
off += chunkLen; |
|
155 |
} |
|
156 |
||
157 |
if (bufP != BUF) { |
|
158 |
free(bufP); |
|
159 |
} |
|
160 |
} |