Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/native/sun/nio/ch/SocketChannelImpl.c
32288 views
/*1* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#include <windows.h>26#include <winsock2.h>27#include <ctype.h>28#include "jni.h"29#include "jni_util.h"30#include "jvm.h"31#include "jlong.h"32#include "sun_nio_ch_SocketChannelImpl.h"3334#include "nio.h"35#include "nio_util.h"36#include "net_util.h"373839static jfieldID ia_addrID; /* java.net.InetAddress.address */4041JNIEXPORT void JNICALL42Java_sun_nio_ch_SocketChannelImpl_initIDs(JNIEnv *env, jclass cls)43{44CHECK_NULL(cls = (*env)->FindClass(env, "java/net/InetAddress"));45CHECK_NULL(ia_addrID = (*env)->GetFieldID(env, cls, "address", "I"));46}4748jint49handleSocketError(JNIEnv *env, int errorValue)50{51NET_ThrowNew(env, errorValue, NULL);52return IOS_THROWN;53}5455JNIEXPORT jint JNICALL56Java_sun_nio_ch_SocketChannelImpl_checkConnect(JNIEnv *env, jobject this,57jobject fdo, jboolean block,58jboolean ready)59{60int optError = 0;61int lastError = 0;62int result = 0;63int retry = 0;64int n = sizeof(int);65jint fd = fdval(env, fdo);66fd_set wr, ex;67struct timeval t = { 0, 0 };6869FD_ZERO(&wr);70FD_ZERO(&ex);71FD_SET((u_int)fd, &wr);72FD_SET((u_int)fd, &ex);7374result = select(fd+1, 0, &wr, &ex, block ? NULL : &t);7576/* save last winsock error */77if (result == SOCKET_ERROR) {78lastError = WSAGetLastError();79}8081if (block) { /* must configure socket back to blocking state */82u_long argp = 0;83int r = ioctlsocket(fd, FIONBIO, &argp);84if (r == SOCKET_ERROR) {85handleSocketError(env, WSAGetLastError());86}87}8889if (result == 0) { /* timeout */90return block ? 0 : IOS_UNAVAILABLE;91} else {92if (result == SOCKET_ERROR) { /* select failed */93handleSocketError(env, lastError);94return IOS_THROWN;95}96}9798/*99* Socket is writable or error occurred. On some Windows editions100* the socket will appear writable when the connect fails so we101* check for error rather than writable.102*/103if (!FD_ISSET(fd, &ex)) {104return 1; /* connection established */105}106107/*108* A getsockopt( SO_ERROR ) may indicate success on NT4 even109* though the connection has failed. The workaround is to allow110* winsock to be scheduled and this is done via by yielding.111* As the yield approach is problematic in heavy load situations112* we attempt up to 3 times to get the failure reason.113*/114for (retry=0; retry<3; retry++) {115result = getsockopt((SOCKET)fd,116SOL_SOCKET,117SO_ERROR,118(char *)&optError,119&n);120if (result == SOCKET_ERROR) {121int lastError = WSAGetLastError();122if (lastError == WSAEINPROGRESS) {123return IOS_UNAVAILABLE;124}125NET_ThrowNew(env, lastError, "getsockopt");126return IOS_THROWN;127}128if (optError) {129break;130}131Sleep(0);132}133134if (optError != NO_ERROR) {135handleSocketError(env, optError);136return IOS_THROWN;137}138139return 0;140}141142JNIEXPORT jint JNICALL143Java_sun_nio_ch_SocketChannelImpl_sendOutOfBandData(JNIEnv* env, jclass this,144jobject fdo, jbyte b)145{146int n = send(fdval(env, fdo), (const char*)&b, 1, MSG_OOB);147if (n == SOCKET_ERROR) {148handleSocketError(env, WSAGetLastError());149return IOS_THROWN;150} else {151return n;152}153}154155156