commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1156708 - in /commons/sandbox/runtime/trunk/src/main/native/os: linux/sendfile.c solaris/sendfile.c
Date Thu, 11 Aug 2011 17:59:38 GMT
Author: mturk
Date: Thu Aug 11 17:59:37 2011
New Revision: 1156708

URL: http://svn.apache.org/viewvc?rev=1156708&view=rev
Log:
Try sending as much as we can in a single call

Modified:
    commons/sandbox/runtime/trunk/src/main/native/os/linux/sendfile.c
    commons/sandbox/runtime/trunk/src/main/native/os/solaris/sendfile.c

Modified: commons/sandbox/runtime/trunk/src/main/native/os/linux/sendfile.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/linux/sendfile.c?rev=1156708&r1=1156707&r2=1156708&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/linux/sendfile.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/linux/sendfile.c Thu Aug 11 17:59:37
2011
@@ -38,23 +38,21 @@
 ACR_NET_EXPORT(jint, Sendfile, send0)(JNI_STDARGS, jlong sp, jlong fp)
 {
     int     rc = 0;
-    ssize_t wr = 0;
-    size_t cnt = ZERO_FILE_LINE_CHUNK;
     acr_sd_t *sd = J2P(sp, acr_sd_t *);
     acr_sf_t *sf = J2P(fp, acr_sf_t *);
     int     sock;
-    off_t   rdoff;
+    int     xmitted = 0;
+    size_t  tosend  = ZERO_FILE_LINE_CHUNK;
 
     if ((sock = AcrSdRetain(sd)) == -1) {
         ACR_THROW_NET_ERROR(ACR_EBADF);
         return -1;
     }
-    rdoff = sf->off;
     if (sf->size != 0) {
         if ((sf->size - sf->off) > INT_MAX)
-            cnt = INT_MAX;
+            tosend = INT_MAX;
         else
-            cnt = (size_t)(sf->size - sf->off);
+            tosend = (size_t)(sf->size - sf->off);
     }
     if (ACR_HASFLAG(sd, ACR_SO_WPART)) {
         /* Flush any previous writes */
@@ -63,37 +61,41 @@ ACR_NET_EXPORT(jint, Sendfile, send0)(JN
         if (rc != 0)
             goto finally;
     }
-    if (cnt == 0) {
+    if (tosend == 0) {
         AcrSdRelease(sd);
         return -1;
     }
-    do {
-        wr = sendfile(sock,     /* socket */
-                      sf->fd,   /* file descriptor of the file to be sent */
-                      &sf->off, /* where in the file to start */
-                      cnt);     /* number of bytes to send */
-    } while (wr == -1 && errno == EINTR);
+    while (tosend > 0) {
+        ssize_t wr;
+        off_t   offset = sf->off;
+        do {
+            wr = sendfile(sock,     /* socket */
+                          sf->fd,   /* file descriptor of the file to be sent */
+                          &sf->off, /* where in the file to start */
+                          tosend);  /* number of bytes to send */
+        } while (wr == -1 && errno == EINTR);
 
-    if (wr == -1) {
-        rc = errno;
-        if ((rc == EAGAIN || rc == EWOULDBLOCK) && sd->timeout > 0) {
-            if ((rc = AcrWaitIO(sock, sd->timeout, POLLOUT)) == 0) {
-                /* Send someting */
-                wr = (ssize_t)(sf->off - rdoff);
+        if (wr == -1) {
+            rc = errno;
+            if ((rc == EAGAIN || rc == EWOULDBLOCK) && sd->timeout > 0) {
+                if ((rc = AcrWaitIO(sock, sd->timeout, POLLOUT)) == 0) {
+                    /* We have send someting.
+                    * See how many bytes was actually send.
+                    */
+                    wr = (ssize_t)(sf->off - offset);
+                }
             }
         }
+        if (rc != 0)
+            break;
+        tosend  -= wr;
+        xmitted += wr;
     }
 finally:
-    if (rc != 0) {
-        wr = -1;
+    if (rc != 0 && xmitted == 0) {
+        xmitted = -1;
         ACR_THROW_NET_ERROR(rc);
     }
-    else if (sd->timeout > 0 && wr < (ssize_t)(sf->off - rdoff)) {
-        /* Send more then written.
-         * The next send call will have to wait
-         */
-        ACR_SETFLAG(sd, ACR_SO_WPART);
-    }
     AcrSdRelease(sd);
-    return (jint)wr;
+    return xmitted;
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/os/solaris/sendfile.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/solaris/sendfile.c?rev=1156708&r1=1156707&r2=1156708&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/solaris/sendfile.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/solaris/sendfile.c Thu Aug 11 17:59:37
2011
@@ -38,23 +38,21 @@
 ACR_NET_EXPORT(jint, Sendfile, send0)(JNI_STDARGS, jlong sp, jlong fp)
 {
     int     rc = 0;
-    ssize_t wr = 0;
-    size_t cnt = ZERO_FILE_LINE_CHUNK;
     acr_sd_t *sd = J2P(sp, acr_sd_t *);
     acr_sf_t *sf = J2P(fp, acr_sf_t *);
     int     sock;
-    off_t   rdoff;
+    int     xmitted = 0;
+    size_t  tosend  = ZERO_FILE_LINE_CHUNK;
 
     if ((sock = AcrSdRetain(sd)) == -1) {
         ACR_THROW_NET_ERROR(ACR_EBADF);
         return -1;
     }
-    rdoff = sf->off;
     if (sf->size != 0) {
         if ((sf->size - sf->off) > INT_MAX)
-            cnt = INT_MAX;
+            tosend = INT_MAX;
         else
-            cnt = (size_t)(sf->size - sf->off);
+            tosend = (size_t)(sf->size - sf->off);
     }
     if (ACR_HASFLAG(sd, ACR_SO_WPART)) {
         /* Flush any previous writes */
@@ -63,39 +61,41 @@ ACR_NET_EXPORT(jint, Sendfile, send0)(JN
         if (rc != 0)
             goto finally;
     }
-    if (cnt == 0) {
+    if (tosend == 0) {
         AcrSdRelease(sd);
         return -1;
     }
-    do {
-        wr = sendfile(sock,     /* socket */
-                      sf->fd,   /* file descriptor of the file to be sent */
-                      &sf->off, /* where in the file to start */
-                      cnt);     /* number of bytes to send */
-    } while (wr == -1 && errno == EINTR);
+    while (tosend > 0) {
+        ssize_t wr;
+        off_t   offset = sf->off;
+        do {
+            wr = sendfile(sock,     /* socket */
+                          sf->fd,   /* file descriptor of the file to be sent */
+                          &sf->off, /* where in the file to start */
+                          tosend);  /* number of bytes to send */
+        } while (wr == -1 && errno == EINTR);
 
-    if (wr == -1) {
-        rc = errno;
-        if ((rc == EAGAIN || rc == EWOULDBLOCK) && sd->timeout > 0) {
-            if ((rc = AcrWaitIO(sock, sd->timeout, POLLOUT)) == 0) {
-                /* We have send someting.
-                 * See how many bytes was actually send.
-                 */
-                wr = (ssize_t)(sf->off - rdoff);
+        if (wr == -1) {
+            rc = errno;
+            if ((rc == EAGAIN || rc == EWOULDBLOCK) && sd->timeout > 0) {
+                if ((rc = AcrWaitIO(sock, sd->timeout, POLLOUT)) == 0) {
+                    /* We have send someting.
+                    * See how many bytes was actually send.
+                    */
+                    wr = (ssize_t)(sf->off - offset);
+                }
             }
         }
+        if (rc != 0)
+            break;
+        tosend  -= wr;
+        xmitted += wr;
     }
 finally:
-    if (rc != 0) {
-        wr = -1;
+    if (rc != 0 && xmitted == 0) {
+        xmitted = -1;
         ACR_THROW_NET_ERROR(rc);
     }
-    else if (sd->timeout > 0 && wr < (ssize_t)(sf->off - rdoff)) {
-        /* Send more then written.
-         * The next send call will have to wait
-         */
-        ACR_SETFLAG(sd, ACR_SO_WPART);
-    }
     AcrSdRelease(sd);
-    return (jint)wr;
+    return xmitted;
 }



Mime
View raw message