--- /home/tromey/gnu/Nightly/classpath/classpath/gnu/java/net/PlainSocketImpl.java 2004-03-19 02:20:09.000000000 -0700 +++ gnu/java/net/PlainSocketImpl.java 2004-03-12 02:18:23.000000000 -0700 @@ -76,6 +76,23 @@ System.loadLibrary("javanet"); } } + + // These fields are mirrored for use in native code to avoid cpp conflicts + // when the #defines in system header files are the same as the public fields. + static final int _Jv_TCP_NODELAY_ = SocketOptions.TCP_NODELAY, + _Jv_SO_BINDADDR_ = SocketOptions.SO_BINDADDR, + _Jv_SO_REUSEADDR_ = SocketOptions.SO_REUSEADDR, + _Jv_SO_BROADCAST_ = SocketOptions.SO_BROADCAST, + _Jv_SO_OOBINLINE_ = SocketOptions.SO_OOBINLINE, + _Jv_IP_MULTICAST_IF_ = SocketOptions.IP_MULTICAST_IF, + _Jv_IP_MULTICAST_IF2_ = SocketOptions.IP_MULTICAST_IF2, + _Jv_IP_MULTICAST_LOOP_ = SocketOptions.IP_MULTICAST_LOOP, + _Jv_IP_TOS_ = SocketOptions.IP_TOS, + _Jv_SO_LINGER_ = SocketOptions.SO_LINGER, + _Jv_SO_TIMEOUT_ = SocketOptions.SO_TIMEOUT, + _Jv_SO_SNDBUF_ = SocketOptions.SO_SNDBUF, + _Jv_SO_RCVBUF_ = SocketOptions.SO_RCVBUF, + _Jv_SO_KEEPALIVE_ = SocketOptions.SO_KEEPALIVE; /** * The OS file handle representing the socket. @@ -86,6 +103,12 @@ */ int native_fd = -1; + // This value is set/read by setOption/getOption. + int timeout = 0; + + // localAddress cache + InetAddress localAddress; + /** * A cached copy of the in stream for reading from the socket. */ @@ -177,15 +200,9 @@ */ public native Object getOption(int optID) throws SocketException; - public void shutdownInput() - { - throw new InternalError ("PlainSocketImpl::shutdownInput not implemented"); - } + public native void shutdownInput() throws IOException; - public void shutdownOutput() - { - throw new InternalError ("PlainSocketImpl::shutdownOutput not implemented"); - } + public native void shutdownOutput() throws IOException; /** * Creates a new socket that is not bound to any local address/port and @@ -195,7 +212,7 @@ * * @param stream true for a stream socket, false for a datagram socket */ - protected native synchronized void create(boolean stream) throws IOException; + protected native void create(boolean stream) throws IOException; /** * Connects to the remote hostname and port specified as arguments. @@ -205,7 +222,7 @@ * * @exception IOException If an error occurs */ - protected synchronized void connect(String host, int port) throws IOException + protected void connect(String host, int port) throws IOException { connect(InetAddress.getByName(host), port); } @@ -218,7 +235,10 @@ * * @exception IOException If an error occurs */ - protected native void connect(InetAddress addr, int port) throws IOException; + protected void connect(InetAddress host, int port) throws IOException + { + connect (new InetSocketAddress (host, port), 0); + } /** * Connects to the remote socket address with a specified timeout. @@ -227,33 +247,7 @@ * * @exception IOException If an error occurs */ - protected synchronized void connect(SocketAddress address, int timeout) throws IOException - { - InetSocketAddress sockAddr = (InetSocketAddress) address; - InetAddress addr = sockAddr.getAddress(); - - if (addr == null) - throw new IllegalArgumentException("address is unresolved: " + sockAddr); - - int port = sockAddr.getPort(); - - if (timeout < 0) - throw new IllegalArgumentException("negative timeout"); - - Object oldTimeoutObj = null; - - try - { - oldTimeoutObj = this.getOption (SocketOptions.SO_TIMEOUT); - this.setOption (SocketOptions.SO_TIMEOUT, new Integer (timeout)); - connect (addr, port); - } - finally - { - if (oldTimeoutObj != null) - this.setOption (SocketOptions.SO_TIMEOUT, oldTimeoutObj); - } - } + protected native void connect(SocketAddress addr, int timeout) throws IOException; /** * Binds to the specified port on the specified addr. Note that this addr @@ -264,7 +258,7 @@ * * @exception IOException If an error occurs */ - protected native synchronized void bind(InetAddress addr, int port) + protected native void bind(InetAddress host, int port) throws IOException; /** @@ -277,7 +271,7 @@ * * @exception IOException If an error occurs */ - protected native synchronized void listen(int queuelen) + protected native void listen(int queuelen) throws IOException; /** @@ -286,7 +280,13 @@ * * @param impl The SocketImpl object to accept this connection. */ - protected native synchronized void accept(SocketImpl impl) + protected void accept(SocketImpl impl) + throws IOException + { + accept((PlainSocketImpl) impl); + } + + private native void accept(PlainSocketImpl impl) throws IOException; /** @@ -310,32 +310,7 @@ */ protected native void close() throws IOException; - public void sendUrgentData(int data) - { - throw new InternalError ("PlainSocketImpl::sendUrgentData not implemented"); - } - - /** - * Internal method used by SocketInputStream for reading data from - * the connection. Reads up to len bytes of data into the buffer - * buf starting at offset bytes into the buffer. - * - * @return The actual number of bytes read or -1 if end of stream. - * - * @exception IOException If an error occurs - */ - protected native int read(byte[] buf, int offset, int len) - throws IOException; - - /** - * Internal method used by SocketOuputStream for writing data to - * the connection. Writes up to len bytes of data from the buffer - * buf starting at offset bytes into the buffer. - * - * @exception IOException If an error occurs - */ - protected native void write(byte[] buf, int offset, int len) - throws IOException; + protected native void sendUrgentData(int data) throws IOException; /** * Returns an InputStream object for reading from this socket. This will @@ -403,16 +378,7 @@ * * @exception IOException If an error occurs. */ - public int read() throws IOException - { - byte buf[] = new byte [1]; - int bytes_read = read(buf, 0, 1); - - if (bytes_read == -1) - return -1; - - return buf[0] & 0xFF; - } + public native int read() throws IOException; /** * Reads up to len bytes of data into the caller supplied buffer starting @@ -426,15 +392,7 @@ * * @exception IOException If an error occurs. */ - public int read (byte[] buf, int offset, int len) throws IOException - { - int bytes_read = PlainSocketImpl.this.read (buf, offset, len); - - if (bytes_read == 0) - return -1; - - return bytes_read; - } + public native int read(byte[] buf, int offset, int len) throws IOException; } /** @@ -467,11 +425,7 @@ * * @exception IOException If an error occurs */ - public void write(int b) throws IOException - { - byte buf[] = { (byte) b }; - write(buf, 0, 1); - } + public native void write(int b) throws IOException; /** * Writes len number of bytes from the array buf to the stream starting @@ -483,9 +437,6 @@ * * @exception IOException If an error occurs. */ - public void write (byte[] buf, int offset, int len) throws IOException - { - PlainSocketImpl.this.write (buf, offset, len); - } + public native void write(byte[] buf, int offset, int len) throws IOException; } }