GSOAP source code analysis (1)
Yan shengsong
A soap message structure
SOAP messages include the following elements:
The required Envelope element. This XML document can be identified as a SOAP message. The top-level element of the XML file indicates that the file is a SOAP message.
Optional Header elements, including Header information
Required Body elements, including all call and response information
An optional Fault element that provides information about errors that occur when processing the message.
Optional Attachment, mainly used to transmit attachments and extended SOAP messages
Envelope is the root node of a SOAP message and is a required part of a SOAP message. The Header is an optional part of a SOAP message. If a SOAP message contains it, it must be soap: the first element node in Envelope>. The Body is a required part of SOAP. If there is no Header in the SOAP message, the Body must be the first element node in SOAP.
Define the Header element in the default generated code
/* SOAP Header :*/
Struct SOAP_ENV _ Header
{
# Ifdef WITH_NOEMPTYSTRUCT
Private:
Char dummy;/* dummy member to enable compilation */
# Endif
};
Structure of the SOAP message
Fault Structure
<Faultcode> code for Fault Identification
<Faultstring> the error here is set for people to understand, rather than set for program processing.
<Faultactor> information about who caused the fault
<Detail> when the content in the Body element cannot be processed successfully, it appears.
The fault struct in the Code is as follows:
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 */
};
Fault is a child element of the Body. It is mainly used to report errors.
SOAP namespace
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}
};
The URL here does not point to a file, but just a name. If a SOAP application receives a message and the SOAP Envelope element of the message uses a different namespace than the preceding one, the application regards it as a version error and ignores the message.
GSOAP Keep-Alive and timeout Management
GSOAP is bound to the Http protocol to transmit xml data. a soap request is actually an http post request.
A message is transmitted from the sender to the receiver in one way, that is, in the form of a request/response. This is why all generated xml files are req and res appear in pairs.
The gSOAP runtime environment instance is a struct soap-type variable, struct SOAP_STD_API soap
.
GSOAP supports HTTP Keep-Alive. By default, Keep-Alive is not used in the generated code.
CalcService_init (SOAP_IO_DEFAULT, SOAP_IO_DEFAULT );
If the parameter is changed, SOAP_IO_KEEPALIVE uses Keep-Alive, and the default maximum connection is 100.
The following code is used in the soap struct:
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 )*/
How long does TCP_KEEPIDLE start to send keepalive packets when it is idle?
Total number of TCP_KEEPCNT messages
The sending interval of each two keepalive packets in TCP_KEEPINTVL
For non-blocking management, you need to set the timeout time.
Receiving timeout
Sending timeout
Connection timeout
Request receiving timeout
The Code is as follows, which is part of the soap struct.
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 */
The positive value is in seconds. Negative values are measured in microseconds.
3. Speed improvement skills
1. Increase the cache size by changing the SOAP_BUFLEN macro in the stdsoap2.h File
2. If the client needs to connect to the same server multiple times, the client uses HTTP keep-alive. The server must also support HTTP keep-alive to maximize the performance of the server and client.
3. HTTP multipart Transmission
4. Do not use gzip Compression
From the column flyfish1986