1 /*
   2  * Copyright (c) 1997, 2018, 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 #include <errno.h>
  26 #include <stdlib.h>
  27 #include <string.h>
  28 
  29 #include "net_util.h"
  30 #include "rdma_util_md.h"
  31 
  32 #include "rdma_ch_RdmaSocketOutputStream.h"
  33 
  34 #define min(a, b)       ((a) < (b) ? (a) : (b))
  35 
  36 static jfieldID IO_fd_fdID;
  37 
  38 /*
  39  * Class:     jdk_net_SocketOutputStream
  40  * Method:    init
  41  * Signature: ()V
  42  */
  43 JNIEXPORT void JNICALL
  44 Java_rdma_ch_RdmaSocketOutputStream_init(JNIEnv *env, jclass cls) {
  45     IO_fd_fdID = NET_GetFileDescriptorID(env);
  46 }
  47 
  48 /*
  49  * Class:     jdk_net_RdmaSocketOutputStream
  50  * Method:    rdmaSocketWrite0
  51  * Signature: (Ljava/io/FileDescriptor;[BII)V
  52  */
  53 JNIEXPORT void JNICALL
  54 Java_rdma_ch_RdmaSocketOutputStream_rdmaSocketWrite0(JNIEnv *env, jobject this,
  55                                               jobject fdObj,
  56                                               jbyteArray data,
  57                                               jint off, jint len) {
  58     char *bufP;
  59     char BUF[MAX_BUFFER_LEN];
  60     int buflen;
  61     int fd;
  62 
  63     if (IS_NULL(fdObj)) {
  64         JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
  65         return;
  66     } else {
  67         fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
  68         if (fd == -1) {
  69             JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
  70             return;
  71         }
  72 
  73     }
  74 
  75     if (len <= MAX_BUFFER_LEN) {
  76         bufP = BUF;
  77         buflen = MAX_BUFFER_LEN;
  78     } else {
  79         buflen = min(MAX_HEAP_BUFFER_LEN, len);
  80         bufP = (char *)malloc((size_t)buflen);
  81 
  82         if (bufP == NULL) {
  83             bufP = BUF;
  84             buflen = MAX_BUFFER_LEN;
  85         }
  86     }
  87 
  88     while(len > 0) {
  89         int loff = 0;
  90         int chunkLen = min(buflen, len);
  91         int llen = chunkLen;
  92         (*env)->GetByteArrayRegion(env, data, off, chunkLen, (jbyte *)bufP);
  93 
  94         if ((*env)->ExceptionCheck(env)) {
  95             break;
  96         } else {
  97             while(llen > 0) {
  98                 int n = RDMA_Send(fd, bufP + loff, llen, 0);
  99                 if (n > 0) {
 100                     llen -= n;
 101                     loff += n;
 102                     continue;
 103                 }
 104                 if (errno == ECONNRESET) {
 105                     JNU_ThrowByName(env, "sun/net/ConnectionResetException",
 106                         "Connection reset");
 107                 } else {
 108                     JNU_ThrowByNameWithMessageAndLastError
 109                         (env, "java/net/SocketException", "Write failed");
 110                 }
 111                 if (bufP != BUF) {
 112                     free(bufP);
 113                 }
 114                 return;
 115             }
 116             len -= chunkLen;
 117             off += chunkLen;
 118         }
 119     }
 120 
 121     if (bufP != BUF) {
 122         free(bufP);
 123     }
 124 }