0

I have a piece of code that uses websocketpp to run a server. I would like to identify the different connections that come to the server. To do so it seems that the websocketpp::connection_hdl hdl should be used.

namespace websocketpp {

/// A handle to uniquely identify a connection.
/**
 * This type uniquely identifies a connection. It is implemented as a weak
 * pointer to the connection in question. This provides uniqueness across
 * multiple endpoints and ensures that IDs never conflict or run out.
 *
 * It is safe to make copies of this handle, store those copies in containers,
 * and use them from other threads.
 *
 * This handle can be upgraded to a full shared_ptr using
 * `endpoint::get_con_from_hdl()` from within a handler fired by the connection
 * that owns the handler.
 */
typedef lib::weak_ptr<void> connection_hdl;

But as you can see it is a weak_ptr<void> that I don't know how it can be compared with others.

I have a map with websocketpp::connection_hdl as index that when I try to see if has an existent index with:

std::map<websocketpp::connection_hdl, asio::ip::tcp::socket> active_connections;
if (active_connections.count(con->get_socket()) > 0) {}

The compiler complains with:

error C2678: binary '<': no operator found which takes a left-hand operand of type 'const _Ty' (or there is no acceptable conversion)

Is there any way I can get the socket from the connection (the raw integer socket). If I can I could use that as an index and solve the problem.

Can you see any other way to fix it?

user1618465
  • 1,813
  • 2
  • 32
  • 58

1 Answers1

0

There are two problems:

  1. You are using a weak_ptr as a key without using std::owner_less. More information: How can I use a std::map with std::weak_ptr as key?
  2. In your example, you are using a socket as a key instead of a connection_hdl.

Solution:

std::map<websocketpp::connection_hdl, boost::asio::ip::tcp::socket, std::owner_less<websocketpp::connection_hdl>> active_connections;
if (active_connections.count(con) > 0) {}

However, the map doesn't make that much sense: If you have the connection_hdl, you can get the socket of the connection with the get_socket() method. I have never used this method, but I think it should work? If you just want to store all open connections and their sockets, a std::vector containing the connection handles is probably better.

Lho
  • 289
  • 1
  • 9