Path: blob/master/src/java.base/windows/native/libnio/ch/Iocp.c
41134 views
/*1* Copyright (c) 2008, 2019, 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>2627#include "jni.h"28#include "jni_util.h"29#include "jlong.h"30#include "nio.h"31#include "nio_util.h"3233#include "sun_nio_ch_Iocp.h"343536static jfieldID completionStatus_error;37static jfieldID completionStatus_bytesTransferred;38static jfieldID completionStatus_completionKey;39static jfieldID completionStatus_overlapped;404142JNIEXPORT void JNICALL43Java_sun_nio_ch_Iocp_initIDs(JNIEnv* env, jclass this)44{45jclass clazz;4647clazz = (*env)->FindClass(env, "sun/nio/ch/Iocp$CompletionStatus");48CHECK_NULL(clazz);49completionStatus_error = (*env)->GetFieldID(env, clazz, "error", "I");50CHECK_NULL(completionStatus_error);51completionStatus_bytesTransferred = (*env)->GetFieldID(env, clazz, "bytesTransferred", "I");52CHECK_NULL(completionStatus_bytesTransferred);53completionStatus_completionKey = (*env)->GetFieldID(env, clazz, "completionKey", "I");54CHECK_NULL(completionStatus_completionKey);55completionStatus_overlapped = (*env)->GetFieldID(env, clazz, "overlapped", "J");56CHECK_NULL(completionStatus_overlapped);57}5859JNIEXPORT jlong JNICALL60Java_sun_nio_ch_Iocp_createIoCompletionPort(JNIEnv* env, jclass this,61jlong handle, jlong existingPort, jint completionKey, jint concurrency)62{63ULONG_PTR ck = completionKey;64HANDLE port = CreateIoCompletionPort((HANDLE)jlong_to_ptr(handle),65(HANDLE)jlong_to_ptr(existingPort),66ck,67(DWORD)concurrency);68if (port == NULL) {69JNU_ThrowIOExceptionWithLastError(env, "CreateIoCompletionPort failed");70}71return ptr_to_jlong(port);72}7374JNIEXPORT void JNICALL75Java_sun_nio_ch_Iocp_close0(JNIEnv* env, jclass this,76jlong handle)77{78HANDLE h = (HANDLE)jlong_to_ptr(handle);79CloseHandle(h);80}818283JNIEXPORT void JNICALL84Java_sun_nio_ch_Iocp_getQueuedCompletionStatus(JNIEnv* env, jclass this,85jlong completionPort, jobject obj)86{87DWORD bytesTransferred;88ULONG_PTR completionKey;89OVERLAPPED *lpOverlapped;90BOOL res;9192res = GetQueuedCompletionStatus((HANDLE)jlong_to_ptr(completionPort),93&bytesTransferred,94&completionKey,95&lpOverlapped,96INFINITE);97if (res == 0 && lpOverlapped == NULL) {98JNU_ThrowIOExceptionWithLastError(env, "GetQueuedCompletionStatus failed");99} else {100DWORD ioResult = (res == 0) ? GetLastError() : 0;101(*env)->SetIntField(env, obj, completionStatus_error, ioResult);102(*env)->SetIntField(env, obj, completionStatus_bytesTransferred,103(jint)bytesTransferred);104(*env)->SetIntField(env, obj, completionStatus_completionKey,105(jint)completionKey);106(*env)->SetLongField(env, obj, completionStatus_overlapped,107ptr_to_jlong(lpOverlapped));108109}110}111112JNIEXPORT void JNICALL113Java_sun_nio_ch_Iocp_postQueuedCompletionStatus(JNIEnv* env, jclass this,114jlong completionPort, jint completionKey)115{116BOOL res;117118res = PostQueuedCompletionStatus((HANDLE)jlong_to_ptr(completionPort),119(DWORD)0,120(DWORD)completionKey,121NULL);122if (res == 0) {123JNU_ThrowIOExceptionWithLastError(env, "PostQueuedCompletionStatus");124}125}126127JNIEXPORT jstring JNICALL128Java_sun_nio_ch_Iocp_getErrorMessage(JNIEnv* env, jclass this, jint errorCode)129{130WCHAR message[255];131132DWORD len = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM,133NULL,134(DWORD)errorCode,1350,136&message[0],137255,138NULL);139140141if (len == 0) {142return NULL;143} else {144if (len > 3) {145// Drop final '.', CR, LF146if (message[len - 1] == L'\n') len--;147if (message[len - 1] == L'\r') len--;148if (message[len - 1] == L'.') len--;149message[len] = L'\0';150}151152return (*env)->NewString(env, (const jchar *)message, (jsize)wcslen(message));153}154}155156157