mirror of
https://github.com/zeromq/cppzmq.git
synced 2025-10-18 03:29:50 +02:00
Merge pull request #187 from cowo78/master
std::string conversion for zmq::message_t
This commit is contained in:
86
zmq.hpp
86
zmq.hpp
@@ -27,13 +27,13 @@
|
|||||||
#define __ZMQ_HPP_INCLUDED__
|
#define __ZMQ_HPP_INCLUDED__
|
||||||
|
|
||||||
#if (__cplusplus >= 201103L)
|
#if (__cplusplus >= 201103L)
|
||||||
#define ZMQ_CPP11
|
#define ZMQ_CPP11
|
||||||
#define ZMQ_NOTHROW noexcept
|
#define ZMQ_NOTHROW noexcept
|
||||||
#define ZMQ_EXPLICIT explicit
|
#define ZMQ_EXPLICIT explicit
|
||||||
#elif (defined(_MSC_VER) && (_MSC_VER >= 1900))
|
#elif (defined(_MSC_VER) && (_MSC_VER >= 1900))
|
||||||
#define ZMQ_CPP11
|
#define ZMQ_CPP11
|
||||||
#define ZMQ_NOTHROW noexcept
|
#define ZMQ_NOTHROW noexcept
|
||||||
#define ZMQ_EXPLICIT explicit
|
#define ZMQ_EXPLICIT explicit
|
||||||
#else
|
#else
|
||||||
#define ZMQ_CPP03
|
#define ZMQ_CPP03
|
||||||
#define ZMQ_NOTHROW
|
#define ZMQ_NOTHROW
|
||||||
@@ -42,18 +42,22 @@
|
|||||||
|
|
||||||
#include <zmq.h>
|
#include <zmq.h>
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <string>
|
|
||||||
|
#include <algorithm>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <vector>
|
#include <iomanip>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
|
||||||
#ifdef ZMQ_CPP11
|
#ifdef ZMQ_CPP11
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Detect whether the compiler supports C++11 rvalue references.
|
// Detect whether the compiler supports C++11 rvalue references.
|
||||||
@@ -83,11 +87,11 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3, 3, 0)
|
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(3, 3, 0)
|
||||||
#define ZMQ_NEW_MONITOR_EVENT_LAYOUT
|
#define ZMQ_NEW_MONITOR_EVENT_LAYOUT
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 1, 0)
|
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 1, 0)
|
||||||
#define ZMQ_HAS_PROXY_STEERABLE
|
#define ZMQ_HAS_PROXY_STEERABLE
|
||||||
/* Socket event data */
|
/* Socket event data */
|
||||||
typedef struct {
|
typedef struct {
|
||||||
uint16_t event; // id of the event as bitfield
|
uint16_t event; // id of the event as bitfield
|
||||||
@@ -378,6 +382,42 @@ namespace zmq
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
/** Dump content to string. Ascii chars are readable, the rest is printed as hex.
|
||||||
|
* Probably ridiculously slow.
|
||||||
|
*/
|
||||||
|
inline std::string str() const
|
||||||
|
{
|
||||||
|
// Partly mutuated from the same method in zmq::multipart_t
|
||||||
|
std::stringstream os;
|
||||||
|
|
||||||
|
const unsigned char* msg_data = this->data<unsigned char>();
|
||||||
|
unsigned char byte;
|
||||||
|
size_t size = this->size();
|
||||||
|
int is_ascii[2] = {0, 0};
|
||||||
|
|
||||||
|
os << "zmq::message_t [size " << std::dec << std::setw(3) << std::setfill('0') << size << "] (";
|
||||||
|
// Totally arbitrary
|
||||||
|
if (size >= 1000) {
|
||||||
|
os << "... too big to print)";
|
||||||
|
} else {
|
||||||
|
while (size--) {
|
||||||
|
byte = *msg_data++;
|
||||||
|
|
||||||
|
is_ascii[1] = (byte >= 33 && byte < 127);
|
||||||
|
if (is_ascii[1] != is_ascii[0])
|
||||||
|
os << " "; // Separate text/non text
|
||||||
|
|
||||||
|
if (is_ascii[1]) {
|
||||||
|
os << byte;
|
||||||
|
} else {
|
||||||
|
os << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << static_cast<short>(byte);
|
||||||
|
}
|
||||||
|
is_ascii[0] = is_ascii[1];
|
||||||
|
}
|
||||||
|
os << ")";
|
||||||
|
}
|
||||||
|
return os.str();
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// The underlying message
|
// The underlying message
|
||||||
@@ -927,7 +967,7 @@ namespace zmq
|
|||||||
#elif ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 1)
|
#elif ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 1)
|
||||||
virtual void on_event_handshake_failed(const zmq_event_t &event_, const char* addr_) { (void) event_; (void) addr_; }
|
virtual void on_event_handshake_failed(const zmq_event_t &event_, const char* addr_) { (void) event_; (void) addr_; }
|
||||||
virtual void on_event_handshake_succeed(const zmq_event_t &event_, const char* addr_) { (void) event_; (void) addr_; }
|
virtual void on_event_handshake_succeed(const zmq_event_t &event_, const char* addr_) { (void) event_; (void) addr_; }
|
||||||
#endif
|
#endif
|
||||||
virtual void on_event_unknown(const zmq_event_t &event_, const char* addr_) { (void)event_; (void)addr_; }
|
virtual void on_event_unknown(const zmq_event_t &event_, const char* addr_) { (void)event_; (void)addr_; }
|
||||||
private:
|
private:
|
||||||
|
|
||||||
@@ -984,9 +1024,9 @@ namespace zmq
|
|||||||
|
|
||||||
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3)
|
#if ZMQ_VERSION >= ZMQ_MAKE_VERSION(4, 2, 3)
|
||||||
if (zmq_errno () == EAGAIN)
|
if (zmq_errno () == EAGAIN)
|
||||||
#else
|
#else
|
||||||
if (zmq_errno () == ETIMEDOUT)
|
if (zmq_errno () == ETIMEDOUT)
|
||||||
#endif
|
#endif
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
throw error_t ();
|
throw error_t ();
|
||||||
@@ -995,9 +1035,15 @@ namespace zmq
|
|||||||
private:
|
private:
|
||||||
void *poller_ptr;
|
void *poller_ptr;
|
||||||
std::vector<zmq_poller_event_t> poller_events;
|
std::vector<zmq_poller_event_t> poller_events;
|
||||||
};
|
}; // class poller_t
|
||||||
#endif // defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER)
|
#endif // defined(ZMQ_BUILD_DRAFT_API) && defined(ZMQ_CPP11) && defined(ZMQ_HAVE_POLLER)
|
||||||
|
|
||||||
|
|
||||||
|
inline std::ostream& operator<<(std::ostream& os, const message_t& msg)
|
||||||
|
{
|
||||||
|
return os << msg.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
} // namespace zmq
|
||||||
|
|
||||||
|
#endif // __ZMQ_HPP_INCLUDED__
|
||||||
|
@@ -25,6 +25,7 @@
|
|||||||
#define __ZMQ_ADDON_HPP_INCLUDED__
|
#define __ZMQ_ADDON_HPP_INCLUDED__
|
||||||
|
|
||||||
#include <zmq.hpp>
|
#include <zmq.hpp>
|
||||||
|
|
||||||
#include <deque>
|
#include <deque>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
@@ -328,7 +329,7 @@ public:
|
|||||||
{
|
{
|
||||||
return &m_parts[index];
|
return &m_parts[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get a string copy of a specific message part
|
// Get a string copy of a specific message part
|
||||||
std::string peekstr(size_t index) const
|
std::string peekstr(size_t index) const
|
||||||
{
|
{
|
||||||
@@ -582,10 +583,15 @@ private:
|
|||||||
// Disable implicit copying (moving is more efficient)
|
// Disable implicit copying (moving is more efficient)
|
||||||
multipart_t(const multipart_t& other) ZMQ_DELETED_FUNCTION;
|
multipart_t(const multipart_t& other) ZMQ_DELETED_FUNCTION;
|
||||||
void operator=(const multipart_t& other) ZMQ_DELETED_FUNCTION;
|
void operator=(const multipart_t& other) ZMQ_DELETED_FUNCTION;
|
||||||
};
|
}; // class multipart_t
|
||||||
|
|
||||||
#endif
|
#endif // ZMQ_HAS_RVALUE_REFS
|
||||||
|
|
||||||
|
inline std::ostream& operator<<(std::ostream& os, const multipart_t& msg)
|
||||||
|
{
|
||||||
|
return os << msg.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
} // namespace zmq
|
||||||
|
|
||||||
|
#endif // __ZMQ_ADDON_HPP_INCLUDED__
|
||||||
|
Reference in New Issue
Block a user