Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed whitespace and indentation

  • Loading branch information...
commit 6239343d155286820de9f82939483f7bcc91505c 1 parent ce57220
@hintjens hintjens authored
Showing with 59 additions and 50 deletions.
  1. +59 −50 examples/C++/lpclient.cpp
View
109 examples/C++/lpclient.cpp
@@ -7,64 +7,73 @@
#define REQUEST_TIMEOUT 2500 // msecs, (> 1000!)
#define REQUEST_RETRIES 3 // Before we abandon
+
// Helper function that returns a new configured socket
// connected to the Hello World server
//
-static zmq::socket_t * s_client_socket(zmq::context_t & context) {
- printf("I: connecting to server...\n");
- zmq::socket_t * client = new zmq::socket_t(context, ZMQ_REQ);
- client->connect("tcp://localhost:5555");
+static zmq::socket_t * s_client_socket (zmq::context_t & context) {
+ printf ("I: connecting to server...\n");
+ zmq::socket_t * client = new zmq::socket_t (context, ZMQ_REQ);
+ client->connect ("tcp://localhost:5555");
- // Configure socket to not wait at close time
- int linger = 0;
- client->setsockopt(ZMQ_LINGER, &linger, sizeof(linger));
- return client;
+ // Configure socket to not wait at close time
+ int linger = 0;
+ client->setsockopt (ZMQ_LINGER, &linger, sizeof (linger));
+ return client;
}
-int main() {
- zmq::context_t context(1);
+int main () {
+ zmq::context_t context (1);
+
+ zmq::socket_t * client = s_client_socket (context);
+
+ printf ("I: connecting to server...\n");
+
+ int sequence = 0;
+ int retries_left = REQUEST_RETRIES;
- zmq::socket_t * client = s_client_socket(context);
+ while (retries_left) {
+ // We send a request, then we work to get a reply
+ char request [10];
+ sprintf (request, "%d", ++sequence);
+ s_send (*client, request);
+ sleep (1);
- printf("I: connecting to server...\n");
+ int expect_reply = 1;
+ while (expect_reply) {
+ // Poll socket for a reply, with timeout
+ zmq::pollitem_t items[] = { { *client, 0, ZMQ_POLLIN, 0 } };
+ zmq::poll (&items[0], 1, REQUEST_TIMEOUT * 1000);
- int sequence = 0;
- int retries_left = REQUEST_RETRIES;
- while (retries_left) {
- // We send a request, then we work to get a reply
- char request[10];
- sprintf(request, "%d", ++sequence);
- s_send(*client, request);
- sleep(1);
- int expect_reply = 1;
- while (expect_reply) {
- // Poll socket for a reply, with timeout
- zmq::pollitem_t items[] = { { *client, 0, ZMQ_POLLIN, 0 } };
- zmq::poll(&items[0], 1, REQUEST_TIMEOUT * 1000);
- // If we got a reply, process it
- if (items[0].revents & ZMQ_POLLIN) {
- // We got a reply from the server, must match sequence
- std::string *reply = s_recv(*client);
- if (atoi(reply->c_str()) == sequence) {
- printf("I: server replied OK (%s)\n", reply->c_str());
- retries_left = REQUEST_RETRIES;
- expect_reply = 0;
- } else
- printf("E: malformed reply from server: %s\n", reply->c_str());
+ // If we got a reply, process it
+ if (items[0].revents & ZMQ_POLLIN) {
+ // We got a reply from the server, must match sequence
+ std::string *reply = s_recv (*client);
+ if (atoi (reply->c_str ()) == sequence) {
+ printf ("I: server replied OK (%s)\n", reply->c_str ());
+ retries_left = REQUEST_RETRIES;
+ expect_reply = 0;
+ }
+ else
+ printf ("E: malformed reply from server: %s\n",
+ reply->c_str ());
- delete reply;
- } else if (--retries_left == 0) {
- printf("E: server seems to be offline, abandoning\n");
- break;
- } else {
- printf("W: no response from server, retrying...\n");
- // Old socket will be confused; close it and open a new one
- delete client;
- client = s_client_socket(context);
- // Send request again, on new socket
- s_send(*client, request);
- }
- }
- }
- return 0;
+ delete reply;
+ }
+ else
+ if (--retries_left == 0) {
+ printf ("E: server seems to be offline, abandoning\n");
+ break;
+ }
+ else {
+ printf ("W: no response from server, retrying...\n");
+ // Old socket will be confused; close it and open a new one
+ delete client;
+ client = s_client_socket (context);
+ // Send request again, on new socket
+ s_send (*client, request);
+ }
+ }
+ }
+ return 0;
}
Please sign in to comment.
Something went wrong with that request. Please try again.