Commit 0a43c66a authored by Simon Giesecke's avatar Simon Giesecke

Problem: magic literals in zap_client.cpp

Solution: extracted constants
parent 7c2d1c18
...@@ -35,6 +35,12 @@ ...@@ -35,6 +35,12 @@
namespace zmq namespace zmq
{ {
const char zap_version[] = "1.0";
const size_t zap_version_len = sizeof (zap_version) - 1;
const char id[] = "1";
const size_t id_len = sizeof (id) - 1;
zap_client_t::zap_client_t (session_base_t *const session_, zap_client_t::zap_client_t (session_base_t *const session_,
const std::string &peer_address_, const std::string &peer_address_,
const options_t &options_) : const options_t &options_) :
...@@ -72,17 +78,17 @@ void zap_client_t::send_zap_request (const char *mechanism_, ...@@ -72,17 +78,17 @@ void zap_client_t::send_zap_request (const char *mechanism_,
errno_assert (rc == 0); errno_assert (rc == 0);
// Version frame // Version frame
rc = msg.init_size (3); rc = msg.init_size (zap_version_len);
errno_assert (rc == 0); errno_assert (rc == 0);
memcpy (msg.data (), "1.0", 3); memcpy (msg.data (), zap_version, zap_version_len);
msg.set_flags (msg_t::more); msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg); rc = session->write_zap_msg (&msg);
errno_assert (rc == 0); errno_assert (rc == 0);
// Request ID frame // Request ID frame
rc = msg.init_size (1); rc = msg.init_size (id_len);
errno_assert (rc == 0); errno_assert (rc == 0);
memcpy (msg.data (), "1", 1); memcpy (msg.data (), id, id_len);
msg.set_flags (msg_t::more); msg.set_flags (msg_t::more);
rc = session->write_zap_msg (&msg); rc = session->write_zap_msg (&msg);
errno_assert (rc == 0); errno_assert (rc == 0);
...@@ -136,15 +142,16 @@ void zap_client_t::send_zap_request (const char *mechanism_, ...@@ -136,15 +142,16 @@ void zap_client_t::send_zap_request (const char *mechanism_,
int zap_client_t::receive_and_process_zap_reply () int zap_client_t::receive_and_process_zap_reply ()
{ {
int rc = 0; int rc = 0;
msg_t msg[7]; // ZAP reply consists of 7 frames const size_t zap_reply_frame_count = 7;
msg_t msg[zap_reply_frame_count];
// Initialize all reply frames // Initialize all reply frames
for (int i = 0; i < 7; i++) { for (size_t i = 0; i < zap_reply_frame_count; i++) {
rc = msg[i].init (); rc = msg[i].init ();
errno_assert (rc == 0); errno_assert (rc == 0);
} }
for (int i = 0; i < 7; i++) { for (size_t i = 0; i < zap_reply_frame_count; i++) {
rc = session->read_zap_msg (&msg[i]); rc = session->read_zap_msg (&msg[i]);
if (rc == -1) { if (rc == -1) {
if (errno == EAGAIN) { if (errno == EAGAIN) {
...@@ -152,7 +159,8 @@ int zap_client_t::receive_and_process_zap_reply () ...@@ -152,7 +159,8 @@ int zap_client_t::receive_and_process_zap_reply ()
} }
return close_and_return (msg, -1); return close_and_return (msg, -1);
} }
if ((msg[i].flags () & msg_t::more) == (i < 6 ? 0 : msg_t::more)) { if ((msg[i].flags () & msg_t::more)
== (i < zap_reply_frame_count - 1 ? 0 : msg_t::more)) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZAP_MALFORMED_REPLY); session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZAP_MALFORMED_REPLY);
errno = EPROTO; errno = EPROTO;
...@@ -170,7 +178,8 @@ int zap_client_t::receive_and_process_zap_reply () ...@@ -170,7 +178,8 @@ int zap_client_t::receive_and_process_zap_reply ()
} }
// Version frame // Version frame
if (msg[1].size () != 3 || memcmp (msg[1].data (), "1.0", 3)) { if (msg[1].size () != zap_version_len
|| memcmp (msg[1].data (), zap_version, zap_version_len)) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZAP_BAD_VERSION); session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZAP_BAD_VERSION);
errno = EPROTO; errno = EPROTO;
...@@ -178,7 +187,7 @@ int zap_client_t::receive_and_process_zap_reply () ...@@ -178,7 +187,7 @@ int zap_client_t::receive_and_process_zap_reply ()
} }
// Request id frame // Request id frame
if (msg[2].size () != 1 || memcmp (msg[2].data (), "1", 1)) { if (msg[2].size () != id_len || memcmp (msg[2].data (), id, id_len)) {
session->get_socket ()->event_handshake_failed_protocol ( session->get_socket ()->event_handshake_failed_protocol (
session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZAP_BAD_REQUEST_ID); session->get_endpoint (), ZMQ_PROTOCOL_ERROR_ZAP_BAD_REQUEST_ID);
errno = EPROTO; errno = EPROTO;
...@@ -214,7 +223,7 @@ int zap_client_t::receive_and_process_zap_reply () ...@@ -214,7 +223,7 @@ int zap_client_t::receive_and_process_zap_reply ()
} }
// Close all reply frames // Close all reply frames
for (int i = 0; i < 7; i++) { for (size_t i = 0; i < zap_reply_frame_count; i++) {
const int rc2 = msg[i].close (); const int rc2 = msg[i].close ();
errno_assert (rc2 == 0); errno_assert (rc2 == 0);
} }
......
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