jdk/src/windows/native/java/net/SocketInputStream.c
author chegar
Tue, 16 Apr 2013 13:26:30 +0100
changeset 16866 e659009ee08d
parent 5506 202f599c92aa
child 23010 6dadb192ad81
permissions -rw-r--r--
8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows Reviewed-by: alanb, dsamersoff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4508
diff changeset
     2
 * Copyright (c) 1997, 2003, 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: 4508
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: 4508
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: 4508
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4508
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4508
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 <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include <malloc.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
#include <sys/types.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
#include "java_net_SocketInputStream.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include "net_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include "jni_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
/*************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * SocketInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
static jfieldID IO_fd_fdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * Class:     java_net_SocketInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Method:    init
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
Java_java_net_SocketInputStream_init(JNIEnv *env, jclass cls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    IO_fd_fdID = NET_GetFileDescriptorID(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Class:     java_net_SocketInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Method:    socketRead
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * Signature: (Ljava/io/FileDescriptor;[BIII)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
Java_java_net_SocketInputStream_socketRead0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                                            jobject fdObj, jbyteArray data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                                            jint off, jint len, jint timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    char *bufP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    char BUF[MAX_BUFFER_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    jint fd, newfd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    jint nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    if (IS_NULL(fdObj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    if (fd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        NET_ThrowSocketException(env, "Socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * If the caller buffer is large than our stack buffer then we allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * from the heap (up to a limit). If memory is exhausted we always use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * the stack buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    if (len <= MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        bufP = BUF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (len > MAX_HEAP_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            len = MAX_HEAP_BUFFER_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        bufP = (char *)malloc((size_t)len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (bufP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            /* allocation failed so use stack buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            bufP = BUF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            len = MAX_BUFFER_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (timeout <= 5000 || !isRcvTimeoutSupported) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            int ret = NET_Timeout (fd, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            if (ret <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                if (ret == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                                    "Read timed out");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                } else if (ret == JVM_IO_ERR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                    JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "socket closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                } else if (ret == JVM_IO_INTR) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                    JNU_ThrowByName(env, JNU_JAVAIOPKG "InterruptedIOException",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                                    "Operation interrupted");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                if (bufP != BUF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                    free(bufP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            /*check if the socket has been closed while we were in timeout*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            newfd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            if (newfd == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                NET_ThrowSocketException(env, "Socket Closed");
4508
6675f4c35817 6909089: Memory leak occurs by lack of free for read buffer in SocketInputStream#read()
chegar
parents: 2
diff changeset
   124
                if (bufP != BUF) {
6675f4c35817 6909089: Memory leak occurs by lack of free for read buffer in SocketInputStream#read()
chegar
parents: 2
diff changeset
   125
                    free(bufP);
6675f4c35817 6909089: Memory leak occurs by lack of free for read buffer in SocketInputStream#read()
chegar
parents: 2
diff changeset
   126
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    nread = recv(fd, bufP, len, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    if (nread > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        (*env)->SetByteArrayRegion(env, data, off, nread, (jbyte *)bufP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (nread < 0) {
16866
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   137
            // Check if the socket has been closed since we last checked.
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   138
            // This could be a reason for recv failing.
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   139
            if ((*env)->GetIntField(env, fdObj, IO_fd_fdID) == -1) {
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   140
                NET_ThrowSocketException(env, "Socket closed");
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   141
            } else {
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   142
                switch (WSAGetLastError()) {
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   143
                    case WSAEINTR:
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   144
                        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException",
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   145
                            "socket closed");
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   146
                        break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
16866
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   148
                    case WSAECONNRESET:
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   149
                    case WSAESHUTDOWN:
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   150
                        /*
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   151
                         * Connection has been reset - Windows sometimes reports
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   152
                         * the reset as a shutdown error.
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   153
                         */
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   154
                        JNU_ThrowByName(env, "sun/net/ConnectionResetException",
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   155
                            "");
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   156
                        break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
16866
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   158
                    case WSAETIMEDOUT :
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   159
                        JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   160
                                       "Read timed out");
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   161
                        break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
16866
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   163
                    default:
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   164
                        NET_ThrowCurrent(env, "recv failed");
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   165
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    if (bufP != BUF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        free(bufP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    return nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
}