Merge pull request #714 from hintjens/master

Cleaned up the code
This commit is contained in:
Richard Newton 2013-10-21 01:58:00 -07:00
commit 41b2f83bef

View File

@ -76,8 +76,15 @@ int zmq::proxy (
{ control_, 0, ZMQ_POLLIN, 0 }
};
int qt_poll_items = (control_ ? 3 : 2);
enum {suspend, resume, terminate} state = resume;
while (state != terminate) {
// Proxy can be in these three states
enum {
active,
paused,
terminated
} state = active;
while (state != terminated) {
// Wait while there are either requests or replies to process.
rc = zmq_poll (&items [0], qt_poll_items, -1);
if (unlikely (rc < 0))
@ -107,24 +114,23 @@ int zmq::proxy (
if (unlikely (rc < 0))
return -1;
}
// process control command
int size = msg.size();
char* message = (char*) malloc(size + 1);
memcpy(message, msg.data(), size);
message[size] = '\0';
if (size == 8 && !memcmp(message, "SUSPEND", 8))
state = suspend;
else if (size == 7 && !memcmp(message, "RESUME", 7))
state = resume;
else if (size == 10 && !memcmp(message, "TERMINATE", 10))
state = terminate;
if (msg.size () == 6 && memcmp (msg.data (), "PAUSE", 6) == 0)
state = paused;
else
fprintf(stderr, "Warning : \"%s\" bad command received by proxy\n", message); // prefered compared to "return -1"
free (message);
if (msg.size () == 7 && memcmp (msg.data (), "RESUME", 7) == 0)
state = active;
else
if (msg.size () == 10 && memcmp (msg.data (), "TERMINATE", 10) == 0)
state = terminated;
else {
// This is an API error, we should assert
puts ("E: invalid command sent to proxy");
assert (false);
}
}
// Process a request
if (state == resume && items [0].revents & ZMQ_POLLIN) {
if (state == active
&& items [0].revents & ZMQ_POLLIN) {
while (true) {
rc = frontend_->recv (&msg, 0);
if (unlikely (rc < 0))
@ -156,7 +162,8 @@ int zmq::proxy (
}
}
// Process a reply
if (state == resume && items [1].revents & ZMQ_POLLIN) {
if (state == active
&& items [1].revents & ZMQ_POLLIN) {
while (true) {
rc = backend_->recv (&msg, 0);
if (unlikely (rc < 0))
@ -187,7 +194,6 @@ int zmq::proxy (
break;
}
}
}
return 0;
}