1 /*
   2  * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <windows.h>
  27 
  28 #include "jni.h"
  29 #include "jni_util.h"
  30 #include "jlong.h"
  31 #include "nio.h"
  32 #include "nio_util.h"
  33 
  34 #include "sun_nio_ch_Iocp.h"
  35 
  36 
  37 static jfieldID completionStatus_error;
  38 static jfieldID completionStatus_bytesTransferred;
  39 static jfieldID completionStatus_completionKey;
  40 static jfieldID completionStatus_overlapped;
  41 
  42 
  43 JNIEXPORT void JNICALL
  44 Java_sun_nio_ch_Iocp_initIDs(JNIEnv* env, jclass this)
  45 {
  46     jclass clazz;
  47 
  48     clazz = (*env)->FindClass(env, "sun/nio/ch/Iocp$CompletionStatus");
  49     if (clazz == NULL) {
  50         return;
  51     }
  52     completionStatus_error = (*env)->GetFieldID(env, clazz, "error", "I");
  53     if (completionStatus_error == NULL) return;
  54     completionStatus_bytesTransferred = (*env)->GetFieldID(env, clazz, "bytesTransferred", "I");
  55     if (completionStatus_bytesTransferred == NULL) return;
  56     completionStatus_completionKey = (*env)->GetFieldID(env, clazz, "completionKey", "I");
  57     if (completionStatus_completionKey == NULL) return;
  58     completionStatus_overlapped = (*env)->GetFieldID(env, clazz, "overlapped", "J");
  59 }
  60 
  61 JNIEXPORT jint JNICALL
  62 Java_sun_nio_ch_Iocp_osMajorVersion(JNIEnv* env, jclass this)
  63 {
  64     OSVERSIONINFOEX ver;
  65     ver.dwOSVersionInfoSize = sizeof(ver);
  66     GetVersionEx((OSVERSIONINFO *) &ver);
  67     return (ver.dwPlatformId == VER_PLATFORM_WIN32_NT) ?
  68         (jint)(ver.dwMajorVersion) : (jint)0;
  69 }
  70 
  71 JNIEXPORT jlong JNICALL
  72 Java_sun_nio_ch_Iocp_createIoCompletionPort(JNIEnv* env, jclass this,
  73     jlong handle, jlong existingPort, jint completionKey, jint concurrency)
  74 {
  75     ULONG_PTR ck = completionKey;
  76     HANDLE port = CreateIoCompletionPort((HANDLE)jlong_to_ptr(handle),
  77                                          (HANDLE)jlong_to_ptr(existingPort),
  78                                          ck,
  79                                          (DWORD)concurrency);
  80     if (port == NULL) {
  81         JNU_ThrowIOExceptionWithLastError(env, "CreateIoCompletionPort failed");
  82     }
  83     return ptr_to_jlong(port);
  84 }
  85 
  86 JNIEXPORT void JNICALL
  87 Java_sun_nio_ch_Iocp_close0(JNIEnv* env, jclass this,
  88     jlong handle)
  89 {
  90     HANDLE h = (HANDLE)jlong_to_ptr(handle);
  91     CloseHandle(h);
  92 }
  93 
  94 
  95 JNIEXPORT void JNICALL
  96 Java_sun_nio_ch_Iocp_getQueuedCompletionStatus(JNIEnv* env, jclass this,
  97     jlong completionPort, jobject obj)
  98 {
  99     DWORD bytesTransferred;
 100     ULONG_PTR completionKey;
 101     OVERLAPPED *lpOverlapped;
 102     BOOL res;
 103 
 104     res = GetQueuedCompletionStatus((HANDLE)jlong_to_ptr(completionPort),
 105                                   &bytesTransferred,
 106                                   &completionKey,
 107                                   &lpOverlapped,
 108                                   INFINITE);
 109     if (res == 0 && lpOverlapped == NULL) {
 110         JNU_ThrowIOExceptionWithLastError(env, "GetQueuedCompletionStatus failed");
 111     } else {
 112         DWORD ioResult = (res == 0) ? GetLastError() : 0;
 113         (*env)->SetIntField(env, obj, completionStatus_error, ioResult);
 114         (*env)->SetIntField(env, obj, completionStatus_bytesTransferred,
 115             (jint)bytesTransferred);
 116         (*env)->SetIntField(env, obj, completionStatus_completionKey,
 117             (jint)completionKey);
 118         (*env)->SetLongField(env, obj, completionStatus_overlapped,
 119             ptr_to_jlong(lpOverlapped));
 120 
 121     }
 122 }
 123 
 124 JNIEXPORT void JNICALL
 125 Java_sun_nio_ch_Iocp_postQueuedCompletionStatus(JNIEnv* env, jclass this,
 126     jlong completionPort, jint completionKey)
 127 {
 128     BOOL res;
 129 
 130     res = PostQueuedCompletionStatus((HANDLE)jlong_to_ptr(completionPort),
 131                                      (DWORD)0,
 132                                      (DWORD)completionKey,
 133                                      NULL);
 134     if (res == 0) {
 135         JNU_ThrowIOExceptionWithLastError(env, "PostQueuedCompletionStatus");
 136     }
 137 }
 138 
 139 JNIEXPORT jstring JNICALL
 140 Java_sun_nio_ch_Iocp_getErrorMessage(JNIEnv* env, jclass this, jint errorCode)
 141 {
 142     WCHAR message[255];
 143 
 144     DWORD len = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM,
 145                                NULL,
 146                                (DWORD)errorCode,
 147                                0,
 148                                &message[0],
 149                                255,
 150                                NULL);
 151 
 152 
 153     if (len == 0) {
 154         return NULL;
 155     } else {
 156         return (*env)->NewString(env, (const jchar *)message, (jsize)wcslen(message));
 157     }
 158 }