Unverified Commit 0a037a74 authored by Luca Boccassi's avatar Luca Boccassi Committed by GitHub

Merge pull request #3097 from sigiesec/ping-context

ZMTP 3.1 PING Context not implemented
parents df2fe88b be66eacf
......@@ -651,7 +651,8 @@ tests_test_setsockopt_SOURCES = tests/test_setsockopt.cpp
tests_test_setsockopt_LDADD = src/libzmq.la
tests_test_heartbeats_SOURCES = tests/test_heartbeats.cpp
tests_test_heartbeats_LDADD = src/libzmq.la
tests_test_heartbeats_LDADD = src/libzmq.la ${UNITY_LIBS}
tests_test_heartbeats_CPPFLAGS = ${UNITY_CPPFLAGS}
tests_test_stream_exceeds_buffer_SOURCES = tests/test_stream_exceeds_buffer.cpp
tests_test_stream_exceeds_buffer_LDADD = src/libzmq.la
......
......@@ -98,6 +98,8 @@ zmq::stream_engine_t::stream_engine_t (fd_t fd_,
{
int rc = tx_msg.init ();
errno_assert (rc == 0);
rc = pong_msg.init ();
errno_assert (rc == 0);
// Put the socket into non-blocking mode.
unblock_socket (s);
......@@ -935,9 +937,7 @@ int zmq::stream_engine_t::decode_and_push (msg_t *msg_)
}
if (msg_->flags () & msg_t::command) {
uint8_t cmd_id = *((uint8_t *) msg_->data ());
if (cmd_id == 4)
process_heartbeat_message (msg_);
process_command_message (msg_);
}
if (metadata)
......@@ -1061,11 +1061,8 @@ int zmq::stream_engine_t::produce_pong_message (msg_t *msg_)
int rc = 0;
zmq_assert (mechanism != NULL);
rc = msg_->init_size (5);
rc = msg_->move (pong_msg);
errno_assert (rc == 0);
msg_->set_flags (msg_t::command);
memcpy (msg_->data (), "\4PONG", 5);
rc = mechanism->encode (msg_);
next_msg = &stream_engine_t::pull_and_encode;
......@@ -1088,9 +1085,39 @@ int zmq::stream_engine_t::process_heartbeat_message (msg_t *msg_)
has_ttl_timer = true;
}
// As per ZMTP 3.1 the PING command might contain an up to 16 bytes
// context which needs to be PONGed back, so build the pong message
// here and store it. Truncate it if it's too long.
// Given the engine goes straight to out_event, sequential PINGs will
// not be a problem.
size_t context_len = msg_->size () - 7 > 16 ? 16 : msg_->size () - 7;
int rc = pong_msg.init_size (5 + context_len);
errno_assert (rc == 0);
pong_msg.set_flags (msg_t::command);
memcpy (pong_msg.data (), "\4PONG", 5);
if (context_len > 0)
memcpy (((uint8_t *) pong_msg.data ()) + 5,
((uint8_t *) msg_->data ()) + 7, context_len);
next_msg = &stream_engine_t::produce_pong_message;
out_event ();
}
return 0;
}
int zmq::stream_engine_t::process_command_message (msg_t *msg_)
{
uint8_t cmd_name_size = *((uint8_t *) msg_->data ());
// Malformed command
if (msg_->size () < cmd_name_size + sizeof (cmd_name_size))
return -1;
uint8_t *cmd_name = ((uint8_t *) msg_->data ()) + 1;
if (cmd_name_size == 4
&& (memcmp (cmd_name, "PING", cmd_name_size) == 0
|| memcmp (cmd_name, "PONG", cmd_name_size) == 0))
return process_heartbeat_message (msg_);
return 0;
}
......@@ -127,6 +127,7 @@ class stream_engine_t : public io_object_t, public i_engine
typedef metadata_t::dict_t properties_t;
bool init_properties (properties_t &properties);
int process_command_message (msg_t *msg_);
int produce_ping_message (msg_t *msg_);
int process_heartbeat_message (msg_t *msg_);
int produce_pong_message (msg_t *msg_);
......@@ -138,6 +139,8 @@ class stream_engine_t : public io_object_t, public i_engine
bool as_server;
msg_t tx_msg;
// Need to store PING payload for PONG
msg_t pong_msg;
handle_t handle;
......
This diff is collapsed.
......@@ -57,12 +57,37 @@ int test_assert_success_message_errno_helper (int rc,
return rc;
}
int test_assert_success_message_raw_errno_helper (int rc,
const char *msg,
const char *expr)
{
if (rc == -1) {
#if defined ZMQ_HAVE_WINDOWS
int current_errno = WSAGetLastError ();
#else
int current_errno = errno;
#endif
char buffer[512];
buffer[sizeof (buffer) - 1] =
0; // to ensure defined behavior with VC++ <= 2013
snprintf (buffer, sizeof (buffer) - 1, "%s failed%s%s%s, errno = %i",
expr, msg ? " (additional info: " : "", msg ? msg : "",
msg ? ")" : "", current_errno);
TEST_FAIL_MESSAGE (buffer);
}
return rc;
}
#define TEST_ASSERT_SUCCESS_MESSAGE_ERRNO(expr, msg) \
test_assert_success_message_errno_helper (expr, msg, #expr)
#define TEST_ASSERT_SUCCESS_ERRNO(expr) \
test_assert_success_message_errno_helper (expr, NULL, #expr)
#define TEST_ASSERT_SUCCESS_RAW_ERRNO(expr) \
test_assert_success_message_raw_errno_helper (expr, NULL, #expr)
#define TEST_ASSERT_FAILURE_ERRNO(error_code, expr) \
{ \
int rc = (expr); \
......
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