From 8d37f2054b6da02957b12d02aaf96dd5eecc89c7 Mon Sep 17 00:00:00 2001 From: Brian Hinz Date: Wed, 8 Feb 2012 04:21:43 +0000 Subject: [PATCH] corrected return value for read function to match unix socket read. updated exception handling to match C code. git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@4846 3789f03b-4d11-0410-bbf8-ca57d06f2519 --- java/com/tigervnc/network/FileDescriptor.java | 8 +++--- .../tigervnc/network/SocketDescriptor.java | 26 ++++++++--------- .../com/tigervnc/network/SocketException.java | 28 +++++++++++++++++++ java/com/tigervnc/network/TcpSocket.java | 18 ++++++------ java/com/tigervnc/rdr/Exception.java | 12 ++++++++ java/com/tigervnc/rdr/FdInStream.java | 12 ++++---- java/com/tigervnc/rdr/SystemException.java | 26 +++++++++++++++++ 7 files changed, 94 insertions(+), 36 deletions(-) create mode 100644 java/com/tigervnc/network/SocketException.java create mode 100644 java/com/tigervnc/rdr/SystemException.java diff --git a/java/com/tigervnc/network/FileDescriptor.java b/java/com/tigervnc/network/FileDescriptor.java index e2d04fab..28ea96b8 100644 --- a/java/com/tigervnc/network/FileDescriptor.java +++ b/java/com/tigervnc/network/FileDescriptor.java @@ -18,14 +18,14 @@ */ package com.tigervnc.network; -import java.lang.Exception; import java.io.IOException; +import com.tigervnc.rdr.Exception; public interface FileDescriptor { - public int read(byte[] buf, int bufPtr, int length) throws java.lang.Exception; - public int write(byte[] buf, int bufPtr, int length) throws java.lang.Exception; - public int select(int interestOps, int timeout) throws java.lang.Exception; + public int read(byte[] buf, int bufPtr, int length) throws Exception; + public int write(byte[] buf, int bufPtr, int length) throws Exception; + public int select(int interestOps, int timeout) throws Exception; public void close() throws IOException; } diff --git a/java/com/tigervnc/network/SocketDescriptor.java b/java/com/tigervnc/network/SocketDescriptor.java index 375b0335..1998ca5b 100644 --- a/java/com/tigervnc/network/SocketDescriptor.java +++ b/java/com/tigervnc/network/SocketDescriptor.java @@ -19,7 +19,6 @@ package com.tigervnc.network; import java.io.IOException; -import java.lang.Exception; import java.net.SocketAddress; import java.nio.*; @@ -29,6 +28,8 @@ import java.nio.channels.spi.SelectorProvider; import java.util.Set; import java.util.Iterator; +import com.tigervnc.rdr.Exception; + public class SocketDescriptor extends SocketChannel implements FileDescriptor { @@ -38,7 +39,7 @@ public class SocketDescriptor extends SocketChannel channel = SocketChannel.open(); channel.configureBlocking(false); selector = Selector.open(); - } catch (java.io.IOException e) { + } catch (IOException e) { throw new Exception(e.toString()); } try { @@ -54,11 +55,10 @@ public class SocketDescriptor extends SocketChannel try { n = channel.read(b); } catch (java.io.IOException e) { - System.out.println(e.toString()); throw new Exception(e.toString()); } - //if (n == 0) - // throw new Exception; + if (n <= 0) + return (n == 0) ? -1 : 0; b.flip(); b.get(buf, bufPtr, n); b.clear(); @@ -74,7 +74,6 @@ public class SocketDescriptor extends SocketChannel try { n = channel.write(b); } catch (java.io.IOException e) { - System.out.println(e.toString()); throw new Exception(e.toString()); } b.clear(); @@ -85,8 +84,7 @@ public class SocketDescriptor extends SocketChannel int n; try { n = selector.select(timeout); - } catch (Exception e) { - System.out.println(e.toString()); + } catch (java.io.IOException e) { throw new Exception(e.toString()); } Set keys = selector.selectedKeys(); @@ -104,13 +102,12 @@ public class SocketDescriptor extends SocketChannel return n; } - public int write(ByteBuffer buf) throws IOException { + public int write(ByteBuffer buf) throws Exception { int n = 0; try { n = channel.write(buf); } catch (java.io.IOException e) { - System.out.println(e.toString()); - throw e; + throw new Exception(e.toString()); } return n; } @@ -122,7 +119,7 @@ public class SocketDescriptor extends SocketChannel try { n = channel.write(buf, offset, length); } catch (java.io.IOException e) { - System.out.println(e.toString()); + throw new Exception(e.toString()); } return n; } @@ -132,8 +129,7 @@ public class SocketDescriptor extends SocketChannel try { n = channel.read(buf); } catch (java.io.IOException e) { - System.out.println(e.toString()); - throw e; + throw new Exception(e.toString()); } return n; } @@ -145,7 +141,7 @@ public class SocketDescriptor extends SocketChannel try { n = channel.read(buf, offset, length); } catch (java.io.IOException e) { - System.out.println(e.toString()); + throw new Exception(e.toString()); } return n; } diff --git a/java/com/tigervnc/network/SocketException.java b/java/com/tigervnc/network/SocketException.java new file mode 100644 index 00000000..6f795f93 --- /dev/null +++ b/java/com/tigervnc/network/SocketException.java @@ -0,0 +1,28 @@ +/* Copyright (C) 2012 TigerVNC Team + * + * This is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ + +package com.tigervnc.network; + +import com.tigervnc.rdr.SystemException; + +public class SocketException extends SystemException { + public SocketException(String s) { + super(s); + } +} + diff --git a/java/com/tigervnc/network/TcpSocket.java b/java/com/tigervnc/network/TcpSocket.java index 926aa975..9277dd14 100644 --- a/java/com/tigervnc/network/TcpSocket.java +++ b/java/com/tigervnc/network/TcpSocket.java @@ -21,13 +21,13 @@ package com.tigervnc.network; import com.tigervnc.rdr.FdInStream; import com.tigervnc.rdr.FdOutStream; +import com.tigervnc.rdr.Exception; import com.tigervnc.rfb.LogWriter; import java.io.IOException; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.SocketAddress; -import java.net.SocketException; import java.net.UnknownHostException; import java.nio.*; import java.nio.channels.*; @@ -46,7 +46,6 @@ public class TcpSocket extends Socket { public TcpSocket(SocketDescriptor sock, boolean close) { super(new FdInStream(sock), new FdOutStream(sock), true); - //this.sock = sock; closeFd = close; } @@ -71,30 +70,29 @@ public class TcpSocket extends Socket { try { sock = new SocketDescriptor(); - } catch(UnknownHostException e) { - throw new Exception("unable to create socket: "+e.toString()); + } catch(Exception e) { + throw new SocketException("unable to create socket: "+e.toString()); } /* Attempt to connect to the remote host */ try { result = sock.connect(new InetSocketAddress(addr, port)); } catch(java.io.IOException e) { - //throw new java.lang.Exception(e.getMessage()); - System.out.println("connect failed: "+e.getMessage()); + throw new SocketException("unable to connect:"+e.getMessage()); } if (!result && sock.isConnectionPending()) { while (!result) { try { result = sock.finishConnect(); - } catch(java.nio.channels.ClosedChannelException e) { + } catch(java.io.IOException e) { throw new Exception(e.getMessage()); } } } if (!result) - throw new Exception("unable connect to socket"); + throw new SocketException("unable connect to socket"); // Disable Nagle's algorithm, to reduce latency enableNagles(sock, false); @@ -154,8 +152,8 @@ public class TcpSocket extends Socket { public static boolean enableNagles(SocketChannel sock, boolean enable) { try { sock.socket().setTcpNoDelay(!enable); - } catch(SocketException e) { - vlog.error(e.getMessage()); + } catch(java.net.SocketException e) { + vlog.error("unable to setsockopt TCP_NODELAY: "+e.getMessage()); return false; } return true; diff --git a/java/com/tigervnc/rdr/Exception.java b/java/com/tigervnc/rdr/Exception.java index a5fe938d..a2f8833d 100644 --- a/java/com/tigervnc/rdr/Exception.java +++ b/java/com/tigervnc/rdr/Exception.java @@ -23,3 +23,15 @@ public class Exception extends RuntimeException { super(s); } } + +class TimedOut extends Exception { + public TimedOut() { + super("Timed out"); + } +} + +class FrameException extends Exception { + public FrameException() { + super("Frame Exception"); + } +} diff --git a/java/com/tigervnc/rdr/FdInStream.java b/java/com/tigervnc/rdr/FdInStream.java index c638f4b2..245a6340 100644 --- a/java/com/tigervnc/rdr/FdInStream.java +++ b/java/com/tigervnc/rdr/FdInStream.java @@ -153,24 +153,22 @@ public class FdInStream extends InStream { try { n = fd.select(SelectionKey.OP_READ, timeoutms); - } catch (java.lang.Exception e) { - System.out.println(e.toString()); - throw new Exception(e.toString()); + } catch (Exception e) { + throw new SystemException("select:"+e.toString()); } if (n > 0) break; if (!wait) return 0; - //if (blockCallback == null) throw TimedOut(); + if (blockCallback == null) throw new TimedOut(); blockCallback.blockCallback(); } try { n = fd.read(buf, bufPtr, len); - } catch (java.lang.Exception e) { - System.out.println("read:"+e.toString()); - throw new Exception(e.toString()); + } catch (Exception e) { + throw new SystemException("read:"+e.toString()); } if (n == 0) throw new EndOfStream(); diff --git a/java/com/tigervnc/rdr/SystemException.java b/java/com/tigervnc/rdr/SystemException.java new file mode 100644 index 00000000..e3df2634 --- /dev/null +++ b/java/com/tigervnc/rdr/SystemException.java @@ -0,0 +1,26 @@ +/* Copyright (C) 2012 TigerVNC Team + * + * This is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This software is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this software; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ + +package com.tigervnc.rdr; + +public class SystemException extends Exception { + public SystemException(String s) { + super(s); + } +} + -- 2.39.5