Advertisement
Guest User

nanomsg REP/REQ example with threads client code

a guest
Jun 17th, 2014
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.06 KB | None | 0 0
  1. #define AUTH_SOCKET_ADDRESS "ipc://oauth2"
  2. #define AUTH_SUCCESS        0
  3. #define AUTH_FAILED         1
  4. #define AUTH_SOCK_FAILED    2
  5. #define AUTH_BIND_DAILED    3
  6. #define AUTH_SOME_OTHER     4
  7.  
  8. struct core_buf {
  9.     u_int8_t        *data;
  10.     u_int64_t       length;
  11.     u_int64_t       offset;
  12. };
  13.  
  14. struct http_request {
  15.     u_int8_t            method;
  16.     u_int8_t            flags;
  17.     int             status;
  18.     u_int64_t           start;
  19.     u_int64_t           end;
  20.     u_int64_t           total;
  21.     char                *query_string;
  22. };
  23.  
  24. /*  This is part of webserver code..see only nanomsg use case..for clarity I have provided
  25.     the  (part of)struct http_request definition too. */
  26.  
  27. /*  A web client send a request to my webserver, req holds the web client request. 
  28.     This method is trying to send the request query string to REP end-point...and the response
  29.     from the REP end-point will send to web client  as the response to the web client request. */
  30. int send_authentication_request(struct http_request* req)
  31. {
  32.     int sock = nn_socket (AF_SP, NN_REQ);
  33.    
  34.     if(sock >= 0)
  35.     {
  36.         core_debug("send_authentication_request: sock created");
  37.         if(nn_connect (sock, AUTH_SOCKET_ADDRESS) >= 0)
  38.         {
  39.             char *resp = NULL;
  40.             int bytes = 0;
  41.             int result;
  42.             core_debug("'%s' connected with %s",AUTH_SOCKET_ADDRESS, req->query_string);
  43.            
  44.             result = nn_send (sock, req->query_string , strlen(req->query_string), 0);
  45.            
  46.             core_debug("nn_send result: %d",result);    /* *****    This is successful */
  47.            
  48.     //  Here its waiting for REP end-point reply, which its not getting...
  49.             bytes = nn_recv (sock, &resp, NN_MSG, 0);   /* *****    Waiting for response */
  50.            
  51.             core_debug("bytes received: %d",bytes);
  52.            
  53.             if(bytes > 0)
  54.             {
  55.                 //  send received data to the web client here...
  56.                
  57.                 nn_freemsg (resp);
  58.             }
  59.         }
  60.         else
  61.             return AUTH_BIND_FAILED;
  62.     }
  63.     else
  64.         return AUTH_SOCK_FAILED;
  65.    
  66.     return AUTH_SUCCESS;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement