tcpSendBuffer_(0),
tcpRecvBuffer_(0),
intSock1_(THRIFT_INVALID_SOCKET),
- intSock2_(THRIFT_INVALID_SOCKET) {}
+ intSock2_(THRIFT_INVALID_SOCKET),
+ keepAlive_(false)
+{}
TServerSocket::TServerSocket(int port, int sendTimeout, int recvTimeout) :
port_(port),
tcpSendBuffer_(0),
tcpRecvBuffer_(0),
intSock1_(THRIFT_INVALID_SOCKET),
- intSock2_(THRIFT_INVALID_SOCKET) {}
+ intSock2_(THRIFT_INVALID_SOCKET),
+ keepAlive_(false)
+{}
TServerSocket::TServerSocket(string path) :
port_(0),
tcpSendBuffer_(0),
tcpRecvBuffer_(0),
intSock1_(THRIFT_INVALID_SOCKET),
- intSock2_(THRIFT_INVALID_SOCKET) {}
+ intSock2_(THRIFT_INVALID_SOCKET),
+ keepAlive_(false)
+{}
TServerSocket::~TServerSocket() {
close();
if (recvTimeout_ > 0) {
client->setRecvTimeout(recvTimeout_);
}
+ if (keepAlive_) {
+ client->setKeepAlive(keepAlive_);
+ }
client->setCachedAddress((sockaddr*) &clientAddress, size);
return client;
void setRetryLimit(int retryLimit);
void setRetryDelay(int retryDelay);
+ void setKeepAlive(bool keepAlive) {keepAlive_ = keepAlive;}
+
void setTcpSendBuffer(int tcpSendBuffer);
void setTcpRecvBuffer(int tcpRecvBuffer);
int retryDelay_;
int tcpSendBuffer_;
int tcpRecvBuffer_;
+ bool keepAlive_;
THRIFT_SOCKET intSock1_;
THRIFT_SOCKET intSock2_;
connTimeout_(0),
sendTimeout_(0),
recvTimeout_(0),
+ keepAlive_(false),
lingerOn_(1),
lingerVal_(0),
noDelay_(1),
connTimeout_(0),
sendTimeout_(0),
recvTimeout_(0),
+ keepAlive_(false),
lingerOn_(1),
lingerVal_(0),
noDelay_(1),
connTimeout_(0),
sendTimeout_(0),
recvTimeout_(0),
+ keepAlive_(false),
lingerOn_(1),
lingerVal_(0),
noDelay_(1),
connTimeout_(0),
sendTimeout_(0),
recvTimeout_(0),
+ keepAlive_(false),
lingerOn_(1),
lingerVal_(0),
noDelay_(1),
setRecvTimeout(recvTimeout_);
}
+ if(keepAlive_) {
+ setKeepAlive(keepAlive_);
+ }
+
// Linger
setLinger(lingerOn_, lingerVal_);
}
}
+void TSocket::setKeepAlive(bool keepAlive) {
+ keepAlive_ = keepAlive;
+
+ if (socket_ == -1) {
+ return;
+ }
+
+ int value = keepAlive_;
+ int ret = setsockopt(socket_, SOL_SOCKET, SO_KEEPALIVE, const_cast_sockopt(&value), sizeof(value));
+
+ if (ret == -1) {
+ int errno_copy = THRIFT_GET_SOCKET_ERROR; // Copy THRIFT_GET_SOCKET_ERROR because we're allocating memory.
+ GlobalOutput.perror("TSocket::setKeepAlive() setsockopt() " + getSocketInfo(), errno_copy);
+ }
+}
+
void TSocket::setMaxRecvRetries(int maxRecvRetries) {
maxRecvRetries_ = maxRecvRetries;
}
*/
void setMaxRecvRetries(int maxRecvRetries);
+ /**
+ * Set SO_KEEPALIVE
+ */
+ void setKeepAlive(bool keepAlive);
+
/**
* Get socket information formated as a string <Host: x Port: x>
*/
/** Recv timeout in ms */
int recvTimeout_;
+ /** Keep alive on */
+ bool keepAlive_;
+
/** Linger on */
bool lingerOn_;