commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1146022 - in /commons/sandbox/runtime/trunk/src/main/native: include/acr/descriptor.h include/acr/stddefs.h os/unix/localsock.c os/unix/sockstream.c
Date Wed, 13 Jul 2011 13:46:07 GMT
Author: mturk
Date: Wed Jul 13 13:46:06 2011
New Revision: 1146022

URL: http://svn.apache.org/viewvc?rev=1146022&view=rev
Log:
Implement common read methods

Modified:
    commons/sandbox/runtime/trunk/src/main/native/include/acr/descriptor.h
    commons/sandbox/runtime/trunk/src/main/native/include/acr/stddefs.h
    commons/sandbox/runtime/trunk/src/main/native/os/unix/localsock.c
    commons/sandbox/runtime/trunk/src/main/native/os/unix/sockstream.c

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/descriptor.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/descriptor.h?rev=1146022&r1=1146021&r2=1146022&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/descriptor.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/descriptor.h Wed Jul 13 13:46:06
2011
@@ -23,6 +23,7 @@
  * Descriptor flags
  */
 #define ACR_DT_NONBLOCK     0x0001
+#define ACR_DT_HITEOF       0x0002
 
 /**
  * Descriptor types

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/stddefs.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/stddefs.h?rev=1146022&r1=1146021&r2=1146022&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/stddefs.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/stddefs.h Wed Jul 13 13:46:06
2011
@@ -258,6 +258,9 @@
 
 #define ACR_HBUFF_SIZ           8192
 #define ACR_HBUFF_LEN           (ACR_HBUFF_SIZ - 1)
+#define ACR_KILOBYTE            1024
+#define ACR_MEGABYTE            1048576
+
 #define ACR_MTX_MAGIC           0x23036401
 #define ACR_SHM_MAGIC           0x23036402
 

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/localsock.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/localsock.c?rev=1146022&r1=1146021&r2=1146022&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/localsock.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/localsock.c Wed Jul 13 13:46:06
2011
@@ -17,6 +17,7 @@
 #include "acr/error.h"
 #include "acr/iodefs.h"
 #include "acr/clazz.h"
+#include "acr/debug.h"
 #include "acr/string.h"
 #include "acr/memory.h"
 #include "acr/iofd.h"
@@ -392,8 +393,7 @@ ACR_NET_EXPORT(jlong, LocalServerEndpoin
 #endif
 #if defined(DEBUG) || defined(_DEBUG)
     if (aa.sa.unx.sun_family != AF_LOCAL) {
-        fprintf(stderr, "Expected AF_LOCAL but found %d\n", aa.sa.unx.sun_family);
-        fflush(stderr);
+        ACR_DEBUG_TRACE("Expected AF_LOCAL but found %d\n", aa.sa.unx.sun_family);
     }
 #endif
     if ((sp = ACR_TALLOC(acr_fd_t)) == 0) {

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/sockstream.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/sockstream.c?rev=1146022&r1=1146021&r2=1146022&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/sockstream.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/sockstream.c Wed Jul 13 13:46:06
2011
@@ -94,6 +94,10 @@ ACR_NET_EXPORT(jint, SocketStream, read0
     unsigned char ch;
     acr_ss_t *ss = J2P(sp, acr_ss_t *);
 
+    if ((ss->fd->flags & ACR_DT_HITEOF) != 0) {
+        /* Already hit EOF */
+        return -1;
+    }
     if ((sd = _retain_sd(ss)) == -1) {
         rc = ACR_EBADF;
         goto finally;
@@ -108,8 +112,10 @@ ACR_NET_EXPORT(jint, SocketStream, read0
     }
     if (rd == -1)
         rc = ACR_GET_OS_ERROR();
-    else if (rd != 0)
+    else if (rd == 1)
         rv = ch;
+    else
+        ss->fd->flags |= ACR_DT_HITEOF;
 finally:
     _release_sd(ss);
     if (rc != 0) {
@@ -118,3 +124,175 @@ finally:
     }
     return rv;
 }
+
+ACR_NET_EXPORT(jint, SocketStream, read1)(JNI_STDARGS, jlong sp,
+                                          jbyteArray buf,
+                                          jint off,
+                                          jint len)
+{
+    int sd;
+    int rc = 0;
+    ssize_t rd = 0;
+    jbyte  *bb = 0;
+    jbyte  *bc = 0;
+    jbyte   onstack[ACR_PBUFF_SIZ];
+    acr_ss_t *ss = J2P(sp, acr_ss_t *);
+
+    if ((ss->fd->flags & ACR_DT_HITEOF) != 0) {
+        /* Already hit EOF */
+        return -1;
+    }
+    if ((sd = _retain_sd(ss)) == -1) {
+        rc = ACR_EBADF;
+        goto finally;
+    }
+    if (len > ACR_PBUFF_SIZ) {
+        if (len > ACR_MEGABYTE) {
+            if ((bc = (*env)->GetByteArrayElements(env, buf, 0)) != 0)
+                bb = bc + off;
+        }
+        else {
+            /* Allocate buffer */
+            bb = ACR_MALLOC(jbyte, len);
+        }
+    }
+    if (bb == 0) {
+        rc = ACR_ENOMEM;
+        goto finally;
+    }
+    rd = r_read(sd, bb, len);
+    if (rd == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
+        ss->fd->timeout > 0) {
+        rc = AcrWaitIO(sd, AcrTimeAsMsec(ss->fd->timeout), POLLOUT);
+        if (rc != 0)
+            goto finally;
+        rd = r_read(sd, bb, len);
+    }
+    if (rd == -1)
+        rc = ACR_GET_OS_ERROR();
+    else if (rd == 0)
+        ss->fd->flags |= ACR_DT_HITEOF;
+
+finally:
+    _release_sd(ss);
+    if (rc == 0) {
+        if (rd > 0) {
+            if (bc != 0) {
+                (*env)->ReleaseByteArrayElements(env, buf, bc, 0);
+                return (jint)rd;
+            }
+            else
+                (*env)->SetByteArrayRegion(env, buf, (jsize)off, (jsize)rd, bb);
+        }
+    }
+    if (bb != 0 && bb != onstack) {
+        if (bc != 0)
+            (*env)->ReleaseByteArrayElements(env, buf, bc, JNI_ABORT);
+        else
+            AcrFree(bb);
+    }
+    if (rc != 0) {
+        rd = 0;
+        /* Throw exception */
+        ACR_THROW_NET_ERROR(rc);
+    }
+    return (jint)rd;
+}
+
+ACR_NET_EXPORT(jlong, SocketStream, read2)(JNI_STDARGS, jlong sp,
+                                           jlong pa,
+                                           jlong off,
+                                           jlong len)
+{
+    int sd;
+    int rc = 0;
+    ssize_t   rd = 0;
+    size_t    po = (size_t)off;
+    size_t    cs = (size_t)len;
+    char     *bb = J2P(pa, char *);
+    acr_ss_t *ss = J2P(sp, acr_ss_t *);
+
+    if ((ss->fd->flags & ACR_DT_HITEOF) != 0) {
+        /* Already hit EOF */
+        return -1;
+    }
+    if ((sd = _retain_sd(ss)) == -1) {
+        rc = ACR_EBADF;
+        goto finally;
+    }
+    if (bb == 0) {
+        rc = ACR_EINVAL;
+        goto finally;
+    }
+    rd = r_read(sd, bb + po, cs);
+    if (rd == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
+        ss->fd->timeout > 0) {
+        rc = AcrWaitIO(sd, AcrTimeAsMsec(ss->fd->timeout), POLLOUT);
+        if (rc != 0)
+            goto finally;
+        rd = r_read(sd, bb + po, cs);
+    }
+    if (rd == -1)
+        rc = ACR_GET_OS_ERROR();
+    else if (rd == 0)
+        ss->fd->flags |= ACR_DT_HITEOF;
+
+finally:
+    _release_sd(ss);
+    if (rc != 0) {
+        rd = 0;
+        /* Throw exception */
+        ACR_THROW_NET_ERROR(rc);
+        return -1LL;
+    }
+    else
+        return (jlong)rd;
+}
+
+ACR_NET_EXPORT(jint, SocketStream, read3)(JNI_STDARGS, jlong sp,
+                                          jobject dbb,
+                                          jint off,
+                                          jint len)
+{
+    int sd;
+    int rc = 0;
+    ssize_t   rd = 0;
+    char     *bb = 0;
+    acr_ss_t *ss = J2P(sp, acr_ss_t *);
+
+    if ((ss->fd->flags & ACR_DT_HITEOF) != 0) {
+        /* Already hit EOF */
+        return -1;
+    }
+    if ((sd = _retain_sd(ss)) == -1) {
+        rc = ACR_EBADF;
+        goto finally;
+    }
+    bb = (char *)(*env)->GetDirectBufferAddress(env, dbb);
+    if (bb == 0) {
+        rc = ACR_EINVAL;
+        goto finally;
+    }
+
+    rd = r_read(sd, bb + off, len);
+    if (rd == -1 && (errno == EAGAIN || errno == EWOULDBLOCK) &&
+        ss->fd->timeout > 0) {
+        rc = AcrWaitIO(sd, AcrTimeAsMsec(ss->fd->timeout), POLLOUT);
+        if (rc != 0)
+            goto finally;
+        rd = r_read(sd, bb + off, len);
+    }
+    if (rd == -1)
+        rc = ACR_GET_OS_ERROR();
+    else if (rd == 0)
+        ss->fd->flags |= ACR_DT_HITEOF;
+
+finally:
+    _release_sd(ss);
+    if (rc != 0) {
+        rd = 0;
+        /* Throw exception */
+        ACR_THROW_NET_ERROR(rc);
+    }
+    return (jint)rd;
+}



Mime
View raw message