gSOAP开发两部曲

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

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

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

     

    用法:

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

    wsdl2h常用选项

    • Option Description
      -a generate indexed struct names for local elements with anonymous types
      -b bi-directional operations to serve one-way response messages (duplex)
      -c generate C source code
      -d use DOM to populate xs:any and xsd:anyType elements
      -e don'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
      -g generate global top-level element declarations
      -h      print help information
      -I path use path to locate source files for #import
      -i      don't import (advanced option)
      -j don't generate SOAP_ENV__Header and SOAP_ENV__Detail definitions
      -k don't generate SOAP_ENV__Header mustUnderstand qualifiers
      -l include license information in output
      -m use xsd.h module to import primitive types
      -N name use name for service prefixes to produce a service for each binding
      -n name use 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 name use 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
      -u don't generate unions
      -v      verbose output
      -W suppress warnings
      -w      always wrap response parameters in a response struct
      -x don't generate _XML any/anyAttribute extensibility elements
      -y generate typedef synonyms for structs and enums
      -z1     compatibility with 2.7.6e: generate pointer-based arrays
      -z2 compatibility 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常用选项
       
       
       
       
    Option Description
    -1 Generate SOAP 1.1 bindings
    -2 Generate SOAP 1.2 bindings
    -0 Remove SOAP bindings, use plain REST
    -C Generate client-side code only
    -S Generate server-side code only
    -T Generate server auto-test code
    -L Do not generate soapClientLib/soapServerLib
    -a Use SOAPAction with WS-Addressing to invoke server-side operations
    -A Require SOAPAction to invoke server-side operations
    -b serialize byte arrays char[N] as string
    -c Generate pure C code
    -d < path > Save sources in directory specified by < path >
    -e Generate SOAP RPC encoding style bindings
    -f N File split of N XML serializer implementations per file
    -h Print a brief usage message
    -i Generate service proxies and objects inherited from soap struct
    -j Generate C++ service proxies and objects that can share a soap struct
    -I < path > Use < path > for #import (paths separated with ':' or ';' for windows)
    -k generate data structure walkers (experimental)
    -l Generate linkable modules (experimental)
    -m Generate Matlabtm code for MEX compiler
    -n When 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
    -s Generates deserialization code with strict XML validation checks
    -t Generates code to send typed messages (with the xsi:type attribute)
    -u uncomment comments in WSDL/schema output by suppressing XML comments
    -v Display version info
    -w Do not generate WSDL and schema files
    -x Do not generate sample XML message files
    -y include C/C++ type access information in sample XML messages
    -z1 generate 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

     

     

  • 你可能感兴趣的:(gSOAP开发两部曲)