harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From py...@apache.org
Subject svn commit: r502166 - in /harmony/enhanced/classlib/trunk/modules: luni/src/main/java/org/apache/harmony/luni/platform/ luni/src/main/native/luni/shared/ nio/src/main/java/org/apache/harmony/nio/internal/
Date Thu, 01 Feb 2007 09:26:27 GMT
Author: pyang
Date: Thu Feb  1 01:26:26 2007
New Revision: 502166

URL: http://svn.apache.org/viewvc?view=rev&rev=502166
Log:
Apply second patch for HARMONY-3037( [classlib][nio]DatagramChannelImpl and SocketChannelImpl
lack support for direct byte buffer)

Modified:
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/INetworkSystem.java
    harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/OSNetworkSystem.java
    harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/OSNetworkSystem.c
    harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/OSNetworkSystem.h
    harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/DatagramChannelImpl.java

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/INetworkSystem.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/INetworkSystem.java?view=diff&rev=502166&r1=502165&r2=502166
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/INetworkSystem.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/INetworkSystem.java
Thu Feb  1 01:26:26 2007
@@ -99,16 +99,17 @@
 			byte[] data, int offset, int length, int receiveTimeout,
 			boolean peek) throws IOException;
     
-    public int receiveDatagramDirect(FileDescriptor aFD, long address, int offset,
-            int length, int receiveTimeout, boolean peek) throws IOException;
+    public int receiveDatagramDirect(FileDescriptor aFD, DatagramPacket packet,
+            long address, int offset, int length, int receiveTimeout,
+            boolean peek) throws IOException;
 
 	public int recvConnectedDatagram(FileDescriptor aFD, DatagramPacket packet,
 			byte[] data, int offset, int length, int receiveTimeout,
 			boolean peek) throws IOException;
     
-    public int recvConnectedDatagramDirect(FileDescriptor aFD, long address,
-            int offset, int length, int receiveTimeout, boolean peek)
-            throws IOException;
+    public int recvConnectedDatagramDirect(FileDescriptor aFD,
+            DatagramPacket packet, long address, int offset, int length,
+            int receiveTimeout, boolean peek) throws IOException;
     
 	public int peekDatagram(FileDescriptor aFD, InetAddress sender,
 			int receiveTimeout) throws IOException;

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/OSNetworkSystem.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/OSNetworkSystem.java?view=diff&rev=502166&r1=502165&r2=502166
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/OSNetworkSystem.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/java/org/apache/harmony/luni/platform/OSNetworkSystem.java
Thu Feb  1 01:26:26 2007
@@ -175,10 +175,10 @@
                 receiveTimeout, peek);
     }
     
-    public int receiveDatagramDirect(FileDescriptor aFD, long address,
-            int offset, int length, int receiveTimeout, boolean peek)
-            throws IOException {
-        return receiveDatagramDirectImpl(aFD, address, offset, length,
+    public int receiveDatagramDirect(FileDescriptor aFD, DatagramPacket packet,
+            long address, int offset, int length, int receiveTimeout,
+            boolean peek) throws IOException {
+        return receiveDatagramDirectImpl(aFD, packet, address, offset, length,
                 receiveTimeout, peek);
     }
 
@@ -189,11 +189,10 @@
 				receiveTimeout, peek);
 	}
     
-    public int recvConnectedDatagramDirect(FileDescriptor aFD, long address,
-            int offset, int length, int receiveTimeout, boolean peek)
+    public int recvConnectedDatagramDirect(FileDescriptor aFD, DatagramPacket packet, long
address,
+             int offset, int length, int receiveTimeout, boolean peek)
             throws IOException {
-        return recvConnectedDatagramDirectImpl(aFD, address, offset, length,
-                receiveTimeout, peek);
+        return recvConnectedDatagramDirectImpl(aFD, packet, address, offset, length, receiveTimeout,
peek);
     }
 
 	public int peekDatagram(FileDescriptor aFD, InetAddress sender,
@@ -543,8 +542,8 @@
 			int receiveTimeout, boolean peek) throws IOException;
     
     static native int receiveDatagramDirectImpl(FileDescriptor aFD,
-            long address, int offset, int length, int receiveTimeout,
-            boolean peek) throws IOException;
+            DatagramPacket packet, long address, int offset, int length,
+            int receiveTimeout, boolean peek) throws IOException;
 
 	/*
 	 * Recieve data on the connected socket into the specified buffer. The
@@ -565,7 +564,7 @@
 			int receiveTimeout, boolean peek) throws IOException;
     
     static native int recvConnectedDatagramDirectImpl(FileDescriptor aFD,
-            long address, int offset, int length,
+            DatagramPacket packet, long address, int offset, int length,
             int receiveTimeout, boolean peek) throws IOException;
 
 	/*

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/OSNetworkSystem.c
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/OSNetworkSystem.c?view=diff&rev=502166&r1=502165&r2=502166
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/OSNetworkSystem.c
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/OSNetworkSystem.c
Thu Feb  1 01:26:26 2007
@@ -44,10 +44,6 @@
 int 
 selectRead (JNIEnv * env,hysocket_t hysocketP, I_32 uSecTime, BOOLEAN accept);
 
-int receiveDatagram(JNIEnv * env, jclass thisClz, jobject fileDescriptor, 
-    jobject datagramPacket, jbyte* message, jint offset, jint msgLength, jint timeout, jboolean
peek);
-
-
 /**
  * A helper method, to set the remote address into the DatagramPacket.
  *
@@ -269,62 +265,6 @@
   setSocketImplPort (env, socketImpl, hysock_ntohs (nPort));
 }
 
-int receiveDatagram(JNIEnv * env,
-		  jclass thisClz,  jobject  fileDescriptor, jobject datagramPacket, jbyte* message,
-		  jint offset, jint msgLength, jint timeout,  jboolean peek)
-{
-  PORT_ACCESS_FROM_ENV (env);
-  hysocket_t hysocketP;	
-  hysockaddr_struct sockaddrP;  
-  I_32 result, localCount;
-  I_32 flags = HYSOCK_NOFLAGS;
-  jbyte	nlocalAddrBytes[HYSOCK_INADDR6_LEN];
-
-  hysocketP = getJavaIoFileDescriptorContentsAsAPointer	(env, fileDescriptor);
-  result = pollSelectRead (env,	fileDescriptor,	timeout, TRUE);	
-  //result = selectRead (env,hysocketP, timeout, FALSE);
-  if (0	> result)
-    return (jint) 0;
-
-  if (!hysock_socketIsValid (hysocketP))
-    {
-      throwJavaNetSocketException (env,	HYPORT_ERROR_SOCKET_BADSOCKET);	
-      return (jint) 0;
-    }
-
-  hysock_sockaddr_init6	(&sockaddrP, (U_8 *) nlocalAddrBytes,
-       HYSOCK_INADDR_LEN, HYADDR_FAMILY_AFINET4, 0, 0, 0,
-       hysocketP);
-
-  localCount = (msgLength < 65536) ? msgLength : 65536;	
- 
-  if (peek)
-    {
-      result = hysock_setflag (HYSOCK_MSG_PEEK,	&flags);
-      if (result)
-	{
-	  throwJavaNetSocketException (env, result);
-	  return (jint)	0;
-	}
-    }
-  result =
-    hysock_readfrom (hysocketP,	(U_8 *)message, localCount, flags, &sockaddrP);  
-  
-  if (result < 0)
-    {
-      throwJavaNetSocketException (env,	result);
-      return (jint) 0;
-    }
-  else
-    {
-      if(datagramPacket != NULL)
-      {
-      	updatePacket (env, &sockaddrP, datagramPacket, result);
-      }
-      return (jint) result;
-    }
-}
-
 /*
  * Class:     org_apache_harmony_luni_platform_OSNetworkSystem
  * Method:    oneTimeInitializationDatagram
@@ -1315,7 +1255,8 @@
       return 0;	
     }
   
-  result = receiveDatagram(env, thisClz, fileDescriptor, datagramPacket, message, offset,
localCount , timeout, peek);
+  result = Java_org_apache_harmony_luni_platform_OSNetworkSystem_receiveDatagramDirectImpl(env,
thisClz, fileDescriptor, 
+      datagramPacket, (jlong)message, offset, localCount , timeout, peek);
     
   if (result > 0)
     {
@@ -1328,18 +1269,69 @@
 /*
  * Class:     org_apache_harmony_luni_platform_OSNetworkSystem
  * Method:    receiveDatagramDirectImpl
- * Signature: (Ljava/io/FileDescriptor;JIIIZ)I
- */
+ * Signature: (Ljava/io/FileDescriptor;Ljava/net/DatagramPacket;JIIIZ)I
+ */ 
 JNIEXPORT jint JNICALL Java_org_apache_harmony_luni_platform_OSNetworkSystem_receiveDatagramDirectImpl(JNIEnv
*env,
 		jclass thisClz, 
-		jobject fileDescriptor, 
+		jobject fileDescriptor,
+		jobject datagramPacket, 
 		jlong address, 
 		jint offset, 
 		jint msgLength, 
 		jint timeout, 
 		jboolean peek)
 {
-  return receiveDatagram(env, thisClz, fileDescriptor, NULL, (jbyte*) address, offset, msgLength,
timeout, peek);
+  PORT_ACCESS_FROM_ENV (env);
+  jbyte *message = (jbyte *)address;
+  hysocket_t hysocketP;	
+  hysockaddr_struct sockaddrP;  
+  I_32 result, localCount;
+  I_32 flags = HYSOCK_NOFLAGS;
+  jbyte	nlocalAddrBytes[HYSOCK_INADDR6_LEN];
+
+  hysocketP = getJavaIoFileDescriptorContentsAsAPointer	(env, fileDescriptor);
+  result = pollSelectRead (env,	fileDescriptor,	timeout, TRUE);	
+  //result = selectRead (env,hysocketP, timeout, FALSE);
+  if (0	> result)
+    return (jint) 0;
+
+  if (!hysock_socketIsValid (hysocketP))
+    {
+      throwJavaNetSocketException (env,	HYPORT_ERROR_SOCKET_BADSOCKET);	
+      return (jint) 0;
+    }
+
+  hysock_sockaddr_init6	(&sockaddrP, (U_8 *) nlocalAddrBytes,
+       HYSOCK_INADDR_LEN, HYADDR_FAMILY_AFINET4, 0, 0, 0,
+       hysocketP);
+
+  localCount = (msgLength < 65536) ? msgLength : 65536;	
+ 
+  if (peek)
+    {
+      result = hysock_setflag (HYSOCK_MSG_PEEK,	&flags);
+      if (result)
+	{
+	  throwJavaNetSocketException (env, result);
+	  return (jint)	0;
+	}
+    }
+  result =
+    hysock_readfrom (hysocketP,	(U_8 *)message, localCount, flags, &sockaddrP);  
+  
+  if (result < 0)
+    {
+      throwJavaNetSocketException (env,	result);
+      return (jint) 0;
+    }
+  else
+    {
+      if(datagramPacket != NULL)
+      {
+      	updatePacket (env, &sockaddrP, datagramPacket, result);
+      }
+      return (jint) result;
+    }
 }
 
 /*
@@ -1378,18 +1370,12 @@
       return 0;	
     }
     
-  result = Java_org_apache_harmony_luni_platform_OSNetworkSystem_recvConnectedDatagramDirectImpl(env,
thisClz, fileDescriptor, (jlong)message, offset, localCount, timeout, peek);  
+  result = Java_org_apache_harmony_luni_platform_OSNetworkSystem_recvConnectedDatagramDirectImpl(env,

+  thisClz, fileDescriptor, datagramPacket, (jlong)message, offset, localCount, timeout, peek);
 
   if (result > 0)
     {
       (*env)->SetByteArrayRegion (env, data, offset, result, message);
-      /* update	the packet with	the legth of data received.
-	 Since we are connected	we did not get back an address.	 This
-	 address is cached within the PlainDatagramSocket  java	object and is filled in	at
-	 the java level	*/
-      if(datagramPacket != NULL)
-      {
-      	setDatagramPacketLength (env, datagramPacket, result);
-      }
+      
     }
   hymem_free_memory ( message);  
   return result; 
@@ -1399,11 +1385,13 @@
 /*
  * Class:     org_apache_harmony_luni_platform_OSNetworkSystem
  * Method:    recvConnectedDatagramDirectImpl
- * Signature: (Ljava/io/FileDescriptor;JIIIZ)I
+ * Signature: (Ljava/io/FileDescriptor;Ljava/net/DatagramPacket;JIIIZ)I
  */
 JNIEXPORT jint JNICALL Java_org_apache_harmony_luni_platform_OSNetworkSystem_recvConnectedDatagramDirectImpl(JNIEnv
*env, 
 		jclass thisClz, 
-		jobject fileDescriptor, 
+		jobject fileDescriptor,
+		jobject
+		datagramPacket, 
 		jlong address, 
 		jint offset, 
 		jint msgLength, 
@@ -1464,6 +1452,14 @@
     }
   else
     {
+     /* update	the packet with	the legth of data received.
+	 Since we are connected	we did not get back an address.	 This
+	 address is cached within the PlainDatagramSocket  java	object and is filled in	at
+	 the java level	*/
+      if(datagramPacket != NULL)
+      {
+      	setDatagramPacketLength (env, datagramPacket, result);
+      }
       return (jint) result;
     }
 }

Modified: harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/OSNetworkSystem.h
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/OSNetworkSystem.h?view=diff&rev=502166&r1=502165&r2=502166
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/OSNetworkSystem.h
(original)
+++ harmony/enhanced/classlib/trunk/modules/luni/src/main/native/luni/shared/OSNetworkSystem.h
Thu Feb  1 01:26:26 2007
@@ -248,11 +248,11 @@
 /*
  * Class:     org_apache_harmony_luni_platform_OSNetworkSystem
  * Method:    receiveDatagramDirectImpl
- * Signature: (Ljava/io/FileDescriptor;JIIIZ)I
+ * Signature: (Ljava/io/FileDescriptor;Ljava/net/DatagramPacket;JIIIZ)I
  */
 JNIEXPORT jint JNICALL Java_org_apache_harmony_luni_platform_OSNetworkSystem_receiveDatagramDirectImpl
-  (JNIEnv *, jclass, jobject, jlong, jint, jint, jint, jboolean);
-
+  (JNIEnv *, jclass, jobject, jobject, jlong, jint, jint, jint, jboolean);
+  
 /*
  * Class:     org_apache_harmony_luni_platform_OSNetworkSystem
  * Method:    recvConnectedDatagramImpl
@@ -264,10 +264,11 @@
 /*
  * Class:     org_apache_harmony_luni_platform_OSNetworkSystem
  * Method:    recvConnectedDatagramDirectImpl
- * Signature: (Ljava/io/FileDescriptor;JIIIZ)I
+ * Signature: (Ljava/io/FileDescriptor;Ljava/net/DatagramPacket;JIIIZ)I
  */
 JNIEXPORT jint JNICALL Java_org_apache_harmony_luni_platform_OSNetworkSystem_recvConnectedDatagramDirectImpl
-  (JNIEnv *, jclass, jobject, jlong, jint, jint, jint, jboolean);
+  (JNIEnv *, jclass, jobject, jobject, jlong, jint, jint, jint, jboolean);
+  
 
 /*
  * Class:     org_apache_harmony_luni_platform_OSNetworkSystem

Modified: harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/DatagramChannelImpl.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/DatagramChannelImpl.java?view=diff&rev=502166&r1=502165&r2=502166
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/DatagramChannelImpl.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/nio/src/main/java/org/apache/harmony/nio/internal/DatagramChannelImpl.java
Thu Feb  1 01:26:26 2007
@@ -63,6 +63,8 @@
     private static final int DEFAULT_TIMEOUT = 1;
 
     private static final int ERRCODE_SOCKET_NONBLOCKING_WOULD_BLOCK = -211;
+    
+    private static final byte[] stubArray = new byte[0];
 
     // -------------------------------------------------------------------
     // Instance variables
@@ -250,43 +252,11 @@
             // receive real data packet, (not peek)
             synchronized (readLock) {
                 boolean loop = isBlocking();
-                do {
-                    DatagramPacket receivePacket = new DatagramPacket(
-                            new byte[target.remaining()], target.remaining());
-
-                    if (isConnected()) {
-                        networkSystem.recvConnectedDatagram(fd, receivePacket,
-                                receivePacket.getData(), receivePacket
-                                        .getOffset(),
-                                receivePacket.getLength(), isBlocking() ? 0
-                                        : DEFAULT_TIMEOUT, false);
-                    } else {
-                        networkSystem.receiveDatagram(fd, receivePacket,
-                                receivePacket.getData(), receivePacket
-                                        .getOffset(),
-                                receivePacket.getLength(), isBlocking() ? 0
-                                        : DEFAULT_TIMEOUT, false);
-                    }
-
-                    // security check
-                    SecurityManager sm = System.getSecurityManager();
-                    if (!isConnected() && null != sm) {
-                        try {
-                            sm.checkAccept(receivePacket.getAddress()
-                                    .getHostAddress(), receivePacket.getPort());
-                        } catch (SecurityException e) {
-                            // do discard the datagram packet
-                            receivePacket = null;
-                        }
-                    }
-                    if (null != receivePacket
-                            && null != receivePacket.getAddress()) {
-                        // copy the data of received packet
-                        target.put(ByteBuffer.wrap(receivePacket.getData()));
-                        retAddr = receivePacket.getSocketAddress();
-                        break;
-                    }
-                } while (loop);
+                if (!target.isDirect()) {
+                    retAddr = receiveImpl(target, loop);
+                } else {
+                    retAddr = receiveDirectImpl(target, loop);
+                }
             }
         } catch (InterruptedIOException e) {
             // this line used in Linux
@@ -296,6 +266,105 @@
         }
         return retAddr;
     }
+    
+    private SocketAddress receiveImpl(ByteBuffer target, boolean loop)
+            throws IOException {
+        SocketAddress retAddr = null;
+        DatagramPacket receivePacket;
+        int oldposition = target.position();
+        int received = 0;
+        if (target.hasArray()) {
+            receivePacket = new DatagramPacket(target.array(), target
+                    .position()
+                    + target.arrayOffset(), target.remaining());
+        } else {        
+            receivePacket = new DatagramPacket(new byte[target.remaining()], target.remaining());
+        }
+        do {
+            if (isConnected()) {
+                received = networkSystem.recvConnectedDatagram(fd, receivePacket,
+                        receivePacket.getData(), receivePacket.getOffset(),
+                        receivePacket.getLength(), isBlocking() ? 0
+                                : DEFAULT_TIMEOUT, false);
+            } else {
+                received = networkSystem.receiveDatagram(fd, receivePacket,
+                        receivePacket.getData(), receivePacket.getOffset(),
+                        receivePacket.getLength(), isBlocking() ? 0
+                                : DEFAULT_TIMEOUT, false);
+            }
+
+            // security check
+            SecurityManager sm = System.getSecurityManager();
+            if (!isConnected() && null != sm) {
+                try {
+                    sm.checkAccept(receivePacket.getAddress().getHostAddress(),
+                            receivePacket.getPort());
+                } catch (SecurityException e) {
+                    // do discard the datagram packet
+                    receivePacket = null;
+                }
+            }
+            if (null != receivePacket && null != receivePacket.getAddress()) {
+                               
+                if (received > 0) {
+                    if (target.hasArray()) {
+                        target.position(oldposition + received);
+                    } else {
+                        // copy the data of received packet
+                        target.put(receivePacket.getData(), 0, received);
+                    }
+                }
+                retAddr = receivePacket.getSocketAddress();
+                break;
+            }
+        } while (loop);
+        return retAddr;
+    }
+    
+    private SocketAddress receiveDirectImpl(ByteBuffer target, boolean loop) throws IOException
+    {
+        SocketAddress retAddr = null;  
+        DatagramPacket receivePacket = new DatagramPacket(
+                stubArray, 0);
+        int oldposition = target.position();
+        int received = 0;
+        do {
+            long address = AddressUtil.getDirectBufferAddress(target);
+            if (isConnected()) {
+                received = networkSystem.recvConnectedDatagramDirect(fd, receivePacket,
+                        address, target.position(),
+                        target.remaining(), isBlocking() ? 0
+                                : DEFAULT_TIMEOUT, false);
+            } else {
+                received = networkSystem.receiveDatagramDirect(fd, receivePacket,
+                        address, target.position(),
+                        target.remaining(), isBlocking() ? 0
+                                : DEFAULT_TIMEOUT, false);
+            }
+
+            // security check
+            SecurityManager sm = System.getSecurityManager();
+            if (!isConnected() && null != sm) {
+                try {
+                    sm.checkAccept(receivePacket.getAddress()
+                            .getHostAddress(), receivePacket.getPort());
+                } catch (SecurityException e) {
+                    // do discard the datagram packet
+                    receivePacket = null;
+                }
+            }
+            if (null != receivePacket
+                    && null != receivePacket.getAddress()) {
+                // copy the data of received packet
+                if (received > 0) {
+                    target.position(oldposition + received);
+                }
+                retAddr = receivePacket.getSocketAddress();
+                break;
+            }
+        } while (loop);
+        return retAddr;
+    }
 
     /*
      * @see java.nio.channels.DatagramChannel#send(java.nio.ByteBuffer,
@@ -468,10 +537,10 @@
                     long address = AddressUtil.getDirectBufferAddress(readBuffer);
                     if (isConnected()) {
                         readCount = networkSystem.recvConnectedDatagramDirect(fd,
-                                address, start, length, timeout, false);
+                                null, address, start, length, timeout, false);
                     } else {
                         readCount = networkSystem.receiveDatagramDirect(fd,
-                                address, start, length, timeout, false);
+                                null, address, start, length, timeout, false);
                     }
                 } else {
                     // the target is assured to have array.



Mime
View raw message