src/java.base/windows/native/libnio/ch/SocketDispatcher.c
author alanb
Fri, 22 Mar 2019 13:21:29 +0000
branchniosocketimpl-branch
changeset 57278 bf925a3ee68a
parent 57274 07b6be5d9150
parent 54246 f04e3492fd88
child 57358 f0a1d9760c5e
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 48767
diff changeset
     2
 * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
#include <windows.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#include <winsock2.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
#include <ctype.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include "jvm.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include "jlong.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include "sun_nio_ch_SocketDispatcher.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "nio.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include "nio_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * SocketDispatcher.c
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
Java_sun_nio_ch_SocketDispatcher_read0(JNIEnv *env, jclass clazz, jobject fdo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                                      jlong address, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /* set up */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    DWORD read = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    DWORD flags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    WSABUF buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
5804
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
    53
    /* limit size */
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
    54
    if (len > MAX_BUFFER_SIZE)
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
    55
        len = MAX_BUFFER_SIZE;
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
    56
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /* destination buffer and size */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    buf.buf = (char *)address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    buf.len = (u_long)len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /* read into the buffers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    i = WSARecv((SOCKET)fd, /* Socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            &buf,           /* pointers to the buffers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            (DWORD)1,       /* number of buffers to process */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            &read,          /* receives number of bytes read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            &flags,         /* no flags */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            0,              /* no overlapped sockets */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            0);             /* no completion routine */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    if (i == SOCKET_ERROR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        int theErr = (jint)WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        if (theErr == WSAEWOULDBLOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 48767
diff changeset
    75
        if (theErr == WSAECONNRESET) {
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 48767
diff changeset
    76
            JNU_ThrowByName(env, "sun/net/ConnectionResetException", "Connection reset");
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 48767
diff changeset
    77
        } else {
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 48767
diff changeset
    78
            JNU_ThrowIOExceptionWithLastError(env, "Read failed");
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 48767
diff changeset
    79
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    return convertReturnVal(env, (jint)read, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
Java_sun_nio_ch_SocketDispatcher_readv0(JNIEnv *env, jclass clazz, jobject fdo,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                                       jlong address, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /* set up */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    DWORD read = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    DWORD flags = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    struct iovec *iovp = (struct iovec *)address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    WSABUF *bufs = malloc(len * sizeof(WSABUF));
5804
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
    97
    jint rem = MAX_BUFFER_SIZE;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    if (bufs == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /* copy iovec into WSABUF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    for(i=0; i<len; i++) {
5804
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   106
        jint iov_len = iovp[i].iov_len;
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   107
        if (iov_len > rem)
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   108
            iov_len = rem;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        bufs[i].buf = (char *)iovp[i].iov_base;
5804
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   110
        bufs[i].len = (u_long)iov_len;
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   111
        rem -= iov_len;
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   112
        if (rem == 0) {
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   113
            len = i+1;
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   114
            break;
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   115
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /* read into the buffers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    i = WSARecv((SOCKET)fd, /* Socket */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            bufs,           /* pointers to the buffers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            (DWORD)len,     /* number of buffers to process */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            &read,          /* receives number of bytes read */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            &flags,         /* no flags */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            0,              /* no overlapped sockets */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            0);             /* no completion routine */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /* clean up */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    free(bufs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    if (i != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        int theErr = (jint)WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (theErr == WSAEWOULDBLOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 48767
diff changeset
   135
        if (theErr == WSAECONNRESET) {
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 48767
diff changeset
   136
            JNU_ThrowByName(env, "sun/net/ConnectionResetException", "Connection reset");
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 48767
diff changeset
   137
        } else {
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 48767
diff changeset
   138
            JNU_ThrowIOExceptionWithLastError(env, "Vector read failed");
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 48767
diff changeset
   139
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    return convertLongReturnVal(env, (jlong)read, JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
Java_sun_nio_ch_SocketDispatcher_write0(JNIEnv *env, jclass clazz, jobject fdo,
13017
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   148
                                       jlong address, jint total)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /* set up */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    DWORD written = 0;
13017
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   153
    jint count = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    WSABUF buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
13017
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   157
    do {
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   158
        /* limit size */
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   159
        jint len = total - count;
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   160
        if (len > MAX_BUFFER_SIZE)
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   161
            len = MAX_BUFFER_SIZE;
5804
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   162
13017
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   163
        /* copy iovec into WSABUF */
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   164
        buf.buf = (char *)address;
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   165
        buf.len = (u_long)len;
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   166
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   167
        /* write from the buffer */
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   168
        i = WSASend((SOCKET)fd,     /* Socket */
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   169
                    &buf,           /* pointers to the buffers */
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   170
                    (DWORD)1,       /* number of buffers to process */
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   171
                    &written,       /* receives number of bytes written */
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   172
                    0,              /* no flags */
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   173
                    0,              /* no overlapped sockets */
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   174
                    0);             /* no completion routine */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
13017
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   176
        if (i == SOCKET_ERROR) {
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   177
            if (count > 0) {
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   178
                /* can't throw exception when some bytes have been written */
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   179
                break;
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   180
            } else {
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   181
               int theErr = (jint)WSAGetLastError();
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   182
               if (theErr == WSAEWOULDBLOCK) {
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   183
                   return IOS_UNAVAILABLE;
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   184
               }
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 48767
diff changeset
   185
               if (theErr == WSAECONNRESET) {
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 48767
diff changeset
   186
                   JNU_ThrowIOException(env, "Connection reset by peer");
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 48767
diff changeset
   187
               } else {
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 48767
diff changeset
   188
                   JNU_ThrowIOExceptionWithLastError(env, "Write failed");
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 48767
diff changeset
   189
               }
13017
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   190
               return IOS_THROWN;
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   191
            }
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   192
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
48255
971d83666b23 8170495: JNI primitive type mismatch in SocketDispatcher.c:187
bpb
parents: 47216
diff changeset
   194
        count += (jint)written;
13017
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   195
        address += written;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
13017
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   197
    } while ((count < total) && (written == MAX_BUFFER_SIZE));
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   198
2e79ad4fea09 7176630: (sc) SocketChannel.write does not write more than 128k when channel configured blocking [win]
alanb
parents: 11909
diff changeset
   199
    return count;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
JNIEXPORT jlong JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
Java_sun_nio_ch_SocketDispatcher_writev0(JNIEnv *env, jclass clazz,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                         jobject fdo, jlong address, jint len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    /* set up */
21324
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   207
    int next_index, next_offset, ret=0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    DWORD written = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    struct iovec *iovp = (struct iovec *)address;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    WSABUF *bufs = malloc(len * sizeof(WSABUF));
21324
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   212
    jlong count = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    if (bufs == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        JNU_ThrowOutOfMemoryError(env, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
21324
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   219
    // next buffer and offset to consume
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   220
    next_index = 0;
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   221
    next_offset = 0;
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   222
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   223
    while (next_index  < len) {
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   224
        DWORD buf_count = 0;
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   225
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   226
        /* Prepare the WSABUF array to a maximum total size of MAX_BUFFER_SIZE */
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   227
        jint rem = MAX_BUFFER_SIZE;
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   228
        while (next_index < len && rem > 0) {
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   229
            jint iov_len = iovp[next_index].iov_len - next_offset;
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   230
            char* ptr = (char *)iovp[next_index].iov_base;
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   231
            ptr += next_offset;
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   232
            if (iov_len > rem) {
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   233
                iov_len = rem;
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   234
                next_offset += rem;
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   235
            } else {
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   236
                next_index ++;
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   237
                next_offset = 0;
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   238
            }
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   239
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   240
            bufs[buf_count].buf = ptr;
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   241
            bufs[buf_count].len = (u_long)iov_len;
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   242
            buf_count++;
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   243
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   244
            rem -= iov_len;
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   245
        }
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   246
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   247
        /* write the buffers */
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   248
        ret = WSASend((SOCKET)fd,           /* Socket */
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   249
                              bufs,         /* pointers to the buffers */
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   250
                              buf_count,    /* number of buffers to process */
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   251
                              &written,     /* receives number of bytes written */
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   252
                              0,            /* no flags */
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   253
                              0,            /* no overlapped sockets */
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   254
                              0);           /* no completion routine */
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   255
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   256
        if (ret == SOCKET_ERROR) {
5804
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   257
            break;
faf9bd47d6ce 6395224: (so) SocketChannel writer blocked on large buffer is not preempted by close method (vista)
alanb
parents: 5506
diff changeset
   258
        }
21324
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   259
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   260
        count += written;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /* clean up */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    free(bufs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
21324
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   266
    if (ret == SOCKET_ERROR && count == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        int theErr = (jint)WSAGetLastError();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if (theErr == WSAEWOULDBLOCK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            return IOS_UNAVAILABLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
54246
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 48767
diff changeset
   271
        if (theErr == WSAECONNRESET) {
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 48767
diff changeset
   272
            JNU_ThrowIOException(env, "Connection reset by peer");
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 48767
diff changeset
   273
        } else {
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 48767
diff changeset
   274
            JNU_ThrowIOExceptionWithLastError(env, "Vector write failed");
f04e3492fd88 8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents: 48767
diff changeset
   275
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        return IOS_THROWN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
21324
1f9eb351241e 7074436: (sc) SocketChannel can do short gathering writes when channel configured blocking (win)
alanb
parents: 14342
diff changeset
   279
    return convertLongReturnVal(env, count, JNI_FALSE);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
JNIEXPORT void JNICALL
11909
a1a7165ac1fa 6346658: (se) Selector briefly spins when asynchronously closing a registered channel [win]
alanb
parents: 7668
diff changeset
   283
Java_sun_nio_ch_SocketDispatcher_preClose0(JNIEnv *env, jclass clazz,
a1a7165ac1fa 6346658: (se) Selector briefly spins when asynchronously closing a registered channel [win]
alanb
parents: 7668
diff changeset
   284
                                           jobject fdo)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    jint fd = fdval(env, fdo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    struct linger l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    int len = sizeof(l);
11909
a1a7165ac1fa 6346658: (se) Selector briefly spins when asynchronously closing a registered channel [win]
alanb
parents: 7668
diff changeset
   289
    if (getsockopt(fd, SOL_SOCKET, SO_LINGER, (char *)&l, &len) == 0) {
a1a7165ac1fa 6346658: (se) Selector briefly spins when asynchronously closing a registered channel [win]
alanb
parents: 7668
diff changeset
   290
        if (l.l_onoff == 0) {
48767
0c6ce8fdb50a 8080990: libdt_socket/socket_md.c(202) : warning C4996: 'gethostbyname': Use getaddrinfo() or GetAddrInfoW()
gadams
parents: 48255
diff changeset
   291
            shutdown(fd, SD_SEND);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
}
11909
a1a7165ac1fa 6346658: (se) Selector briefly spins when asynchronously closing a registered channel [win]
alanb
parents: 7668
diff changeset
   295
a1a7165ac1fa 6346658: (se) Selector briefly spins when asynchronously closing a registered channel [win]
alanb
parents: 7668
diff changeset
   296
JNIEXPORT void JNICALL
a1a7165ac1fa 6346658: (se) Selector briefly spins when asynchronously closing a registered channel [win]
alanb
parents: 7668
diff changeset
   297
Java_sun_nio_ch_SocketDispatcher_close0(JNIEnv *env, jclass clazz,
a1a7165ac1fa 6346658: (se) Selector briefly spins when asynchronously closing a registered channel [win]
alanb
parents: 7668
diff changeset
   298
                                         jobject fdo)
a1a7165ac1fa 6346658: (se) Selector briefly spins when asynchronously closing a registered channel [win]
alanb
parents: 7668
diff changeset
   299
{
a1a7165ac1fa 6346658: (se) Selector briefly spins when asynchronously closing a registered channel [win]
alanb
parents: 7668
diff changeset
   300
    jint fd = fdval(env, fdo);
a1a7165ac1fa 6346658: (se) Selector briefly spins when asynchronously closing a registered channel [win]
alanb
parents: 7668
diff changeset
   301
    if (closesocket(fd) == SOCKET_ERROR) {
a1a7165ac1fa 6346658: (se) Selector briefly spins when asynchronously closing a registered channel [win]
alanb
parents: 7668
diff changeset
   302
        JNU_ThrowIOExceptionWithLastError(env, "Socket close failed");
a1a7165ac1fa 6346658: (se) Selector briefly spins when asynchronously closing a registered channel [win]
alanb
parents: 7668
diff changeset
   303
    }
a1a7165ac1fa 6346658: (se) Selector briefly spins when asynchronously closing a registered channel [win]
alanb
parents: 7668
diff changeset
   304
}