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
This commit is contained in:
Mark Slee 2006-09-27 20:51:11 +00:00
parent 9ffe9d426c
commit 845fe3da64
2 changed files with 36 additions and 21 deletions

View File

@ -85,11 +85,7 @@ public class TServerSocket extends TServerTransport {
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;

View File

@ -42,7 +42,13 @@ public class TSocket extends TIOStreamTransport {
*/
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 @@ public class TSocket extends TIOStreamTransport {
* @param port Remote port
*/
public TSocket(String host, int port) {
this(host, port, 500);
this(host, port, 0);
}
/**
@ -74,10 +80,24 @@ public class TSocket extends TIOStreamTransport {
* @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 @@ public class TSocket extends TIOStreamTransport {
}
/**
* 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 @@ public class TSocket extends TIOStreamTransport {
* 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);