893N/A/*
3909N/A * Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
893N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
893N/A *
893N/A * This code is free software; you can redistribute it and/or modify it
893N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
893N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
893N/A *
893N/A * This code is distributed in the hope that it will be useful, but WITHOUT
893N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
893N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
893N/A * version 2 for more details (a copy is included in the LICENSE file that
893N/A * accompanied this code).
893N/A *
893N/A * You should have received a copy of the GNU General Public License version
893N/A * 2 along with this work; if not, write to the Free Software Foundation,
893N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
893N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
893N/A */
893N/A
893N/A#include <windows.h>
893N/A
893N/A#include "jni.h"
893N/A#include "jni_util.h"
893N/A#include "jlong.h"
893N/A#include "nio.h"
893N/A#include "nio_util.h"
893N/A
893N/A#include "sun_nio_ch_Iocp.h"
893N/A
893N/A
893N/Astatic jfieldID completionStatus_error;
893N/Astatic jfieldID completionStatus_bytesTransferred;
893N/Astatic jfieldID completionStatus_completionKey;
893N/Astatic jfieldID completionStatus_overlapped;
893N/A
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_ch_Iocp_initIDs(JNIEnv* env, jclass this)
893N/A{
893N/A jclass clazz;
893N/A
893N/A clazz = (*env)->FindClass(env, "sun/nio/ch/Iocp$CompletionStatus");
893N/A if (clazz == NULL) {
893N/A return;
893N/A }
893N/A completionStatus_error = (*env)->GetFieldID(env, clazz, "error", "I");
893N/A if (completionStatus_error == NULL) return;
893N/A completionStatus_bytesTransferred = (*env)->GetFieldID(env, clazz, "bytesTransferred", "I");
893N/A if (completionStatus_bytesTransferred == NULL) return;
893N/A completionStatus_completionKey = (*env)->GetFieldID(env, clazz, "completionKey", "I");
893N/A if (completionStatus_completionKey == NULL) return;
893N/A completionStatus_overlapped = (*env)->GetFieldID(env, clazz, "overlapped", "J");
893N/A}
893N/A
1580N/AJNIEXPORT jint JNICALL
1580N/AJava_sun_nio_ch_Iocp_osMajorVersion(JNIEnv* env, jclass this)
1580N/A{
1580N/A OSVERSIONINFOEX ver;
1580N/A ver.dwOSVersionInfoSize = sizeof(ver);
1580N/A GetVersionEx((OSVERSIONINFO *) &ver);
1580N/A return (ver.dwPlatformId == VER_PLATFORM_WIN32_NT) ?
1580N/A (jint)(ver.dwMajorVersion) : (jint)0;
1580N/A}
1580N/A
893N/AJNIEXPORT jlong JNICALL
893N/AJava_sun_nio_ch_Iocp_createIoCompletionPort(JNIEnv* env, jclass this,
893N/A jlong handle, jlong existingPort, jint completionKey, jint concurrency)
893N/A{
3499N/A ULONG_PTR ck = completionKey;
893N/A HANDLE port = CreateIoCompletionPort((HANDLE)jlong_to_ptr(handle),
893N/A (HANDLE)jlong_to_ptr(existingPort),
3499N/A ck,
893N/A (DWORD)concurrency);
893N/A if (port == NULL) {
893N/A JNU_ThrowIOExceptionWithLastError(env, "CreateIoCompletionPort failed");
893N/A }
893N/A return ptr_to_jlong(port);
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_ch_Iocp_close0(JNIEnv* env, jclass this,
893N/A jlong handle)
893N/A{
893N/A HANDLE h = (HANDLE)jlong_to_ptr(handle);
893N/A CloseHandle(h);
893N/A}
893N/A
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_ch_Iocp_getQueuedCompletionStatus(JNIEnv* env, jclass this,
893N/A jlong completionPort, jobject obj)
893N/A{
893N/A DWORD bytesTransferred;
3499N/A ULONG_PTR completionKey;
893N/A OVERLAPPED *lpOverlapped;
893N/A BOOL res;
893N/A
893N/A res = GetQueuedCompletionStatus((HANDLE)jlong_to_ptr(completionPort),
893N/A &bytesTransferred,
893N/A &completionKey,
893N/A &lpOverlapped,
893N/A INFINITE);
893N/A if (res == 0 && lpOverlapped == NULL) {
893N/A JNU_ThrowIOExceptionWithLastError(env, "GetQueuedCompletionStatus failed");
893N/A } else {
893N/A DWORD ioResult = (res == 0) ? GetLastError() : 0;
893N/A (*env)->SetIntField(env, obj, completionStatus_error, ioResult);
893N/A (*env)->SetIntField(env, obj, completionStatus_bytesTransferred,
893N/A (jint)bytesTransferred);
893N/A (*env)->SetIntField(env, obj, completionStatus_completionKey,
893N/A (jint)completionKey);
893N/A (*env)->SetLongField(env, obj, completionStatus_overlapped,
893N/A ptr_to_jlong(lpOverlapped));
893N/A
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT void JNICALL
893N/AJava_sun_nio_ch_Iocp_postQueuedCompletionStatus(JNIEnv* env, jclass this,
893N/A jlong completionPort, jint completionKey)
893N/A{
893N/A BOOL res;
893N/A
893N/A res = PostQueuedCompletionStatus((HANDLE)jlong_to_ptr(completionPort),
893N/A (DWORD)0,
893N/A (DWORD)completionKey,
893N/A NULL);
893N/A if (res == 0) {
893N/A JNU_ThrowIOExceptionWithLastError(env, "PostQueuedCompletionStatus");
893N/A }
893N/A}
893N/A
893N/AJNIEXPORT jstring JNICALL
893N/AJava_sun_nio_ch_Iocp_getErrorMessage(JNIEnv* env, jclass this, jint errorCode)
893N/A{
893N/A WCHAR message[255];
893N/A
893N/A DWORD len = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM,
893N/A NULL,
893N/A (DWORD)errorCode,
893N/A 0,
893N/A &message[0],
893N/A 255,
893N/A NULL);
893N/A
893N/A
893N/A if (len == 0) {
893N/A return NULL;
893N/A } else {
893N/A return (*env)->NewString(env, (const jchar *)message, (jsize)wcslen(message));
893N/A }
893N/A}