jdk/src/java.base/windows/native/libnet/SocketInputStream.c
author clanger
Thu, 27 Oct 2016 11:00:28 +0200
changeset 41771 18c9669e76ca
parent 41210 73593ed418ff
permissions -rw-r--r--
8167481: cleanup of headers and includes for native libnet Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
41210
73593ed418ff 8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents: 25859
diff changeset
     2
 * Copyright (c) 1997, 2016, 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
 */
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41210
diff changeset
    25
#include <malloc.h>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
41771
18c9669e76ca 8167481: cleanup of headers and includes for native libnet
clanger
parents: 41210
diff changeset
    27
#include "net_util.h"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "java_net_SocketInputStream.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/*************************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * SocketInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
static jfieldID IO_fd_fdID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Class:     java_net_SocketInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * Method:    init
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * Signature: ()V
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
Java_java_net_SocketInputStream_init(JNIEnv *env, jclass cls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    IO_fd_fdID = NET_GetFileDescriptorID(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * Class:     java_net_SocketInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * Method:    socketRead
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Signature: (Ljava/io/FileDescriptor;[BIII)I
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
Java_java_net_SocketInputStream_socketRead0(JNIEnv *env, jobject this,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                                            jobject fdObj, jbyteArray data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                                            jint off, jint len, jint timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
{
41210
73593ed418ff 8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents: 25859
diff changeset
    56
    char BUF[MAX_BUFFER_LEN];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    char *bufP;
41210
73593ed418ff 8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents: 25859
diff changeset
    58
    jint fd, newfd, nread;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    if (IS_NULL(fdObj)) {
41210
73593ed418ff 8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents: 25859
diff changeset
    61
        JNU_ThrowByName(env, "java/net/SocketException",
73593ed418ff 8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents: 25859
diff changeset
    62
                        "Socket closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    if (fd == -1) {
41210
73593ed418ff 8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents: 25859
diff changeset
    67
        JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * If the caller buffer is large than our stack buffer then we allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * from the heap (up to a limit). If memory is exhausted we always use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * the stack buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    if (len <= MAX_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        bufP = BUF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        if (len > MAX_HEAP_BUFFER_LEN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            len = MAX_HEAP_BUFFER_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        bufP = (char *)malloc((size_t)len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (bufP == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            /* allocation failed so use stack buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            bufP = BUF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            len = MAX_BUFFER_LEN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    if (timeout) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (timeout <= 5000 || !isRcvTimeoutSupported) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            int ret = NET_Timeout (fd, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            if (ret <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                if (ret == 0) {
41210
73593ed418ff 8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents: 25859
diff changeset
    97
                    JNU_ThrowByName(env, "java/net/SocketTimeoutException",
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                                    "Read timed out");
23015
73b21ab36615 8034174: Remove use of JVM_* functions from java.net code
chegar
parents: 23010
diff changeset
    99
                } else if (ret == -1) {
41210
73593ed418ff 8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents: 25859
diff changeset
   100
                    JNU_ThrowByName(env, "java/net/SocketException", "socket closed");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                if (bufP != BUF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                    free(bufP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            /*check if the socket has been closed while we were in timeout*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            newfd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            if (newfd == -1) {
41210
73593ed418ff 8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents: 25859
diff changeset
   111
                JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
4508
6675f4c35817 6909089: Memory leak occurs by lack of free for read buffer in SocketInputStream#read()
chegar
parents: 2
diff changeset
   112
                if (bufP != BUF) {
6675f4c35817 6909089: Memory leak occurs by lack of free for read buffer in SocketInputStream#read()
chegar
parents: 2
diff changeset
   113
                    free(bufP);
6675f4c35817 6909089: Memory leak occurs by lack of free for read buffer in SocketInputStream#read()
chegar
parents: 2
diff changeset
   114
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    nread = recv(fd, bufP, len, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    if (nread > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        (*env)->SetByteArrayRegion(env, data, off, nread, (jbyte *)bufP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (nread < 0) {
16866
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   125
            // 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
   126
            // 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
   127
            if ((*env)->GetIntField(env, fdObj, IO_fd_fdID) == -1) {
41210
73593ed418ff 8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents: 25859
diff changeset
   128
                JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
16866
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   129
            } else {
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   130
                switch (WSAGetLastError()) {
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   131
                    case WSAEINTR:
41210
73593ed418ff 8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents: 25859
diff changeset
   132
                        JNU_ThrowByName(env, "java/net/SocketException",
16866
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   133
                            "socket closed");
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   134
                        break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
16866
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   136
                    case WSAECONNRESET:
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   137
                    case WSAESHUTDOWN:
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   138
                        /*
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   139
                         * Connection has been reset - Windows sometimes reports
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   140
                         * the reset as a shutdown error.
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   141
                         */
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   142
                        JNU_ThrowByName(env, "sun/net/ConnectionResetException",
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   143
                            "");
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   144
                        break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
16866
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   146
                    case WSAETIMEDOUT :
41210
73593ed418ff 8166584: Remove obsolete utility function NET_ThrowSocketException in windows libnet
clanger
parents: 25859
diff changeset
   147
                        JNU_ThrowByName(env, "java/net/SocketTimeoutException",
16866
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   148
                                       "Read timed out");
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   149
                        break;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
16866
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   151
                    default:
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   152
                        NET_ThrowCurrent(env, "recv failed");
e659009ee08d 8012244: java/net/Socket/asyncClose/Race.java fails intermittently on Windows
chegar
parents: 5506
diff changeset
   153
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    if (bufP != BUF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        free(bufP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    return nread;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
}