Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/native/sun/nio/ch/ServerSocketChannelImpl.c
32288 views
/*1* Copyright (c) 2000, 2010, 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 <stdio.h>29#include <stdlib.h>30#include <malloc.h>31#include <sys/types.h>3233#include "jni.h"34#include "jni_util.h"35#include "jvm.h"36#include "jlong.h"3738#include "nio.h"39#include "nio_util.h"40#include "net_util.h"4142#include "sun_nio_ch_ServerSocketChannelImpl.h"434445static jfieldID fd_fdID; /* java.io.FileDescriptor.fd */46static jclass isa_class; /* java.net.InetSocketAddress */47static jmethodID isa_ctorID; /* InetSocketAddress(InetAddress, int) */484950/**************************************************************51* static method to store field IDs in initializers52*/5354JNIEXPORT void JNICALL55Java_sun_nio_ch_ServerSocketChannelImpl_initIDs(JNIEnv *env, jclass cls)56{57cls = (*env)->FindClass(env, "java/io/FileDescriptor");58CHECK_NULL(cls);59fd_fdID = (*env)->GetFieldID(env, cls, "fd", "I");60CHECK_NULL(fd_fdID);6162cls = (*env)->FindClass(env, "java/net/InetSocketAddress");63CHECK_NULL(cls);64isa_class = (*env)->NewGlobalRef(env, cls);65if (isa_class == NULL) {66JNU_ThrowOutOfMemoryError(env, NULL);67return;68}69isa_ctorID = (*env)->GetMethodID(env, cls, "<init>",70"(Ljava/net/InetAddress;I)V");71CHECK_NULL(isa_ctorID);72}7374JNIEXPORT void JNICALL75Java_sun_nio_ch_ServerSocketChannelImpl_listen(JNIEnv *env, jclass cl,76jobject fdo, jint backlog)77{78if (listen(fdval(env,fdo), backlog) == SOCKET_ERROR) {79NET_ThrowNew(env, WSAGetLastError(), "listen");80}81}8283JNIEXPORT jint JNICALL84Java_sun_nio_ch_ServerSocketChannelImpl_accept0(JNIEnv *env, jobject this,85jobject ssfdo, jobject newfdo,86jobjectArray isaa)87{88jint ssfd = (*env)->GetIntField(env, ssfdo, fd_fdID);89jint newfd;90SOCKETADDRESS sa;91jobject remote_ia;92int remote_port;93jobject isa;94int addrlen = sizeof(sa);9596memset((char *)&sa, 0, sizeof(sa));97newfd = (jint)accept(ssfd, (struct sockaddr *)&sa, &addrlen);98if (newfd == INVALID_SOCKET) {99int theErr = (jint)WSAGetLastError();100if (theErr == WSAEWOULDBLOCK) {101return IOS_UNAVAILABLE;102}103JNU_ThrowIOExceptionWithLastError(env, "Accept failed");104return IOS_THROWN;105}106107SetHandleInformation((HANDLE)(UINT_PTR)newfd, HANDLE_FLAG_INHERIT, 0);108(*env)->SetIntField(env, newfdo, fd_fdID, newfd);109remote_ia = NET_SockaddrToInetAddress(env, (struct sockaddr *)&sa, (int *)&remote_port);110CHECK_NULL_RETURN(remote_ia, IOS_THROWN);111112isa = (*env)->NewObject(env, isa_class, isa_ctorID, remote_ia, remote_port);113CHECK_NULL_RETURN(isa, IOS_THROWN);114(*env)->SetObjectArrayElement(env, isaa, 0, isa);115return 1;116}117118119