mturk       2005/04/15 03:26:19

  Modified:    jni/java/org/apache/tomcat/jni Socket.java
               jni/native/src network.c
  Log:
  Added timeout reads for recv and recvb.
  
  Revision  Changes    Path
  1.6       +48 -1     
jakarta-tomcat-connectors/jni/java/org/apache/tomcat/jni/Socket.java
  
  Index: Socket.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-connectors/jni/java/org/apache/tomcat/jni/Socket.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- Socket.java       14 Apr 2005 16:41:08 -0000      1.5
  +++ Socket.java       15 Apr 2005 10:26:19 -0000      1.6
  @@ -264,6 +264,30 @@
       public static native int recv(long sock, byte[] buf, int offset, int 
nbytes);
   
       /**
  +     * Read data from a network with timeout.
  +     * 
  +     * <PRE>
  +     * This functions acts like a blocking read by default.  To change
  +     * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
  +     * socket option.
  +     * The number of bytes actually received is stored in argument 3.
  +     *
  +     * It is possible for both bytes to be received and an APR_EOF or
  +     * other error to be returned.
  +     *
  +     * APR_EINTR is never returned.
  +     * </PRE>
  +     * @param sock The socket to read the data from.
  +     * @param buf The buffer to store the data in.
  +     * @param offset Offset in the byte buffer.
  +     * @param nbytes The number of bytes to read (-1) for full array.
  +     * @param timeout The socket timeout in microseconds.
  +     * @return the number of bytes received.
  +     */
  +    public static native int recvt(long sock, byte[] buf, int offset,
  +                                   int nbytes, long timeout);
  +
  +    /**
        * Read data from a network.
        * 
        * <PRE>
  @@ -286,6 +310,29 @@
       public static native int recvb(long sock, ByteBuffer buf,
                                      int offset, int nbytes);
   
  +    /**
  +     * Read data from a network with timeout.
  +     * 
  +     * <PRE>
  +     * This functions acts like a blocking read by default.  To change
  +     * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
  +     * socket option.
  +     * The number of bytes actually received is stored in argument 3.
  +     *
  +     * It is possible for both bytes to be received and an APR_EOF or
  +     * other error to be returned.
  +     *
  +     * APR_EINTR is never returned.
  +     * </PRE>
  +     * @param sock The socket to read the data from.
  +     * @param buf The buffer to store the data in.
  +     * @param offset Offset in the byte buffer.
  +     * @param nbytes The number of bytes to read (-1) for full array.
  +     * @param timeout The socket timeout in microseconds.
  +     * @return the number of bytes received.
  +     */
  +    public static native int recvbt(long sock, ByteBuffer buf,
  +                                    int offset, int nbytes, long timeout);
   
       /**
        * @param from The apr_sockaddr_t to fill in the recipient info
  
  
  
  1.6       +64 -0     jakarta-tomcat-connectors/jni/native/src/network.c
  
  Index: network.c
  ===================================================================
  RCS file: /home/cvs/jakarta-tomcat-connectors/jni/native/src/network.c,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- network.c 14 Apr 2005 16:41:24 -0000      1.5
  +++ network.c 15 Apr 2005 10:26:19 -0000      1.6
  @@ -313,6 +313,36 @@
           return -(jint)ss;
   }
   
  +TCN_IMPLEMENT_CALL(jint, Socket, recvt)(TCN_STDARGS, jlong sock,
  +                                        jbyteArray buf, jint offset,
  +                                        jint toread, jlong timeout)
  +{
  +    apr_socket_t *s = J2P(sock, apr_socket_t *);
  +    apr_size_t nbytes = (*e)->GetArrayLength(e, buf);
  +    jbyte *bytes = (*e)->GetByteArrayElements(e, buf, NULL);
  +    apr_status_t ss;
  +    apr_interval_time_t t;
  +
  +    UNREFERENCED(o);
  +
  +    if (toread > 0)
  +        nbytes = min(nbytes - offset, (apr_size_t)toread);
  +    if ((ss = apr_socket_timeout_get(s, &t)) != APR_SUCCESS)
  +        goto cleanup;
  +    if ((ss = apr_socket_timeout_set(s, J2T(timeout))) != APR_SUCCESS)
  +        goto cleanup;
  +    ss = apr_socket_recv(s, bytes + offset, &nbytes);
  +    /* Resore the original timeout */
  +    apr_socket_timeout_set(s, t);
  +cleanup:
  +    (*e)->ReleaseByteArrayElements(e, buf, bytes,
  +                                   nbytes ? 0 : JNI_ABORT);
  +    if (ss == APR_SUCCESS)
  +        return (jint)nbytes;
  +    else
  +        return -(jint)ss;
  +}
  +
   TCN_IMPLEMENT_CALL(jint, Socket, recvb)(TCN_STDARGS, jlong sock,
                                           jobject buf, jint offset, jint len)
   {
  @@ -339,6 +369,40 @@
           return -(jint)ss;
   }
   
  +TCN_IMPLEMENT_CALL(jint, Socket, recvbt)(TCN_STDARGS, jlong sock,
  +                                         jobject buf, jint offset,
  +                                         jint len, jlong timeout)
  +{
  +    apr_socket_t *s = J2P(sock, apr_socket_t *);
  +    apr_status_t ss;
  +    apr_size_t nbytes;
  +    char *bytes;
  +    apr_interval_time_t t;
  +
  +    UNREFERENCED(o);
  +    bytes  = (char *)(*e)->GetDirectBufferAddress(e, buf);
  +    nbytes = (apr_size_t)(*e)->GetDirectBufferCapacity(e, buf);
  +    if (bytes == NULL || nbytes < 0) {
  +        tcn_ThrowAPRException(e, APR_EGENERAL);
  +        return (jint)(-APR_EGENERAL);
  +    }
  +    if (len > 0)
  +        nbytes = min(nbytes - offset, (apr_size_t)len);
  +
  +    if ((ss = apr_socket_timeout_get(s, &t)) != APR_SUCCESS)
  +         return -(jint)ss;
  +    if ((ss = apr_socket_timeout_set(s, J2T(timeout))) != APR_SUCCESS)
  +         return -(jint)ss;
  +    ss = apr_socket_recv(s, bytes + offset, &nbytes);
  +    /* Resore the original timeout */
  +    apr_socket_timeout_set(s, t);
  +
  +    if (ss == APR_SUCCESS)
  +        return (jint)nbytes;
  +    else
  +        return -(jint)ss;
  +}
  +
   TCN_IMPLEMENT_CALL(jint, Socket, recvfrom)(TCN_STDARGS, jlong from,
                                             jlong sock, jint flags,
                                             jbyteArray buf, jint offset, jint 
toread)
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to