Patch: FYI: fixlet in nio socket

Tom Tromey tromey@redhat.com
Mon Apr 2 21:19:00 GMT 2007


I'm checking this in on the trunk and the RH 4.1 branch.

This is a somewhat lame fix for nio sockets.  It fixes a bug seen
while running Azureus.

The bug is that it is valid to set options on a brand-new nio
socket.  So, we have to create the underlying socket in the
SocketChannelImpl constructor.

This is already fixed in Classpath.  However, a full nio merge is
pretty big, including changes to the Windows port, and I don't have
time for it at the moment :(.  It is definitely something we want,
though, since Classpath has an epoll-based selector implementation,
which ought to be noticeably better.

Tom

Index: ChangeLog
from  Tom Tromey  <tromey@redhat.com>

	https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=233406
	* gnu/java/net/natPlainSocketImplPosix.cc (create): Return if
	already created.
	* gnu/java/net/PlainSocketImpl.java (getLocalAddress): Handle case
	where localport is -1.
	(create): Now public.
	* gnu/java/nio/SocketChannelImpl.java (SocketChannelImpl): Call
	'create' on the socket.

Index: gnu/java/nio/SocketChannelImpl.java
===================================================================
--- gnu/java/nio/SocketChannelImpl.java	(revision 123266)
+++ gnu/java/nio/SocketChannelImpl.java	(working copy)
@@ -1,5 +1,5 @@
 /* SocketChannelImpl.java -- 
-   Copyright (C) 2002, 2003, 2004, 2006 Free Software Foundation, Inc.
+   Copyright (C) 2002, 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
 
 This file is part of GNU Classpath.
 
@@ -71,6 +71,7 @@
   {
     super (provider);
     impl = new PlainSocketImpl();
+    impl.create(true);
     socket = new NIOSocket (impl, this);
     configureBlocking(true);
   }
Index: gnu/java/net/natPlainSocketImplPosix.cc
===================================================================
--- gnu/java/net/natPlainSocketImplPosix.cc	(revision 123266)
+++ gnu/java/net/natPlainSocketImplPosix.cc	(working copy)
@@ -64,6 +64,10 @@
 void
 gnu::java::net::PlainSocketImpl::create (jboolean stream)
 {
+  // We might already have been create()d in the nio case.
+  if (native_fd != -1)
+    return;
+
   int sock = _Jv_socket (AF_INET, stream ? SOCK_STREAM : SOCK_DGRAM, 0);
 
   if (sock < 0)
Index: gnu/java/net/PlainSocketImpl.java
===================================================================
--- gnu/java/net/PlainSocketImpl.java	(revision 123266)
+++ gnu/java/net/PlainSocketImpl.java	(working copy)
@@ -228,7 +228,9 @@
    *
    * @param stream true for a stream socket, false for a datagram socket
    */
-  protected native void create(boolean stream) throws IOException;
+  // FIXME: this is public for nio ... but this is just a hack
+  // until we upgrade to Classpath's nio.
+  public native void create(boolean stream) throws IOException;
 
   /**
    * Connects to the remote hostname and port specified as arguments.
@@ -336,7 +338,7 @@
 	  {
 	    localSocketAddress
 	      = new InetSocketAddress ((InetAddress) getOption(SocketOptions.SO_BINDADDR),
-				       localport);
+				       localport == -1 ? 0 : localport);
 	  }
 	catch (SocketException _)
 	  {



More information about the Java-patches mailing list