gSOAP: Obtaining IP to the client when thread pool processing is implemented

Source: Internet
Author: User
Tags soap

Problem:

Ip! is not available to clients when processing client requests using the thread pool

Reason:

Since the Server_loop note loop only adds the connection word sock to the queue, there is no client IP, so each queue callback function can only get sock connection word, no client IP
Workaround:In the queue sock joins the IP also added to the IPs queue, the IPs queue length and queue, and the data stored in the same subscript, take sock processing also take the IP out, you can get the client IP
steps:Define a variable that holds the IP address globally [CPP]View Plaincopyprint?
    1. static unsigned long ips[max_queue];
    Static unsigned long ips[max_queue];

Modifying the Enqueue function [CPP]View Plaincopyprint?
  1. int enqueue (soap_socket sock,unsigned long IP)//Add IP parameter
  2. {
  3. int status = SOAP_OK;
  4. int Next;
  5. int ret;
  6. if (ret = Mutex_lock (queue_cs)))
  7. fprintf (stderr, "Mutex_lock error%d\n", ret);
  8. Next = (tail + 1)% Max_queue; //1000
  9. if (head = = next)
  10. {
  11. / * don ' t block on the full queue,
  12. * Queue is full, return Soap_eom * /
  13. status = Soap_eom;
  14. }
  15. Else
  16. {
  17. Queue[tail] = sock;
  18. Ips[tail] = IP; //Save IP
  19. tail = next;
  20. if (ret = cond_signal (QUEUE_CV)))
  21. fprintf (stderr, "cond_signal error%d\n", ret);
  22. }
  23. if (ret = Mutex_unlock (queue_cs)))
  24. fprintf (stderr, "Mutex_unlock error%d\n", ret);
  25. return status;
  26. }
int Enqueue (soap_socket sock,unsigned long IP)//Add IP parameter {        int status = SOAP_OK;        int next;        int ret;         if (ret = Mutex_lock (queue_cs)))                fprintf (stderr, "Mutex_lock error%d\n", ret);        Next = (tail + 1)% Max_queue;        if (head = = next)        {/                    * don ' t ' block on full queue,                 * Queue filled, return Soap_eom */                status = Soap_eom ;        }            else         {                    Queue[tail] = sock;                Ips[tail] = IP;  Save IP                tail = next;                if (ret = cond_signal (QUEUE_CV)))                        fprintf (stderr, "cond_signal error%d\n", ret);        }            if (ret = Mutex_unlock (queue_cs)))                fprintf (stderr, "Mutex_unlock error%d\n", ret);        return status;}


Add Dequeue_ip () function [CPP]View Plaincopyprint?
  1. Unsigned long dequeue_ip ()
  2. {
  3. Unsigned long IP;
  4. int num=0;
  5. if (head = = 0)
  6. num = max_queue-1;
  7. Else
  8. num = head-1;
  9. ip = ips[num];
  10. return IP;
  11. }
unsigned long  dequeue_ip () {        unsigned long IP;        int num=0;        if (head = = 0)            num = max_queue-1;        else             num = head-1;        ip = ips[num];        return IP;}



Modifying the queue callback function function [CPP]View Plaincopyprint?
  1. void *process_queue (void *soap)
  2. {
  3. struct Soap *tsoap = (struct soap*) soap;
  4. for (;;)
  5. {
  6. Tsoap->socket = Dequeue ();
  7. Tsoap->ip = Dequeue_ip (); //Get the appropriate IP address
  8. if (!soap_valid_socket (Tsoap->socket))
  9. {
  10. #ifdef DEBUG
  11. fprintf (stderr, "Thread%d terminating\n", (int) (long) tsoap->user);
  12. #endif
  13. break;
  14. }
void *process_queue (void *soap) {        struct soap *tsoap = (struct soap*) soap;        for (;;)        {                tsoap->socket = dequeue ();                Tsoap->ip = Dequeue_ip ();//get the corresponding IP address                if (!soap_valid_socket (tsoap->socket))                {#ifdef DEBUG                        fprintf (stderr, "Thread%d terminating\n", (int) (long) tsoap->user); #endif break                        ;                }


Solve!
Test:Testing in the Http_get_handler function [CPP]View Plaincopyprint?
</pre><div class= "Dp-highlighter bg_cpp" ><div class= "bar" ><div class= "Tools" ><strong >[cpp]</strong> <a target=_blank class= "Viewsource" title= "View Plain" href= "http://blog.csdn.net/ jk110333/article/details/9445761# ">view plain</a><a target=_blank class=" CopyToClipboard "title=" copy " href= "http://blog.csdn.net/jk110333/article/details/9445761#" >copy</a><a target=_blank class= " Printsource "title=" print "href=" http://blog.csdn.net/jk110333/article/details/9445761# ">print</a><a Target=_blank class= "About" title= "?" href= "http://blog.csdn.net/jk110333/article/details/9445761#" >?</a ></div></div><ol class= "Dp-cpp" ><li class= "alt" ><span class= "Datatypes" >int</ Span> http_get_handler (<span class= "keyword" >struct</span> soap *soap)    </li><li>{  </li><li class= "alt" >     .....   </li><li>     fprintf (Stderr, <span class= "string" > "Request  accepts connection from ip %d.%d.%d.%d\n "</span>,  </li><li class= "Alt" >            (<span class= "Datatypes" >int </span>) (soap->ip>>24) &0xFF,  (<span class= "Datatypes" >int</span>) (soap-> IP&GT;&GT;16) &0xff, (<span class= "Datatypes" >int</span>)   (soap->ip>>8) &0xff,   (<span class= "Datatypes" >int</span>) soap->ip&0xff);  </li><li> ...   </li><li class= "alt" >}  </li></ol></div><pre style= "     Display:none "class=" cpp "name=" code ">int http_get_handler (struct soap *soap) {... fprintf (stderr, "Request accepts connection from IP%d.%d.%d.%d\n", (int) (SOAP-&GT;IP&GT;&GT;24) &0xff, (int) (Soap-> ip>>16) &0xff, (int) (SOAP-&GT;IP&GT;&GT;8) &0xff, (int) soap->ip&0xff); }


Output: [Ruby]View Plaincopyprint?
    1. Request accepts connection from IP 192.168.1.136
    2. Request accepts connection from IP 192.168.1.136
    3. Thread 3 finished serving request with failure 404
    4. Error 404 Fault:soap-env:client [No subcode]
    5. "HTTP error:404 not Found"
    6. Detail: [No Detail]
    7. Request accepts connection from IP 192.168.1.87
    8. Request accepts connection from IP 192.168.1.87
    9. Request accepts connection from IP 192.168.1.87

gSOAP: Obtaining IP to the client when thread pool processing is implemented

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.