Unverified Commit 2d186486 authored by kladko's avatar kladko

bug/SKALE-3751-enable-zeromq

parent 6545d811
......@@ -22,6 +22,9 @@
*/
#include "sys/random.h"
#include <sys/types.h>
#include <sys/syscall.h>
#include "common.h"
#include "BLSSignReqMessage.h"
......@@ -62,14 +65,25 @@ shared_ptr <ZMQMessage> ZMQClient::doRequestReply(Json::Value &_req) {
throw;
}
}
string ZMQClient::doZmqRequestReply(string &_req) {
stringstream request;
if (!clientSocket)
reconnect();
shared_ptr <zmq::socket_t> clientSocket = nullptr;
{
lock_guard <recursive_mutex> m(mutex);
if (!clientSockets.count(getProcessID()))
reconnect();
clientSocket = clientSockets.at(getProcessID());
CHECK_STATE(clientSocket);
}
CHECK_STATE(clientSocket);
spdlog::debug("ZMQ client sending: \n {}" , _req);
......@@ -108,18 +122,24 @@ ZMQClient::ZMQClient(string &ip, uint16_t port) : ctx(1) {
void ZMQClient::reconnect() {
getrandom(identity, 10, 0);
lock_guard<recursive_mutex> lock(mutex);
auto pid = getProcessID();
if (clientSockets.count(pid) > 0) {
clientSockets.erase(pid);
}
clientSocket = nullptr; // delete previous
clientSocket = make_unique<zmq::socket_t>(ctx, ZMQ_DEALER);
char identity[10];
getrandom(identity, 10, 0);
auto clientSocket = make_shared<zmq::socket_t>(ctx, ZMQ_DEALER);
clientSocket->setsockopt(ZMQ_IDENTITY, identity, 10);
// Configure socket to not wait at close time
int linger = 0;
clientSocket->setsockopt(ZMQ_LINGER, &linger, sizeof(linger));
clientSocket->connect(url);
clientSockets.insert({pid, clientSocket});
}
......@@ -147,4 +167,10 @@ string ZMQClient::ecdsaSignMessageHash(int base, const std::string &keyName, con
CHECK_STATE(result);
CHECK_STATE(result->getStatus() == 0);
return result->getSignature();
}
uint64_t ZMQClient::getProcessID() {
return syscall(__NR_gettid);
}
\ No newline at end of file
......@@ -44,23 +44,29 @@ class ZMQClient {
private:
recursive_mutex mutex;
zmq::context_t ctx;
std::unique_ptr <zmq::socket_t> clientSocket;
string url;
// generate random identity
char identity[10] = {};
map<uint64_t , shared_ptr <zmq::socket_t>> clientSockets;
shared_ptr <ZMQMessage> doRequestReply(Json::Value &_req);
string doZmqRequestReply(string &_req);
uint64_t getProcessID();
public:
ZMQClient(string &ip, uint16_t port);
void reconnect() ;
string blsSignMessageHash(const std::string &keyShareName, const std::string &messageHash, int t, int n);
......
......@@ -47,7 +47,7 @@ public:
ZMQServer();
enum {
kMaxThread = 16
kMaxThread = 1
};
void run();
......
This diff is collapsed.
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