Advertisement
Guest User

Untitled

a guest
Jul 18th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.27 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "server_protocol.pb-c.h"
  4. #define MAX_MSG_SIZE 1024 // 1K
  5.  
  6. //
  7.  
  8. int main(int argc, char *argv[]) {
  9.  
  10.     ServerProtocol msg = SERVER__PROTOCOL__INIT;
  11.     void * buf;
  12.     unsigned len;
  13.  
  14.     msg.command = "AUTH";
  15.     msg.username = "Dashed";
  16.     msg.password = "rofl23";
  17.  
  18.     // This is calculated packing length
  19.     len = server__protocol__get_packed_size(&msg);
  20.  
  21.     // Allocated memory for packed/encode serialized data
  22.     buf = malloc(len);
  23.  
  24.     server__protocol__pack(&msg,buf); // Put it in buf now
  25.  
  26.     fprintf(stderr,"Writing %d serialized bytes\n",len); // See the length of message
  27.  
  28.     printf("Sent: ");
  29.     // Write to stdout to allow direct command line piping
  30.     //fwrite(buf,len,1,stdout);
  31.     printf("\n");
  32.  
  33.     // Free the allocated serialized buffer
  34.     //free(buf);        
  35. ////////////////////////////////////
  36.  
  37.     ServerProtocol *msg2;
  38.     //uint8_t buf2[MAX_MSG_SIZE];  // Create a temporary buffer
  39.     msg2 = server__protocol__unpack(NULL,len,buf); // Deserialize the serialized data
  40.    
  41.  
  42.     printf("Received: command=%s username=%s password=%s table=%s key=%s value=%s\n",msg2->command, msg2->username, msg2->password, msg2->table, msg2->key, msg2->value);
  43.    
  44.  
  45.     return 0;
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement