Add better socket linger, tcp_nodelay and timeout handling to thrift


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@664807 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/java/src/transport/TServerSocket.java b/lib/java/src/transport/TServerSocket.java
index 22f8afa..ab2b5e7 100644
--- a/lib/java/src/transport/TServerSocket.java
+++ b/lib/java/src/transport/TServerSocket.java
@@ -85,11 +85,7 @@
       throw new TTransportException("No underlying server socket.");
     }
     try {
-      // Accept socket and tune TCP params
       Socket result = serverSocket_.accept();
-      client.setSoLinger(false, 0);
-      client.setTcpNoDelay(true);
-      // Wrap in TSocket and set timeout
       TSocket result2 = new TSocket(result);
       result2.setTimeout(clientTimeout_);
       return result2;
diff --git a/lib/java/src/transport/TSocket.java b/lib/java/src/transport/TSocket.java
index 544a1d5..4922b04 100644
--- a/lib/java/src/transport/TSocket.java
+++ b/lib/java/src/transport/TSocket.java
@@ -42,7 +42,13 @@
    */
   public TSocket(Socket socket) throws TTransportException {
     socket_ = socket;
-   
+    try {
+      socket_.setSoLinger(false, 0);
+      socket_.setTcpNoDelay(true);
+    } catch (SocketException sx) {
+      sx.printStackTrace();
+    }
+
     if (isOpen()) {
       try {
         inputStream_ = new BufferedInputStream(socket_.getInputStream(), 1024);
@@ -62,7 +68,7 @@
    * @param port Remote port
    */
   public TSocket(String host, int port) {
-    this(host, port, 500);
+    this(host, port, 0);
   }
 
   /**
@@ -74,10 +80,24 @@
    * @param timeout Socket timeout
    */
   public TSocket(String host, int port, int timeout) {
-    socket_ = new Socket();
     host_ = host;
     port_ = port;
     timeout_ = timeout;
+    initSocket();
+  }
+
+  /**
+   * Initializes the socket object
+   */
+  private void initSocket() {
+    socket_ = new Socket();
+    try {
+      socket_.setSoLinger(false, 0);
+      socket_.setTcpNoDelay(true);
+      socket_.setSoTimeout(timeout_);
+    } catch (SocketException sx) {
+      sx.printStackTrace();
+    }
   }
 
   /**
@@ -95,13 +115,11 @@
   }
 
   /**
-   * Returns a reference to the underlying socket. Can be used to set
-   * socket options, etc. If an underlying socket does not exist yet, this
-   * will create one.
+   * Returns a reference to the underlying socket.
    */
   public Socket getSocket() {
     if (socket_ == null) {
-      socket_ = new Socket();
+      initSocket();
     }
     return socket_;
   }
@@ -120,20 +138,21 @@
    * Connects the socket, creating a new socket object if necessary.
    */
   public void open() throws TTransportException {
-    if (socket_ == null) {
-      if (host_.length() == 0) {
-        throw new TTransportException("Cannot open null host.");
-      }
-      if (port_ <= 0) {
-        throw new TTransportException("Cannot open without port.");
-      }
-      socket_ = new Socket();
-    }
-
     if (isOpen()) {
       throw new TTransportException("Socket already connected.");
     }
 
+    if (host_.length() == 0) {
+      throw new TTransportException("Cannot open null host.");
+    }
+    if (port_ <= 0) {
+      throw new TTransportException("Cannot open without port.");
+    }
+
+    if (socket_ == null) {
+      initSocket();
+    }
+
     try {
       socket_.connect(new InetSocketAddress(host_, port_));
       inputStream_ = new BufferedInputStream(socket_.getInputStream(), 1024);