Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions buttonrpc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class buttonrpc
std::map<std::string, std::function<void(Serializer*, const char*, int)>> m_handlers;

zmq::context_t m_context;
zmq::socket_t* m_socket;
std::unique_ptr<zmq::socket_t, std::function<void(zmq::socket_t*)>> m_socket;

rpc_err_code m_error_code;

Expand All @@ -225,16 +225,14 @@ inline buttonrpc::buttonrpc() : m_context(1){
}

inline buttonrpc::~buttonrpc(){
m_socket->close();
delete m_socket;
m_context.close();
}

// network
inline void buttonrpc::as_client( std::string ip, int port )
{
m_role = RPC_CLIENT;
m_socket = new zmq::socket_t(m_context, ZMQ_REQ);
m_socket = std::unique_ptr<zmq::socket_t, std::function<void(zmq::socket_t*)>>(new zmq::socket_t(m_context, ZMQ_REQ), [](zmq::socket_t* sock){ sock->close(); delete sock; sock =nullptr;});
ostringstream os;
os << "tcp://" << ip << ":" << port;
m_socket->connect (os.str());
Expand All @@ -243,7 +241,7 @@ inline void buttonrpc::as_client( std::string ip, int port )
inline void buttonrpc::as_server( int port )
{
m_role = RPC_SERVER;
m_socket = new zmq::socket_t(m_context, ZMQ_REP);
m_socket = std::unique_ptr<zmq::socket_t, std::function<void(zmq::socket_t*)>>(new zmq::socket_t(m_context, ZMQ_REP), [](zmq::socket_t* sock){ sock->close(); delete sock; sock =nullptr;});
ostringstream os;
os << "tcp://*:" << port;
m_socket->bind (os.str());
Expand Down