gosta100

zmq_fileclient.c

Nov 18th, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.14 KB | None | 0 0
  1. #include <czmq.h>
  2. #define CHUNK_SIZE 4096
  3. #define PIPELINE 10
  4.  
  5. int main (void)
  6. {
  7.   zctx_t *ctx = zctx_new ();
  8.  
  9.   void *dealer = zsocket_new (ctx, ZMQ_DEALER);
  10.   zsocket_set_hwm (dealer, PIPELINE);
  11.   zsocket_connect (dealer, "tcp://127.0.0.1:6000");
  12.  
  13.   // We'll allow up to five chunks in transit at once
  14.   size_t credit = PIPELINE;
  15.  
  16.   size_t total = 0; // Total bytes received
  17.   size_t chunks = 0; // Total chunks received
  18.   size_t offset = 0; // Offset of next chunk request
  19.  
  20.   while (true) {
  21.     while (credit) {
  22.       // Ask for next chunk
  23.       zstr_sendfm (dealer, "fetch");
  24.       zstr_sendfm (dealer, "%ld", offset);
  25.       zstr_sendf (dealer, "%ld", CHUNK_SIZE);
  26.       offset += CHUNK_SIZE;
  27.       credit--;
  28.     }
  29.    
  30.     zframe_t *chunk = zframe_recv (dealer);
  31.     if (!chunk)
  32.       break; // Shutting down, quit
  33.    
  34.     chunks++;
  35.     credit++;
  36.    
  37.     size_t size = zframe_size (chunk);
  38.     zframe_destroy (&chunk);
  39.     total += size;
  40.     if (size < CHUNK_SIZE)
  41.       break; // Last chunk received; exit
  42.   }
  43.   printf ("%zd chunks received, %zd bytes\n", chunks, total);
  44.  
  45.   zctx_destroy (&ctx);
  46.   return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment