{以下是码云平台说明,您可以替换此简介 码云是 OSCHINA 推出的基于 Git 的代码托管平台(同时支持 SVN)。专为开发者提供稳定、高效、安全的云端软件开发协作平台 无论是个人、团队、或是企业,都能够用码云实现代码托管、项目管理、协作开发。企业项目请看 https://gitee.com/enterprises}
软件架构说明
Alternatives
This section describes some alternatives to using XML-RPC For C/C++.
Other Progamming Languages
There are plenty of facilities to help you create XML-RPC clients and servers in a language other than C or C++. Search on Freshmeat.
It is worth mentioning that with some of these other-language facilities, the client or server is way slower than with XML-RPC for C/C++, because of the nature of the language. For example, I have used the Perl RPC::XML modules from CPAN and found a client program that takes 50 milliseconds to run when written with XML-RPC For C And C++ takes 2000 milliseconds to run when done with RPC::XML::Client.
In the case of Perl, there is a middle ground. The RPC::Xmlrpc_c modules from CPAN are based on Perl extensions that use the libraries of XML-RPC For C And C++. One reason RPC::XML is so slow is that it is built on top of a stack about 6 layers high, each one implemented in interpreted Perl. With RPC::Xmlrpc_c, all those layers except the top are implemented as executable machine code, efficiently compiled from C, so you have the same ease of Perl coding, without the slowness.
RPC::Xmlrpc_c is much younger than RPC::XML, so doesn't have many features, and in fact does not include any server facilities. But you could add missing features yourself (and, ideally, submit them for inclusion in the RPC::Xmlrpc_c package on CPAN, so others can use them).
RPC::Xmlrpc_c was new in December 2006 and needs XML-RPC For C And C++ Release 1.08 or better.
In other interpreted languages, the same hybrid may be possible -- replacing slow interpreted code with executable XML-RPC libraries.
Apache Module
You can make a nice XML-RPC server based on an Apache HTTP server (which may or may not simultaneously be a regular web server) using an Apache module.
There once was an Apache module based on Xmlrpc-c, so you could use the same method code as you do for other Xmlrpc-c-based implementations. It was called mod_xmlrpc and is described by a Freshmeat entry, but as of April 2009, the download link is dead.
Another module, also called mod_xmlrpc, is distributed via a Sourceforge project, but hasn't been updated since 2001, is undocumented, and looks pretty weak.
An even simpler, though less efficient and more limited way to make an XML-RPC server out of an Apache server is to do it via a CGI script. That script can be written in a variety of languages, but if you write it in C, you can use Xmlrpc-c's libxmlrpc_server_cgi library.
Other RPC Protocols
SOAP and CORBA are common alternatives to XML-RPC. Lots of expensive commercial software helps you use those. There is more to know and more you can do with them.
REST-RPC was invented in 2006 and was meant to be superior to XML-RPC for at least some things. It is easier to use in many ways than XML-RPC. Like XML-RPC, it uses HTTP, and like XML-RPC an RPC's result is XML. But unlike XML-RPC, a call is not XML. It is encoded entirely in the query part of a URL. (Example: http:/test.rest-rpc.org/?_function=GetCart&cart=1563). In the result, there are no inherent data types; server and client agree on those separately. The Xins project has more information.
JSON-RPC is like XML-RPC using JSON instead of XML, but isn't really an RPC protocol at all. It is a protocol for sending arbitrary messages back and forth between two communicants (whereas in an RPC protocol, the messages must have a request/response relationship). JSON is a way of representing typical program data structures such as integers and arrays in text, and is much simpler than XML-RPC XML elements or indeed any XML. It is far easier for a person to read a JSON-RPC message than to read an XML-RPC message. JSON-RPC was developed after XML-RPC.
Appendices
Introductory Examples
Here, to show you what Xmlrpc-c is, we present example code (almost an entire C program) for a simple XML-RPC client that exploits the Xmlrpc-c libraries, and a corresponding simple XML-RPC server.
There is not enough here for you just to copy and paste it and build the program, because there is more than Xmlrpc-c details to building a program.
The Xmlrpc-c website has a page of full examples for each of these programs, and we provide links to the relevant files along with the examples below.
But even that leaves some work for you to do, figuring out how to compile it with your tools and for your environment.
You can find complete working versions of these, with a working build system, and lots of other examples in the examples/ directory in the Xmlrpc-c source tree. You will probably have an easier time getting started with Xmlrpc-c starting from those examples than by cutting and pasting the code shown below.
In these examples, the service to be provided is adding of two numbers. You wouldn't do this with RPC in real life, of course, because a program can add two numbers without the help of a remote server. This is just to demonstrate the concept.
Table Of Contents
Small C Client Example
Small C Server Example
CGI C Server Example
Small C++ Server Example
Small C++ Client Example
Small C Client Example
Here is an example of C code that implements an XML-RPC client using the highest level facilities of Xmlrpc-c. This client sends a request to add 5 and 7 together to an XMLRPC-C server that is designed to provide the service of adding numbers.
int main(int const argc,
const char ** const argv) {
xmlrpc_env env;
xmlrpc_value * resultP;
int sum;
char * const clientName = "XML-RPC C Test Client";
char * const clientVersion = "1.0";
char * const url = "http://localhost:8080/RPC2";
char * const methodName = "sample.add";
/* Initialize our error-handling environment. */
xmlrpc_env_init(&env);
/* Start up our XML-RPC client library. */
xmlrpc_client_init2(&env, XMLRPC_CLIENT_NO_FLAGS, clientName, clientVersion, NULL, 0);
[ handle possible failure of above ]
/* Make the remote procedure call */
resultP = xmlrpc_client_call(&env, url, methodName,
"(ii)", (xmlrpc_int32) 5, (xmlrpc_int32) 7);
[ handle possible failure of above ]
/* Print out the sum the server returned */
xmlrpc_parse_value(&env, resultP, "i", &sum);
[ handle possible failure of above ]
printf("The sum is %d\n", sum);
/* Dispose of our result value. */
xmlrpc_DECREF(resultP);
/* Clean up our error-handling environment. */
xmlrpc_env_clean(&env);
/* Shutdown our XML-RPC client library. */
xmlrpc_client_cleanup();
return 0;
}
See the complete source file in the Xmlrpc-c source tree.
Small C Server Example
Now, here is code that implements an XML-RPC server that provides the number-adding service from the previous section.
#include <xmlrpc.h>
#include <xmlrpc_server.h>
#include <xmlrpc_server_abyss.h>
static xmlrpc_value *sample_add(xmlrpc_env * const envP, xmlrpc_value * const paramArrayP, void * const serverContext) {
xmlrpc_int32 x, y, z;
/* Parse our argument array. */
xmlrpc_parse_value(envP, paramArrayP, "(ii)", &x, &y);
if (envP->fault_occurred)
return NULL;
/* Add our two numbers. */
z = x + y;
/* Return our result. */
return xmlrpc_build_value(envP, "i", z);
}
int main (int const argc, const char ** const argv) {
xmlrpc_server_abyss_parms serverparm;
xmlrpc_registry * registryP;
xmlrpc_env env;
xmlrpc_env_init(&env);
registryP = xmlrpc_registry_new(&env);
xmlrpc_registry_add_method(&env, registryP, NULL, "sample.add", &sample_add, NULL);
serverparm.config_file_name = argv[1];
serverparm.registryP = registryP;
printf("Starting XML-RPC server...\n");
xmlrpc_server_abyss(&env, &serverparm, XMLRPC_APSIZE(registryP));
return 0;
}
See the complete source file in the Xmlrpc-c source tree.
There's a lot going on under the covers of this example server. What the xmlrpc_server_abyss() statement does is run a whole HTTP server. The function doesn't normally return. The HTTP server runs the abyss web server (i.e. HTTP server) program. abyss is like the more serious web server program apache, but on a much smaller scale. An XML-RPC call is just an HTTP POST request, so while abyss was not designed specifically for XML-RPC, it provides much of the function an XML-RPC server needs.
The only way this Abyss web server differs from one you would run to do traditional web serving is that it contains a special handler to call Xmlrpc-c functions to handle an XML-RPC POST request. The server calls that handler for any URI that starts with "/RPC2", which is what XML-RPC URIs conventionally have.
While abyss is distributed independently of Xmlrpc-c, Xmlrpc-c contains an old copy of it, somewhat modified. So you don't need to install abyss separately.
In this example, you have to provide an example abyss configuration file as a program argument. The main thing you need that file for is to specify on which TCP port the server will listen. A single "Port 8080" statement is probably enough. (I say 8080, because in the example client code above, I hardcoded 8080 as the port in the URI the client uses).
There are lots of other ways to use Xmlrpc-c libraries to build XML-RPC clients and servers. The more code you're willing to write, and the more involved in the guts of the protocol you want to get, the more control you can have.
CGI Server Example
#include <xmlrpc.h>
#include <xmlrpc_server_cgi.h>
static xmlrpc_value *sample_add(xmlrpc_env * const env, xmlrpc_value * const param_array, void * const user_data) {
xmlrpc_int32 x, y, z;
/* Parse our argument array. */
xmlrpc_decompose_value(env, param_array, "(ii)", &x, &y);
if (env->fault_occurred)
return NULL;
/* Add our two numbers. */
z = x + y;
/* Return our result. */
return xmlrpc_int_new(env, z);
}
int main(int const argc, const char ** const argv) {
/* Process our request. */
xmlrpc_cgi_init(XMLRPC_CGI_NO_FLAGS);
xmlrpc_cgi_add_method_w_doc("sample.add", &sample_add, NULL,
"i:ii", "Add two integers.");
xmlrpc_cgi_process_call();
xmlrpc_cgi_cleanup();
return 0;
}
See the complete source file in the Xmlrpc-c source tree.
About This Document
This document is part of the XML-RPC For C/C++ project. It is the main user documentation for the project.
The master copy of this document lives at http://xmlrpc-c.sourceforge.net/doc/. The HTML copy there is the original source -- it is hand edited.
This is a living document. It gets updated continuously, both to document changes in Xmlrpc-c and to improve the documentation. It documents all current and past, and, where possible, future releases of Xmlrpc-c. There is no benefit to keeping an old copy of the document to use with an old copy of the code.
Bryan Henderson wrote and published the first draft of this document in November 2004, as an entirely original work. Bryan placed it in the public domain.
Bryan enthusiastically maintains the document. If you have a problem with it, from typos to missing topics, please email Bryan at bryanh@giraffe-data.com
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。