gSOAP开发两部曲

来源:互联网 发布:手机看盘软件下载 编辑:程序博客网 时间:2024/05/17 20:28

一、导入和解析wsdl强大利器---wsdl2h

wsdl2h 根据webservice生成 .h文件, 从WSDL中产生头文件, 是C/c++语言的WSDL/schema 导入和解析工具。

  • 只负责生成头文件,且此头文件不能直接使用,必须在经过soapcpp2转换后才能使用。
  • 输入为一个或多个wsdl或xsd文件,或URL;
  • 如果输入为wsld或xsd文件,默认输出为第一个文件名,后缀为.h。
  • 如果输入为URL,则默认输出为标准输出。

     

    用法:

    wsdl2h -o 头文件名 WSDL文件名或URL 

    wsdl2h常用选项

    • OptionDescription-agenerate indexed struct names for local elements with anonymous types-bbi-directional operations to serve one-way response messages (duplex)-cgenerate C source code-duse DOM to populate xs:any and xsd:anyType elements-edon't qualify enum names This option is for backward compatibility with gSOAP 2.4.1 and earlier. The option does not produce code that conforms to WS-I Basic Profile 1.0a.-f     generate flat C++ class hierarchy for schema extensions-ggenerate global top-level element declarations-h     print help information-I pathuse path to locate source files for #import-i     don't import (advanced option)-jdon't generate SOAP_ENV__Header and SOAP_ENV__Detail definitions-kdon't generate SOAP_ENV__Header mustUnderstand qualifiers-linclude license information in output-muse xsd.h module to import primitive types-N nameuse name for service prefixes to produce a service for each binding-n nameuse name as the base namespace prefix name instead of ns-o file output to file-P     don't create polymorphic types inherited from xsd__anyType-p     create polymorphic types inherited from base xsd__anyType This is automatically performed when WSDL contains polymorphic definitions-q nameuse name for the C++ namespace of all declarations-r host[:port[:uid:pwd]]     connect via proxy host, port, and proxy credentials-R     generate REST operations for REST bindings in the WSDL-s     don't generate STL code (no std::string and no std::vector)-t file use type map file instead of the default file typemap.dat-udon't generate unions-v     verbose output-Wsuppress warnings-w     always wrap response parameters in a response struct-xdon't generate _XML any/anyAttribute extensibility elements-ygenerate typedef synonyms for structs and enums-z1    compatibility with 2.7.6e: generate pointer-based arrays-z2compatibility with 2.7.15: qualify element/attribute referenced members-z3    compatibility with 2.7.16 to 2.8.7: qualify element/attribute references-z4    compatibility up to 2.8.11: don't generate union structs in std::vector-z5    compatibility up to 2.8.15-_don't generate _USCORE (replace with UNICODE _x005f)

    typemap文件用于指定SOAP/XML中的类型与C/C++之间的转换规则。我将在后续的文章里专门分析它。现在而言,你只需记住,它是很重要的。

    二 C/C++代码自动产生的利器----soapcpp2

    用法

    soapcpp2 头文件 
    soapcpp2常用选项
    OptionDescription-1Generate SOAP 1.1 bindings-2Generate SOAP 1.2 bindings-0Remove SOAP bindings, use plain REST-CGenerate client-side code only-SGenerate server-side code only-TGenerate server auto-test code-LDo not generate soapClientLib/soapServerLib-aUse SOAPAction with WS-Addressing to invoke server-side operations-ARequire SOAPAction to invoke server-side operations-bserialize byte arrays char[N] as string-cGenerate pure C code-d < path > Save sources in directory specified by < path > -eGenerate SOAP RPC encoding style bindings-f NFile split of N XML serializer implementations per file-hPrint a brief usage message-iGenerate service proxies and objects inherited from soap struct-jGenerate C++ service proxies and objects that can share a soap struct-I < path > Use < path > for #import (paths separated with ':' or ';' for windows)-kgenerate data structure walkers (experimental)-lGenerate linkable modules (experimental)-mGenerate Matlabtm code for MEX compiler-nWhen used with -p, enables multi-client and multi-server builds: Sets compiler option WITH_NONAMESPACES, see Section 9.11 Saves the namespace mapping table with name < name > _namespaces instead of namespaces Renames soap_serve() into < name > _serve() and soap_destroy() into < name > _destroy()-p < name > Save sources with file name prefix < name > instead of "soap"-q < name > Use name for the C++ namespace of all declarations-sGenerates deserialization code with strict XML validation checks-tGenerates code to send typed messages (with the xsi:type attribute)-uuncomment comments in WSDL/schema output by suppressing XML comments-vDisplay version info-wDo not generate WSDL and schema files-xDo not generate sample XML message files-yinclude C/C++ type access information in sample XML messages-z1generate deprecated old-style C++ service proxies and objects

    例:

    soapcpp2 ayandy.h

    将生成下面这些文件

    • soapStub.h    // soap的存根文件,定义了ayandy.h里对应的远程调用模型
    • soapC.c soapH.h  // soap的序列和反序列代码,它已经包含了soapStub.h,服务器端与客户端都要包含它
    • soapClient.c soapClientLib.c // 客户端代码,soapClientLib.c文件则只是简单地包含soapClient.c和soapC.c
    • soapServer.c soapServerLib.c // 服务器端代码,soapServerLib.c文件则只是简单地包含soapServer.c和soapC.c
    • ServiceSoap.nsmap ServiceSoap12.nsmap // 名空间定义,服务器端与客户端都要包含它
    • soapServiceSoapProxy.h soapServiceSoap12Proxy.h // 客户端的C++简单包装(如果头文件是纯C代码,这两个文件就不会生成)

    综上所述

    • 如果编写服务器端,项目里应该加入soapServerLib.c,代码里包含头文件soapH.h
    • 如果编写客户端,项目里应该加入soapClientLib.c,代码里包含头文件SoapH.h(或xxxxProxy.h)
    • 当然,还要加入gsoap库里的stdsoap2.cpp文件(如果是写C代码,则加入stdsoap2.c)

    如果看到soapcpp2提示:”Critical error: #import: Cannot open file "stlvector.h" for reading.“, 那是因为我们的头文件使用了STL(wsdl2h 没用-s选项),这时要使用-I选项指定gSOAP的 import文件路径,这个路径是"$gsoap\gsoap\import":

    soapcpp2 ayandy.h -I D:\gsoap-2.7\gsoap\import

     

     

  • 原创粉丝点击