gSOAP 源码分析(一)

http://blog.csdn.NET/flyfish1986/article/details/7593224

gSOAP 源码分析()

邵盛松 2012-5-22

一 SOAP消息结构

SOAP消息包括以下元素

必需的 Envelope 元素,可把此 XML 文档标识为一条 SOAP 消息,XML文件的顶层元素,代表该文件为SOAP消息

可选的 Header 元素,包含头部信息

必需的 Body 元素,包含所有的调用和响应信息

可选的 Fault 元素,提供有关在处理此消息所发生错误的信息

可选的Attachment,主要用于传递附件,扩展的SOAP消息

EnvelopeSOAP消息中的根节点,是SOAP消息中必需的部分;HeaderSOAP消息中可选部分,如果SOAP消息中含有它,那么它一定要是soap:Envelope>中的第一个元素节点;BodySOAP中必需部分,如果SOAP消息中没Header,那么Body必须是SOAP中第一个元素节点。

在默认生成的代码中关于Header 元素定义

/* SOAP Header: */

struct SOAP_ENV__Header

{

#ifdef WITH_NOEMPTYSTRUCT

private:

char dummy; /* dummy member to enable compilation */

#endif

};

下图为SOAP消息的结构

Fault结构

<faultcode>供识别故障的代码

<faultstring>这里的错误是为人设定的,让人读懂,而不是为程序处理设定的。

<faultactor>有关是谁引发故障的信息

<detail>Body元素中的内容不能被成功地处理的时候,它就出现了。

代码中fault结构体如下

struct SOAP_ENV__Fault

{

public:

char *faultcode; /* optional element of type xsd:QName */

char *faultstring; /* optional element of type xsd:string */

char *faultactor; /* optional element of type xsd:string */

struct SOAP_ENV__Detail *detail; /* optional element of type SOAP-ENV:Detail */

struct SOAP_ENV__Code *SOAP_ENV__Code; /* optional element of type SOAP-ENV:Code */

struct SOAP_ENV__Reason *SOAP_ENV__Reason; /* optional element of type SOAP-ENV:Reason */

char *SOAP_ENV__Node; /* optional element of type xsd:string */

char *SOAP_ENV__Role; /* optional element of type xsd:string */

struct SOAP_ENV__Detail *SOAP_ENV__Detail; /* optional element of type SOAP-ENV:Detail */

};

FaultBody的一个子元素主要用于用于报告错误。

SOAP命名空间

SOAP_NMAC struct Namespace namespaces[] =

{

{"SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/", "http://www.w3.org/*/soap-envelope", NULL},

{"SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/", "http://www.w3.org/*/soap-encoding", NULL},

{"xsi", "http://www.w3.org/2001/XMLSchema-instance", "http://www.w3.org/*/XMLSchema-instance", NULL},

{"xsd", "http://www.w3.org/2001/XMLSchema", "http://www.w3.org/*/XMLSchema", NULL},

{"ns", "urn:calc", NULL, NULL},

{NULL, NULL, NULL, NULL}

};

这里的URL并不是指向文件,而只是一个名字。如果一个SOAP应用程序接收了一个消息,而该消息的SOAP Envelope元素使用和上述不同的名称空间,则该应用程序就将其视为版本错误并忽略该消息。

二 gSOAP Keep-Alive和超时管理

gSOAP是绑定Http协议来对xml数据进行传输,一个SOAP请求实际上就是一个HTTP POST请求。

消息从发送方到接受方方是单向传送,即以请求/应答的方式实现的。这也就是为什么生成的xml文件都是req,res成对出现的

gSOAP运行环境实例是一个struct soap类型的变量,struct SOAP_STD_API soap

 gSOAP支持HTTP Keep-Alive,生成代码中默认没有使用Keep-Alive

calcService_init(SOAP_IO_DEFAULT, SOAP_IO_DEFAULT);

如果参数更改SOAP_IO_KEEPALIVE将使用Keep-Alive,默认最大连接100

以下代码在soap结构体中

  short tcp_keep_alive; /* enable SO_KEEPALIVE */

  unsigned int tcp_keep_idle;  /* set TCP_KEEPIDLE */

  unsigned int tcp_keep_intvl;  /* set TCP_KEEPINTVL */

  unsigned int tcp_keep_cnt;  /* set TCP_KEEPCNT */

  unsigned int max_keep_alive;  /* maximum keep-alive session (default=100) */

TCP_KEEPIDLE  空闲多久开始发送keepalive包

TCP_KEEPCNT 总共发送多少个

TCP_KEEPINTVL 每两个keepalive包的发送时间间隔

这种非阻塞管理需要设置超时时间

接收超时时间

发送超时时间

连接超时时间

接受请求超时时间

代码如下,这也是soap结构体的一部分

int recv_timeout;/* when > 0, gives socket recv timeout in seconds, < 0 in usec */

int send_timeout;/* when > 0, gives socket send timeout in seconds, < 0 in usec */

int connect_timeout;/* when > 0, gives socket connect() timeout in seconds, < 0 in usec */

int accept_timeout;/* when > 0, gives socket accept() timeout in seconds, < 0 in usec */

正值以秒为单位。负值以微秒为单位。

三 提高速度技巧

通过更改在stdsoap2.h文件中SOAP_BUFLEN宏,增加缓存大小

如果客户端需要多次连接相同的服务器,那么客户端使用HTTP keep-alive。服务端也要支持HTTP keep-alive,这样能够最大的增强服务端和客户端的性能。

使用HTTP分块传输

不要使用gzip压缩

gsoap-2.8源码,基于服务器客户端的实例,带自动生成服务客户端代码的批处理程序,及如何使用。带自己学习参考的教程; 0.解压附件,soapInterface.bat所在路径不得含中文 空格 1.新建头文件 取soapInterface.bat文件的同名:soapInterface.h 文件内编写接口,具体说明看附件参考的教程 //gsoap ns service name: gservice //gsoap ns service style: rpc int ns__add(int num1, int num2, int* result ); int ns__sub(int num1, int num2, int* result ); int ns__mult( int num1, int num2, int *result); int ns__divid( int num1, int num2, int *result); 2.从附件内gsoap-2.8包中搜索复制stdsoap2.h,stdsoap2.cpp,soapcpp2.exe, 存放于soapInterface.bat同级目录 3.双击soapInterface.bat运行。生成gClientSoap,gServerSoap两个文件夹,分别复制到服务器工程与客户端工程中使用 4.gClientSoap,gServerSoap两个文件夹内用到的文件功能说明,更多参考附件教程 1)soapC.cpp , soapH.h//soap的序列和反序列代码,它已经包含了soapStub.h 2)soapServer.c ppsoapServerLib.cpp //服务器端代码(纯C代码是soapServer.c soapServerLib.c ),soapServerLib.cpp文件则只是简单地包含soapServer.cpp和soapC.cpp 3)soapClient.cpp soapClientLib.cpp//客户端代码(纯C代码是soapClient.csoapClientLib.c ),soapClientLib.cpp文件则只是简单地包含soapClient.cpp和soapC.cpp 4) soapStub.h // soap的存根文件,定义了我们编写的头文件里对应的远程调用模型 5) add.nsmap //XML服务命名空间 6)服务器端要载入的文件有:soapServer.cpp,soapC.cpp,stdsoap2.cpp; 要包含的文件有:gservice.nsmap,soapH.h; 客户端要输入的文件有: soapClient.cpp,soapC.cpp,stdsoap2.cpp; 要包含的文件有:gservice.nsmap,soapH.h
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值