XMLRPC的C++代码在下载后的ros_comm-noetic-develutilitiesxmlrpcpp路径下。
还好,整个工程不算太大。XMLRPC分成客户端和服务器端两大部分。
咱们先看客户端,主要代码在XmlRpcClient.cpp文件里。
擒贼先擒王,XmlRpcClient.cpp文件中最核心的函数就是execute,用于执行远程调用,代码如下。
// Execute the named procedure on the remote server.
// Params should be an array of the arguments for the method.
// Returns true if the request was sent and a result received (although the result might be a fault).
bool XmlRpcClient::execute(const char* method, XmlRpcValue const& params, XmlRpcValue& result)
{
XmlRpcUtil::log(1, "XmlRpcClient::execute: method %s (_connectionState %s).", method, connectionStateStr(_connectionState));
// This is not a thread-safe operation, if you want to do multithreading, use separate
// clients for each thread. If you want to protect yourself from multiple threads
// accessing the same client, replace this code with a real mutex.
if (_executing)
return false;
_executing = true;
ClearFlagOnExit cf(_executing);
_sendAttempts = 0;
_isFault = false;
if ( ! setupConnection())
return false;
if ( ! generateRequest(method, params))
return false;
result.clear();
double msTime = -1.0; // Process until exit is called
_disp.work(msTime);
if (_connectionState != IDLE || ! parseResponse(result)) {
_header = "";
return false;
}
// close() if server does not supports HTTP1.1
// otherwise, reusing the socket to write leads to a SIGPIPE because
// the remote server could shut down the corresponding socket.
if (_header.find("HTTP/1.1 200 OK", 0, 15) != 0) {
close();
}
XmlRpcUtil::log(1, "XmlRpcClient::execute: method %s completed.", method);
_header = "";
_response = "";
return true;
}
它首先调用setupConnection()函数与服务器端建立连接。
连接成功后,调用generateRequest()函数生成发送请求报文。
XMLRPC请求报文的头部又交给generateHeader()函数做了,代码如下。
// Prepend http headers
std::string XmlRpcClient::generateHeader(size_t length) const
{
std::string header =
"POST " + _uri + " HTTP/1.1rn"
"User-Agent: ";
header += XMLRPC_VERSION;
header += "rnHost: ";
header += _host;
char buff[40];
std::snprintf(buff,40,":%drn", _port);
header += buff;
header += "Content-Type: text/xmlrnContent-length: ";
std::snprintf(buff,40,"%zurnrn", length);
return header + buff;
}
主体部分则先将远程调用的方法和参数变成XML格式,generateRequest()函数再将头部和主体组合成完整的报文,如下:
std::string header = generateHeader(body.length());
_request = header + body;
把报文发给服务器后,就开始静静地等待。
一旦接收到服务器返回的报文后,就调用parseResponse函数解析报文数据,也就是把XML格式变成纯净的数据格式。
我们发现,XMLRPC使用了socket功能实现客户端和服务器通信。
我们搜索socket这个单词,发现它原始的意思是插座。这非常形象,建立连接实现通信就像把插头插入插座。
虽说XMLRPC也是ROS的一部分,但它毕竟只是一个基础功能,我们会用即可,暂时不去探究其实现细节,
全部0条评论
快来发表一下你的评论吧 !