Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/windows/native/sun/nio/ch/WindowsAsynchronousServerSocketChannelImpl.c
32288 views
/*1* Copyright (c) 2008, 2009, 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>2728#include "jni.h"29#include "jni_util.h"30#include "jlong.h"31#include "nio.h"32#include "nio_util.h"33#include "net_util.h"3435#include "sun_nio_ch_WindowsAsynchronousServerSocketChannelImpl.h"363738#ifndef WSAID_ACCEPTEX39#define WSAID_ACCEPTEX {0xb5367df1,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}40#endif4142#ifndef SO_UPDATE_ACCEPT_CONTEXT43#define SO_UPDATE_ACCEPT_CONTEXT 0x700B44#endif454647typedef BOOL (*AcceptEx_t)48(49SOCKET sListenSocket,50SOCKET sAcceptSocket,51PVOID lpOutputBuffer,52DWORD dwReceiveDataLength,53DWORD dwLocalAddressLength,54DWORD dwRemoteAddressLength,55LPDWORD lpdwBytesReceived,56LPOVERLAPPED lpOverlapped57);585960static AcceptEx_t AcceptEx_func;616263JNIEXPORT void JNICALL64Java_sun_nio_ch_WindowsAsynchronousServerSocketChannelImpl_initIDs(JNIEnv* env, jclass this) {65GUID GuidAcceptEx = WSAID_ACCEPTEX;66SOCKET s;67int rv;68DWORD dwBytes;6970s = socket(AF_INET, SOCK_STREAM, 0);71if (s == INVALID_SOCKET) {72JNU_ThrowIOExceptionWithLastError(env, "socket failed");73return;74}75rv = WSAIoctl(s,76SIO_GET_EXTENSION_FUNCTION_POINTER,77(LPVOID)&GuidAcceptEx,78sizeof(GuidAcceptEx),79&AcceptEx_func,80sizeof(AcceptEx_func),81&dwBytes,82NULL,83NULL);84if (rv != 0)85JNU_ThrowIOExceptionWithLastError(env, "WSAIoctl failed");86closesocket(s);87}8889JNIEXPORT jint JNICALL90Java_sun_nio_ch_WindowsAsynchronousServerSocketChannelImpl_accept0(JNIEnv* env, jclass this,91jlong listenSocket, jlong acceptSocket, jlong ov, jlong buf)92{93BOOL res;94SOCKET s1 = (SOCKET)jlong_to_ptr(listenSocket);95SOCKET s2 = (SOCKET)jlong_to_ptr(acceptSocket);96PVOID outputBuffer = (PVOID)jlong_to_ptr(buf);9798DWORD nread = 0;99OVERLAPPED* lpOverlapped = (OVERLAPPED*)jlong_to_ptr(ov);100ZeroMemory((PVOID)lpOverlapped, sizeof(OVERLAPPED));101102res = (*AcceptEx_func)(s1,103s2,104outputBuffer,1050,106sizeof(SOCKETADDRESS)+16,107sizeof(SOCKETADDRESS)+16,108&nread,109lpOverlapped);110if (res == 0) {111int error = WSAGetLastError();112if (error == ERROR_IO_PENDING) {113return IOS_UNAVAILABLE;114}115JNU_ThrowIOExceptionWithLastError(env, "AcceptEx failed");116return IOS_THROWN;117}118119return 0;120}121122JNIEXPORT void JNICALL123Java_sun_nio_ch_WindowsAsynchronousServerSocketChannelImpl_updateAcceptContext(JNIEnv* env, jclass this,124jlong listenSocket, jlong acceptSocket)125{126SOCKET s1 = (SOCKET)jlong_to_ptr(listenSocket);127SOCKET s2 = (SOCKET)jlong_to_ptr(acceptSocket);128129setsockopt(s2, SOL_SOCKET, SO_UPDATE_ACCEPT_CONTEXT, (char *)&s1, sizeof(s1));130}131132133JNIEXPORT void JNICALL134Java_sun_nio_ch_WindowsAsynchronousServerSocketChannelImpl_closesocket0(JNIEnv* env, jclass this,135jlong socket)136{137SOCKET s = (SOCKET)jlong_to_ptr(socket);138139if (closesocket(s) == SOCKET_ERROR)140JNU_ThrowIOExceptionWithLastError(env, "closesocket failed");141}142143144