Commit 47d0866c by Jeff Sturm Committed by Jeff Sturm

* java/net/PlainDatagramSocketImpl.java

	(close): Use native implementation.
	(finalize): New method.

	* java/net/PlainSocketImpl.java (finalize): New method.

	* java/net/natPlainDatagramSocketImpl.cc
	(java/io/FileDescriptor.h): Don't include.
	(close): Implement method here.
	(create): Don't assign fd.

	* java/net/natPlainSocketImpl.cc
	(java/io/FileDescriptor.h): Don't include.
	(create): Don't assign fd.
	(accept): Likewise.
	(close): Synchronize.

From-SVN: r51492
parent 1b58660a
2002-03-27 Jeff Sturm <jsturm@one-point.com>
* java/net/PlainDatagramSocketImpl.java
(close): Use native implementation.
(finalize): New method.
* java/net/PlainSocketImpl.java (finalize): New method.
* java/net/natPlainDatagramSocketImpl.cc
(java/io/FileDescriptor.h): Don't include.
(close): Implement method here.
(create): Don't assign fd.
* java/net/natPlainSocketImpl.cc
(java/io/FileDescriptor.h): Don't include.
(create): Don't assign fd.
(accept): Likewise.
(close): Synchronize.
2002-03-27 Richard Henderson <rth@redhat.com> 2002-03-27 Richard Henderson <rth@redhat.com>
* include/posix-threads.h [alpha] (_Jv_ThreadSelf): Avoid a copy. * include/posix-threads.h [alpha] (_Jv_ThreadSelf): Avoid a copy.
......
...@@ -67,27 +67,7 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl ...@@ -67,27 +67,7 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl
public native Object getOption(int optID) throws SocketException; public native Object getOption(int optID) throws SocketException;
private native void mcastGrp(InetAddress inetaddr, boolean join) private native void mcastGrp(InetAddress inetaddr, boolean join)
throws IOException; throws IOException;
protected native void close();
protected void close()
{
// FIXME: The close method in each of the DatagramSocket* classes does
// not throw an IOException. The issue is that FileDescriptor.close()
// in natFileDescriptorPosix.cc can throw one, so we have to catch
// it here. It seems that FileDescriptor.close is properly throwing
// the IOException on errors since many of the java.io classes depend
// on that. This probably requires a bit more research but for now,
// we'll catch the IOException here.
try
{
if (fd.valid())
fd.close();
}
catch (IOException e)
{
System.err.println("PlainDatagramSocketImpl.close: Error closing - " +
e.getMessage());
}
}
// Deprecated in JDK 1.2. // Deprecated in JDK 1.2.
protected byte getTTL() throws IOException protected byte getTTL() throws IOException
...@@ -110,4 +90,14 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl ...@@ -110,4 +90,14 @@ class PlainDatagramSocketImpl extends DatagramSocketImpl
{ {
mcastGrp(inetaddr, false); mcastGrp(inetaddr, false);
} }
protected void finalize() throws Throwable
{
synchronized (this)
{
if (fnum != -1)
close();
}
super.finalize();
}
} }
...@@ -39,11 +39,6 @@ class PlainSocketImpl extends SocketImpl ...@@ -39,11 +39,6 @@ class PlainSocketImpl extends SocketImpl
* This is used for reads and writes to/from the socket and * This is used for reads and writes to/from the socket and
* to close it. * to close it.
* *
* {@link SocketImpl#fd} is created from this like so:
* <pre>
* fd = new FileDescriptor (fnum);
* </pre>
*
* When the socket is closed this is reset to -1. * When the socket is closed this is reset to -1.
*/ */
int fnum = -1; int fnum = -1;
...@@ -108,6 +103,22 @@ class PlainSocketImpl extends SocketImpl ...@@ -108,6 +103,22 @@ class PlainSocketImpl extends SocketImpl
private native void write(byte[] buffer, int offset, int count) private native void write(byte[] buffer, int offset, int count)
throws IOException; throws IOException;
protected void finalize() throws Throwable
{
synchronized (this)
{
if (fnum != -1)
try
{
close();
}
catch (IOException ex)
{
// ignore
}
}
super.finalize();
}
/** @return the input stream attached to the socket. /** @return the input stream attached to the socket.
*/ */
......
...@@ -51,7 +51,6 @@ _Jv_bind (int fd, struct sockaddr *addr, int addrlen) ...@@ -51,7 +51,6 @@ _Jv_bind (int fd, struct sockaddr *addr, int addrlen)
#include <gcj/cni.h> #include <gcj/cni.h>
#include <java/io/IOException.h> #include <java/io/IOException.h>
#include <java/io/FileDescriptor.h>
#include <java/io/InterruptedIOException.h> #include <java/io/InterruptedIOException.h>
#include <java/net/BindException.h> #include <java/net/BindException.h>
#include <java/net/SocketException.h> #include <java/net/SocketException.h>
...@@ -91,6 +90,13 @@ java::net::PlainDatagramSocketImpl::peek (java::net::InetAddress *) ...@@ -91,6 +90,13 @@ java::net::PlainDatagramSocketImpl::peek (java::net::InetAddress *)
} }
void void
java::net::PlainDatagramSocketImpl::close ()
{
throw new java::io::IOException (
JvNewStringLatin1 ("DatagramSocketImpl.close: unimplemented"));
}
void
java::net::PlainDatagramSocketImpl::send (java::net::DatagramPacket *) java::net::PlainDatagramSocketImpl::send (java::net::DatagramPacket *)
{ {
throw new java::io::IOException ( throw new java::io::IOException (
...@@ -188,8 +194,9 @@ java::net::PlainDatagramSocketImpl::create () ...@@ -188,8 +194,9 @@ java::net::PlainDatagramSocketImpl::create ()
_Jv_platform_close_on_exec (sock); _Jv_platform_close_on_exec (sock);
// We use fnum in place of fd here. From leaving fd null we avoid
// the double close problem in FileDescriptor.finalize.
fnum = sock; fnum = sock;
fd = new java::io::FileDescriptor (sock);
} }
void void
...@@ -284,6 +291,19 @@ java::net::PlainDatagramSocketImpl::peek (java::net::InetAddress *i) ...@@ -284,6 +291,19 @@ java::net::PlainDatagramSocketImpl::peek (java::net::InetAddress *i)
throw new java::io::IOException (JvNewStringUTF (strerr)); throw new java::io::IOException (JvNewStringUTF (strerr));
} }
// Close(shutdown) the socket.
void
java::net::PlainDatagramSocketImpl::close ()
{
// Avoid races from asynchronous finalization.
JvSynchronize sync (this);
// The method isn't declared to throw anything, so we disregard
// the return value.
::close (fnum);
fnum = -1;
}
void void
java::net::PlainDatagramSocketImpl::send (java::net::DatagramPacket *p) java::net::PlainDatagramSocketImpl::send (java::net::DatagramPacket *p)
{ {
......
...@@ -102,7 +102,6 @@ _Jv_accept (int fd, struct sockaddr *addr, socklen_t *addrlen) ...@@ -102,7 +102,6 @@ _Jv_accept (int fd, struct sockaddr *addr, socklen_t *addrlen)
#include <gcj/cni.h> #include <gcj/cni.h>
#include <gcj/javaprims.h> #include <gcj/javaprims.h>
#include <java/io/IOException.h> #include <java/io/IOException.h>
#include <java/io/FileDescriptor.h>
#include <java/io/InterruptedIOException.h> #include <java/io/InterruptedIOException.h>
#include <java/net/BindException.h> #include <java/net/BindException.h>
#include <java/net/ConnectException.h> #include <java/net/ConnectException.h>
...@@ -234,8 +233,9 @@ java::net::PlainSocketImpl::create (jboolean stream) ...@@ -234,8 +233,9 @@ java::net::PlainSocketImpl::create (jboolean stream)
_Jv_platform_close_on_exec (sock); _Jv_platform_close_on_exec (sock);
// We use fnum in place of fd here. From leaving fd null we avoid
// the double close problem in FileDescriptor.finalize.
fnum = sock; fnum = sock;
fd = new java::io::FileDescriptor (sock);
} }
void void
...@@ -402,7 +402,6 @@ java::net::PlainSocketImpl::accept (java::net::PlainSocketImpl *s) ...@@ -402,7 +402,6 @@ java::net::PlainSocketImpl::accept (java::net::PlainSocketImpl *s)
s->localport = localport; s->localport = localport;
s->address = new InetAddress (raddr, NULL); s->address = new InetAddress (raddr, NULL);
s->port = rport; s->port = rport;
s->fd = new java::io::FileDescriptor (new_socket);
return; return;
error: error:
char* strerr = strerror (errno); char* strerr = strerror (errno);
...@@ -413,6 +412,9 @@ java::net::PlainSocketImpl::accept (java::net::PlainSocketImpl *s) ...@@ -413,6 +412,9 @@ java::net::PlainSocketImpl::accept (java::net::PlainSocketImpl *s)
void void
java::net::PlainSocketImpl::close() java::net::PlainSocketImpl::close()
{ {
// Avoid races from asynchronous finalization.
JvSynchronize sync (this);
// should we use shutdown here? how would that effect so_linger? // should we use shutdown here? how would that effect so_linger?
int res = ::close (fnum); int res = ::close (fnum);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment