Commit 4293c55a authored by legoc's avatar legoc
Browse files

Set the socket linger according to the timeout

parent c28287f2
......@@ -60,13 +60,9 @@ Server::Server(const std::string& endpoint, int timeoutMs) :
m_eventThread.reset(new EventThread(this, socket));
m_eventThread->start();
}
catch (const std::exception& e) {
cout << "event error " << e.what() << endl;
}
catch (...) {
// ...
}
}
Server::~Server() {
......
......@@ -29,9 +29,9 @@ namespace cameo {
RequestSocketImpl::RequestSocketImpl(ServicesImpl * services, const std::string& endpoint, int timeout) :
m_services(services), m_endpoint(endpoint) {
setTimeout(timeout);
init();
setTimeout(timeout);
}
RequestSocketImpl::~RequestSocketImpl() {
......@@ -40,6 +40,11 @@ RequestSocketImpl::~RequestSocketImpl() {
void RequestSocketImpl::setTimeout(int timeout) {
m_timeout = timeout;
// Apply the linger to the socket.
setSocketLinger();
}
void RequestSocketImpl::setSocketLinger() {
// Set the linger in case of timeout.
// If not, the context can block indefinitely.
// Does the value 100 can lead to a side-effect? A too small value like 1 has some side-effect.
......@@ -53,8 +58,12 @@ void RequestSocketImpl::setTimeout(int timeout) {
void RequestSocketImpl::init() {
// Reset if the socket is null.
if (m_socket.get() == nullptr) {
m_socket.reset(m_services->createRequestSocket(m_endpoint));
// Apply the linger to the socket.
setSocketLinger();
}
}
......@@ -98,7 +107,7 @@ std::unique_ptr<zmq::message_t> RequestSocketImpl::request(const std::string& re
int rc = zmq::poll(items, 1, timeout);
if (rc == 0) {
// Reset the socket.
// Reset the socket. It is necessary if a new request is done.
reset();
// Timeout occurred.
......
......@@ -32,6 +32,7 @@ public:
virtual ~RequestSocketImpl();
void setTimeout(int timeout);
void setSocketLinger();
void init();
void reset();
......
......@@ -406,27 +406,21 @@ std::string ServicesImpl::createOutputRequest(const std::string& name) const {
bool ServicesImpl::isAvailable(RequestSocketImpl * socket, int timeout) {
cout << 1 << endl;
string requestTypePart = createRequestType(PROTO_INIT);
string requestDataPart = createInitRequest();
cout << 2 << endl;
try {
unique_ptr<zmq::message_t> reply = socket->request(requestTypePart, requestDataPart, timeout);
cout << 3 << endl;
if (reply.get() != nullptr) {
return true;
}
} catch (const ConnectionTimeout&) {
}
catch (const ConnectionTimeout&) {
// The server is not accessible.
cout << 4 << endl;
} catch (const std::exception& e) {
cout << "error " << e.what() << endl;
}
catch (...) {
// Should not happen.
}
return false;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment