WindowsAsynchronousSocketChannelImpl.c revision 1191
0N/A/*
0N/A * Copyright 2008-2009 Sun Microsystems, Inc. All Rights Reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation. Sun designates this
0N/A * particular file as subject to the "Classpath" exception as provided
0N/A * by Sun in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
0N/A * have any questions.
0N/A */
0N/A
0N/A#include <windows.h>
0N/A#include <winsock2.h>
0N/A#include <stddef.h>
0N/A
0N/A#include "jni.h"
0N/A#include "jni_util.h"
0N/A#include "jlong.h"
0N/A#include "nio.h"
0N/A#include "nio_util.h"
0N/A#include "net_util.h"
0N/A
0N/A#include "sun_nio_ch_WindowsAsynchronousSocketChannelImpl.h"
0N/A
0N/A#ifndef WSAID_CONNECTEX
0N/A#define WSAID_CONNECTEX {0x25a207b9,0xddf3,0x4660,{0x8e,0xe9,0x76,0xe5,0x8c,0x74,0x06,0x3e}}
0N/A#endif
0N/A
0N/A#ifndef SO_UPDATE_CONNECT_CONTEXT
0N/A#define SO_UPDATE_CONNECT_CONTEXT 0x7010
0N/A#endif
0N/A
0N/Atypedef BOOL (*ConnectEx_t)
0N/A(
0N/A SOCKET s,
0N/A const struct sockaddr* name,
0N/A int namelen,
0N/A PVOID lpSendBuffer,
0N/A DWORD dwSendDataLength,
0N/A LPDWORD lpdwBytesSent,
0N/A LPOVERLAPPED lpOverlapped
0N/A);
0N/A
0N/Astatic ConnectEx_t ConnectEx_func;
0N/A
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_nio_ch_WindowsAsynchronousSocketChannelImpl_initIDs(JNIEnv* env, jclass this) {
0N/A GUID GuidConnectEx = WSAID_CONNECTEX;
0N/A SOCKET s;
0N/A int rv;
0N/A DWORD dwBytes;
0N/A
0N/A s = socket(AF_INET, SOCK_STREAM, 0);
0N/A if (s == INVALID_SOCKET) {
0N/A JNU_ThrowIOExceptionWithLastError(env, "socket failed");
0N/A return;
0N/A }
0N/A rv = WSAIoctl(s,
0N/A SIO_GET_EXTENSION_FUNCTION_POINTER,
0N/A (LPVOID)&GuidConnectEx,
0N/A sizeof(GuidConnectEx),
0N/A &ConnectEx_func,
0N/A sizeof(ConnectEx_func),
0N/A &dwBytes,
0N/A NULL,
0N/A NULL);
0N/A if (rv != 0)
0N/A JNU_ThrowIOExceptionWithLastError(env, "WSAIoctl failed");
0N/A closesocket(s);
0N/A}
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJava_sun_nio_ch_WindowsAsynchronousSocketChannelImpl_connect0(JNIEnv* env, jclass this,
0N/A jlong socket, jboolean preferIPv6, jobject iao, jint port, jlong ov)
0N/A{
0N/A SOCKET s = (SOCKET) jlong_to_ptr(socket);
0N/A OVERLAPPED* lpOverlapped = (OVERLAPPED*) jlong_to_ptr(ov);
0N/A
0N/A SOCKETADDRESS sa;
0N/A int sa_len;
0N/A BOOL res;
0N/A
0N/A if (NET_InetAddressToSockaddr(env, iao, port, (struct sockaddr *)&sa, &sa_len, preferIPv6) != 0) {
0N/A return IOS_THROWN;
0N/A }
0N/A
0N/A ZeroMemory((PVOID)lpOverlapped, sizeof(OVERLAPPED));
0N/A
0N/A res = (*ConnectEx_func)(s,
0N/A (struct sockaddr *)&sa,
0N/A sa_len,
0N/A NULL,
0N/A 0,
NULL,
lpOverlapped);
if (res == 0) {
int error = GetLastError();
if (error == ERROR_IO_PENDING) {
return IOS_UNAVAILABLE;
}
JNU_ThrowIOExceptionWithLastError(env, "ConnectEx failed");
return IOS_THROWN;
}
return 0;
}
JNIEXPORT void JNICALL
Java_sun_nio_ch_WindowsAsynchronousSocketChannelImpl_updateConnectContext(JNIEnv* env, jclass this,
jlong socket)
{
SOCKET s = (SOCKET)jlong_to_ptr(socket);
setsockopt(s, SOL_SOCKET, SO_UPDATE_CONNECT_CONTEXT, NULL, 0);
}
JNIEXPORT void JNICALL
Java_sun_nio_ch_WindowsAsynchronousSocketChannelImpl_shutdown0(JNIEnv *env, jclass cl,
jlong socket, jint how)
{
SOCKET s =(SOCKET) jlong_to_ptr(socket);
if (shutdown(s, how) == SOCKET_ERROR) {
JNU_ThrowIOExceptionWithLastError(env, "shutdown failed");
}
}
JNIEXPORT void JNICALL
Java_sun_nio_ch_WindowsAsynchronousSocketChannelImpl_closesocket0(JNIEnv* env, jclass this,
jlong socket)
{
SOCKET s = (SOCKET)jlong_to_ptr(socket);
if (closesocket(s) == SOCKET_ERROR)
JNU_ThrowIOExceptionWithLastError(env, "closesocket failed");
}
JNIEXPORT jint JNICALL
Java_sun_nio_ch_WindowsAsynchronousSocketChannelImpl_read0(JNIEnv* env, jclass this,
jlong socket, jint count, jlong address, jlong ov)
{
SOCKET s = (SOCKET) jlong_to_ptr(socket);
WSABUF* lpWsaBuf = (WSABUF*) jlong_to_ptr(address);
OVERLAPPED* lpOverlapped = (OVERLAPPED*) jlong_to_ptr(ov);
BOOL res;
DWORD flags = 0;
ZeroMemory((PVOID)lpOverlapped, sizeof(OVERLAPPED));
res = WSARecv(s,
lpWsaBuf,
(DWORD)count,
NULL,
&flags,
lpOverlapped,
NULL);
if (res == SOCKET_ERROR) {
int error = WSAGetLastError();
if (error == WSA_IO_PENDING) {
return IOS_UNAVAILABLE;
}
if (error == WSAESHUTDOWN) {
return IOS_EOF; // input shutdown
}
JNU_ThrowIOExceptionWithLastError(env, "WSARecv failed");
return IOS_THROWN;
}
return IOS_UNAVAILABLE;
}
JNIEXPORT jint JNICALL
Java_sun_nio_ch_WindowsAsynchronousSocketChannelImpl_write0(JNIEnv* env, jclass this,
jlong socket, jint count, jlong address, jlong ov)
{
SOCKET s = (SOCKET) jlong_to_ptr(socket);
WSABUF* lpWsaBuf = (WSABUF*) jlong_to_ptr(address);
OVERLAPPED* lpOverlapped = (OVERLAPPED*) jlong_to_ptr(ov);
BOOL res;
ZeroMemory((PVOID)lpOverlapped, sizeof(OVERLAPPED));
res = WSASend(s,
lpWsaBuf,
(DWORD)count,
NULL,
0,
lpOverlapped,
NULL);
if (res == SOCKET_ERROR) {
int error = WSAGetLastError();
if (error == WSA_IO_PENDING) {
return IOS_UNAVAILABLE;
}
if (error == WSAESHUTDOWN) {
return IOS_EOF; // output shutdown
}
JNU_ThrowIOExceptionWithLastError(env, "WSASend failed");
return IOS_THROWN;
}
return IOS_UNAVAILABLE;
}