refactor: deduplicate more zmq/shmem code

This commit is contained in:
Alexey Rybalchenko
2021-11-22 12:42:09 +01:00
parent f24dee33c2
commit 953c4a75c8
3 changed files with 83 additions and 105 deletions

View File

@@ -85,57 +85,12 @@ class Socket final : public fair::mq::Socket
bool Bind(const std::string& address) override
{
// LOG(debug) << "Binding socket " << fId << " on " << address;
if (zmq_bind(fSocket, address.c_str()) != 0) {
if (errno == EADDRINUSE) {
// do not print error in this case, this is handled by FairMQDevice in case no
// connection could be established after trying a number of random ports from a range.
return false;
}
LOG(error) << "Failed binding socket " << fId << ", address: " << address << ", reason: " << zmq_strerror(errno);
return false;
}
return true;
return zmq::Bind(fSocket, address, fId);
}
bool Connect(const std::string& address) override
{
// LOG(debug) << "Connecting socket " << fId << " on " << address;
if (zmq_connect(fSocket, address.c_str()) != 0) {
LOG(error) << "Failed connecting socket " << fId << ", address: " << address << ", reason: " << zmq_strerror(errno);
return false;
}
return true;
}
bool ShouldRetry(int flags, int timeout, int& elapsed) const
{
if ((flags & ZMQ_DONTWAIT) == 0) {
if (timeout > 0) {
elapsed += fTimeout;
if (elapsed >= timeout) {
return false;
}
}
return true;
} else {
return false;
}
}
int HandleErrors() const
{
if (zmq_errno() == ETERM) {
LOG(debug) << "Terminating socket " << fId;
return static_cast<int>(TransferCode::error);
} else {
LOG(error) << "Failed transfer on socket " << fId << ", errno: " << errno << ", reason: " << zmq_strerror(errno);
return static_cast<int>(TransferCode::error);
}
return zmq::Connect(fSocket, address, fId);
}
int64_t Send(MessagePtr& msg, int timeout = -1) override
@@ -157,13 +112,13 @@ class Socket final : public fair::mq::Socket
} else if (zmq_errno() == EAGAIN || zmq_errno() == EINTR) {
if (fCtx.Interrupted()) {
return static_cast<int>(TransferCode::interrupted);
} else if (ShouldRetry(flags, timeout, elapsed)) {
} else if (zmq::ShouldRetry(flags, fTimeout, timeout, elapsed)) {
continue;
} else {
return static_cast<int>(TransferCode::timeout);
}
} else {
return HandleErrors();
return zmq::HandleErrors(fId);
}
}
}
@@ -187,13 +142,13 @@ class Socket final : public fair::mq::Socket
} else if (zmq_errno() == EAGAIN || zmq_errno() == EINTR) {
if (fCtx.Interrupted()) {
return static_cast<int>(TransferCode::interrupted);
} else if (ShouldRetry(flags, timeout, elapsed)) {
} else if (zmq::ShouldRetry(flags, fTimeout, timeout, elapsed)) {
continue;
} else {
return static_cast<int>(TransferCode::timeout);
}
} else {
return HandleErrors();
return zmq::HandleErrors(fId);
}
}
}
@@ -222,14 +177,14 @@ class Socket final : public fair::mq::Socket
} else if (zmq_errno() == EAGAIN || zmq_errno() == EINTR) {
if (fCtx.Interrupted()) {
return static_cast<int>(TransferCode::interrupted);
} else if (ShouldRetry(flags, timeout, elapsed)) {
} else if (zmq::ShouldRetry(flags, fTimeout, timeout, elapsed)) {
repeat = true;
break;
} else {
return static_cast<int>(TransferCode::timeout);
}
} else {
return HandleErrors();
return zmq::HandleErrors(fId);
}
}
@@ -274,14 +229,14 @@ class Socket final : public fair::mq::Socket
} else if (zmq_errno() == EAGAIN || zmq_errno() == EINTR) {
if (fCtx.Interrupted()) {
return static_cast<int>(TransferCode::interrupted);
} else if (ShouldRetry(flags, timeout, elapsed)) {
} else if (zmq::ShouldRetry(flags, fTimeout, timeout, elapsed)) {
repeat = true;
break;
} else {
return static_cast<int>(TransferCode::timeout);
}
} else {
return HandleErrors();
return zmq::HandleErrors(fId);
}
size_t moreSize = sizeof(more);