Support application metadata through ZMQ_METADATA

Lets the application set per-connection metadata.
Metadata is specified as "X-key:value" and set using zmq_setsockopt, eg:
zmq_setsockopt (s, ZMQ_METADATA, "X-key:value", 11);

The peer can then obtain the metadata from a received message:
char *data = zmq_msg_gets(msg, "X-key");
This commit is contained in:
Pontus Sköldström
2018-03-12 01:41:33 +01:00
parent c9437ab755
commit dd5eec35be
9 changed files with 226 additions and 5 deletions

View File

@@ -706,6 +706,26 @@ int zmq::options_t::setsockopt (int option_,
return do_setsockopt_int_as_bool_relaxed (optval_, optvallen_,
&loopback_fastpath);
case ZMQ_METADATA:
if (optvallen_ > 0 && !is_int) {
std::string s ((char *) optval_);
size_t pos = 0;
std::string key, val, delimiter = ":";
pos = s.find (delimiter);
if (pos != std::string::npos && pos != 0
&& pos != s.length () - 1) {
key = s.substr (0, pos);
if (key.compare (0, 2, "X-") == 0 && key.length () < 256) {
val = s.substr (pos + 1, s.length ());
app_metadata.insert (
std::pair<std::string, std::string> (key, val));
return 0;
}
}
}
errno = EINVAL;
return -1;
break;
default:
#if defined(ZMQ_ACT_MILITANT)
// There are valid scenarios for probing with unknown socket option