gSOAP多线程 [英] gSOAP Multithreading

查看:382
本文介绍了gSOAP多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iam试图构建一个多线程Web服务。单线程正在工作,在我的主要功能,我使用这:

iam trying to build an multihreading webservice. Single threading is working, in my main function i use this:

int main(int argc, char **argv) {
    CardSoapBindingService CardSrvc;
    Config Conf ;
    Conf.update();

    int port = Conf.listener_port;
    if (!port)
        CardSrvc.serve();
    else {
        if (CardSrvc.run(port)) {
            CardSrvc.soap_stream_fault(std::cerr);
            exit(-1);
        }
    }
    return 0;
}

但我想要多线程,所以我看了文档, href =http://www.cs.fsu.edu/~engelen/soapdoc2.html#tth_sEc7.2.4 =nofollow>示例,我尝试了我的代码。编译时得到这个错误:


main.cpp:在函数 int main(int,char **)':
main.cpp:56:错误:
soap_serve'undeclared(首先使用此函数)

main.cpp:56:error:(对于每个
函数,每个未声明的标识符仅报告一次in。)

main.cpp:在函数 void * process_request(void *)':< br>
main.cpp:101:错误:
soap_serve'undeclared(首先使用此函数)

make:*** [main.o] Fehler 1

But i want multithreading, so i looked in the documentation and found their example, which i tried instead my code. While compiling i get this errors:

main.cpp: In function int main(int, char**)': main.cpp:56: error:soap_serve' undeclared (first use this function)
main.cpp:56: error: (Each undeclared identifier is reported only once for each function it appears in.)
main.cpp: In function void* process_request(void*)':<br> main.cpp:101: error:soap_serve' undeclared (first use this function)
make: *** [main.o] Fehler 1

我如何获得这项工作?

How can i get this working?

推荐答案

重要提示

代码至少需要gsoap版本2.8.5。它最初构建在Solaris 8上,使用gsoap版本2.8.3,将代码移植到Ubuntu并在valgrind下运行,表明2.8.3 gsoap ++库损坏了导致SIGSEGV的内存。应该注意,从25/11/11的Ubuntu安装使用apt-get的gsoap的版本是破碎的2.8.3。需要手动下载并构建最新版本的gsoap(确保在配置gsoap build之前安装flex和bison!)。

This code requires gsoap version 2.8.5 as a minimum. It was initially built on Solaris 8 with gsoap version 2.8.3, porting the code to Ubuntu and running under valgrind showed that the 2.8.3 gsoap++ library was corrupting memory which lead to a SIGSEGV. It should be noted that as of 25/11/11 the version of gsoap that Ubuntu installs using apt-get is the broken 2.8.3. A manual download and build of the latest version of gsoap was required (make sure to install flex and bison before you configure the gsoap build!).

使用gsoap 2.8.5下面的代码快乐地创建线程并向多个客户端提供SOAP消息,valgrind现在报告内存分配的0个错误。

Using gsoap 2.8.5 the code below happily creates threads and serves SOAP messages to multiple clients, valgrind now reports 0 errors with the memory allocation.

看看你的代码,你工作的例子是用-i(或-j)选项创建的,以创建C ++对象。 gsoap中的线程示例用标准C编写;因此引用了你没有的soap_serve()这样的函数。

Looking at your code the example you have working has been built with the -i (or -j) option to create C++ objects. The thread examples in the gsoap doumention are written in standard C; hence the reference to functions such as soap_serve() which you don't have.

下面是我的多线程示例的快速重写,以使用生成的C +对象。它基于以下定义文件:

Below is my quick re-write of the multithreaded example to use the C+ objects generated. It is based on the following definition file:

// Content of file "calc.h": 
//gsoap ns service name: Calculator 
//gsoap ns service style: rpc 
//gsoap ns service encoding: encoded 
//gsoap ns service location: http://www.cs.fsu.edu/~engelen/calc.cgi 
//gsoap ns schema namespace: urn:calc 
//gsoap ns service method-action: add "" 
int ns__add(double a, double b, double &result); 
int ns__sub(double a, double b, double &result); 
int ns__mul(double a, double b, double &result); 
int ns__div(double a, double b, double &result);

主服务器代码如下:

#include "soapCalculatorService.h"  // get server object 
#include "Calculator.nsmap"     // get namespace bindings

#include <pthread.h>

void *process_request(void *calc) ;

int main(int argc, char* argv[]) 
{ 
    CalculatorService c;
    int port = atoi(argv[1]) ;
    printf("Starting to listen on port %d\n", port) ;
    if (soap_valid_socket(c.bind(NULL, port, 100)))
    {
        CalculatorService *tc ;
        pthread_t tid; 
        for (;;)
        {
            if (!soap_valid_socket(c.accept()))
                return c.error;
            tc = c.copy() ; // make a safe copy 
            if (tc == NULL) 
                break; 
            pthread_create(&tid, NULL, (void*(*)(void*))process_request, (void*)tc); 

            printf("Created a new thread %ld\n", tid) ;
        }
    }
    else {
        return c.error;
    }

} 


void *process_request(void *calc) 
{ 
   pthread_detach(pthread_self());
   CalculatorService *c = static_cast<CalculatorService*>(calc) ;
   c->serve() ;
   c->destroy() ;
   delete c ;
   return NULL; 
}  

这是一个非常基本的线程模型,但它展示了如何使用C ++类由gsoap生成以构建多线程服务器。

This is a very basic threading model but it shows how to use the C++ classes generated by gsoap to build a multithreaded server.

这篇关于gSOAP多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆