2
|
1 |
/*
|
|
2 |
* Copyright 1997-2003 Sun Microsystems, Inc. All Rights Reserved.
|
|
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
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
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 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
#include <windows.h>
|
|
27 |
#include <winsock2.h>
|
|
28 |
#include <ctype.h>
|
|
29 |
#include <stdio.h>
|
|
30 |
#include <stdlib.h>
|
|
31 |
#include <malloc.h>
|
|
32 |
#include <sys/types.h>
|
|
33 |
|
|
34 |
#include "java_net_SocketOutputStream.h"
|
|
35 |
|
|
36 |
#include "net_util.h"
|
|
37 |
#include "jni_util.h"
|
|
38 |
|
|
39 |
/************************************************************************
|
|
40 |
* SocketOutputStream
|
|
41 |
*/
|
|
42 |
static jfieldID IO_fd_fdID;
|
|
43 |
|
|
44 |
/*
|
|
45 |
* Class: java_net_SocketOutputStream
|
|
46 |
* Method: init
|
|
47 |
* Signature: ()V
|
|
48 |
*/
|
|
49 |
JNIEXPORT void JNICALL
|
|
50 |
Java_java_net_SocketOutputStream_init(JNIEnv *env, jclass cls) {
|
|
51 |
IO_fd_fdID = NET_GetFileDescriptorID(env);
|
|
52 |
}
|
|
53 |
|
|
54 |
/*
|
|
55 |
* Class: java_net_SocketOutputStream
|
|
56 |
* Method: socketWrite
|
|
57 |
* Signature: (Ljava/io/FileDescriptor;[BII)V
|
|
58 |
*/
|
|
59 |
JNIEXPORT void JNICALL
|
|
60 |
Java_java_net_SocketOutputStream_socketWrite0(JNIEnv *env, jobject this,
|
|
61 |
jobject fdObj, jbyteArray data,
|
|
62 |
jint off, jint len) {
|
|
63 |
char *bufP;
|
|
64 |
char BUF[MAX_BUFFER_LEN];
|
|
65 |
int buflen;
|
|
66 |
int fd;
|
|
67 |
jint n;
|
|
68 |
|
|
69 |
if (IS_NULL(fdObj)) {
|
|
70 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
|
|
71 |
return;
|
|
72 |
} else {
|
|
73 |
fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
|
|
74 |
}
|
|
75 |
if (IS_NULL(data)) {
|
|
76 |
JNU_ThrowNullPointerException(env, "data argument");
|
|
77 |
return;
|
|
78 |
}
|
|
79 |
|
|
80 |
/*
|
|
81 |
* Use stack allocate buffer if possible. For large sizes we allocate
|
|
82 |
* an intermediate buffer from the heap (up to a maximum). If heap is
|
|
83 |
* unavailable just use our stack buffer.
|
|
84 |
*/
|
|
85 |
if (len <= MAX_BUFFER_LEN) {
|
|
86 |
bufP = BUF;
|
|
87 |
buflen = MAX_BUFFER_LEN;
|
|
88 |
} else {
|
|
89 |
buflen = min(MAX_HEAP_BUFFER_LEN, len);
|
|
90 |
bufP = (char *)malloc((size_t)buflen);
|
|
91 |
if (bufP == NULL) {
|
|
92 |
bufP = BUF;
|
|
93 |
buflen = MAX_BUFFER_LEN;
|
|
94 |
}
|
|
95 |
}
|
|
96 |
|
|
97 |
while(len > 0) {
|
|
98 |
int loff = 0;
|
|
99 |
int chunkLen = min(buflen, len);
|
|
100 |
int llen = chunkLen;
|
|
101 |
int retry = 0;
|
|
102 |
|
|
103 |
(*env)->GetByteArrayRegion(env, data, off, chunkLen, (jbyte *)bufP);
|
|
104 |
|
|
105 |
while(llen > 0) {
|
|
106 |
int n = send(fd, bufP + loff, llen, 0);
|
|
107 |
if (n > 0) {
|
|
108 |
llen -= n;
|
|
109 |
loff += n;
|
|
110 |
continue;
|
|
111 |
}
|
|
112 |
|
|
113 |
/*
|
|
114 |
* Due to a bug in Windows Sockets (observed on NT and Windows
|
|
115 |
* 2000) it may be necessary to retry the send. The issue is that
|
|
116 |
* on blocking sockets send/WSASend is supposed to block if there
|
|
117 |
* is insufficient buffer space available. If there are a large
|
|
118 |
* number of threads blocked on write due to congestion then it's
|
|
119 |
* possile to hit the NT/2000 bug whereby send returns WSAENOBUFS.
|
|
120 |
* The workaround we use is to retry the send. If we have a
|
|
121 |
* large buffer to send (>2k) then we retry with a maximum of
|
|
122 |
* 2k buffer. If we hit the issue with <=2k buffer then we backoff
|
|
123 |
* for 1 second and retry again. We repeat this up to a reasonable
|
|
124 |
* limit before bailing out and throwing an exception. In load
|
|
125 |
* conditions we've observed that the send will succeed after 2-3
|
|
126 |
* attempts but this depends on network buffers associated with
|
|
127 |
* other sockets draining.
|
|
128 |
*/
|
|
129 |
if (WSAGetLastError() == WSAENOBUFS) {
|
|
130 |
if (llen > MAX_BUFFER_LEN) {
|
|
131 |
buflen = MAX_BUFFER_LEN;
|
|
132 |
chunkLen = MAX_BUFFER_LEN;
|
|
133 |
llen = MAX_BUFFER_LEN;
|
|
134 |
continue;
|
|
135 |
}
|
|
136 |
if (retry >= 30) {
|
|
137 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
|
|
138 |
"No buffer space available - exhausted attempts to queue buffer");
|
|
139 |
if (bufP != BUF) {
|
|
140 |
free(bufP);
|
|
141 |
}
|
|
142 |
return;
|
|
143 |
}
|
|
144 |
Sleep(1000);
|
|
145 |
retry++;
|
|
146 |
continue;
|
|
147 |
}
|
|
148 |
|
|
149 |
/*
|
|
150 |
* Send failed - can be caused by close or write error.
|
|
151 |
*/
|
|
152 |
if (WSAGetLastError() == WSAENOTSOCK) {
|
|
153 |
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
|
|
154 |
} else {
|
|
155 |
NET_ThrowCurrent(env, "socket write error");
|
|
156 |
}
|
|
157 |
if (bufP != BUF) {
|
|
158 |
free(bufP);
|
|
159 |
}
|
|
160 |
return;
|
|
161 |
}
|
|
162 |
len -= chunkLen;
|
|
163 |
off += chunkLen;
|
|
164 |
}
|
|
165 |
|
|
166 |
if (bufP != BUF) {
|
|
167 |
free(bufP);
|
|
168 |
}
|
|
169 |
}
|