gsoap2.8编写C/C++的服务端

最近一个项目需要运行在ppc平台(嵌入式的一个非常小众的操作系统)上,发现没有匹配的jdk,所以tomcat启动不了,之前用java写的webservice就不能用了,需要用C/C++写一个soap服务端。

因为有现成的wsdl,就不用自己写.h了,直接用soap工具生成,命令网上可以找到。

1、实现:

用-S生成soapServer.cpp、soapStub.h等文件,放到工程目录里,在soapStub.h文件中有根据wsdl生成的接口定义,在注释有Server-Side Operations下面,这些接口需要你新写一个cpp文件来实现,里面写你自己的业务逻辑

/******************************************************************************\
 *                                                                            *
 * Server-Side Operations                                                     *
 *                                                                            *
\******************************************************************************/


SOAP_FMAC5 int SOAP_FMAC6 __ns1__onlineDurQuery(struct soap*, ns1__onlineDurQuery *ns1__onlineDurQuery_, ns1__onlineDurQueryResponse *ns1__onlineDurQueryResponse_);

//下面还有一大堆
#include "CityServiceSoapBinding.nsmap"
#include "soapH.h"


int __ns1__onlineDurQuery(struct soap* server, ns1__onlineDurQuery *ns1__onlineDurQuery_, ns1__onlineDurQueryResponse *ns1__onlineDurQueryResponse_)
{
	Msg msg;
	Msg recvMsg;

	std::string input;
	input = *(ns1__onlineDurQuery_->input);

    //这里发送消息,接受消息,或自己的处理逻辑

	std::string ret((char *)recvMsg.GetContent(), recvMsg.Length);
	ns1__onlineDurQueryResponse_->return_ = soap_new_std__string(server);
	*(ns1__onlineDurQueryResponse_->return_) = ret;

	return 0;
}

2、问题:

        对接的client比较操蛋,同时用了axis2和cvf,自测程序时发现axis2发送POST命令,程序能处理,cvf发送GET处理不了,抓包发现GET里面的url只到wsdl,没有方法也没有参数(数据),懵逼了好长时间,windows断点调试,linux下gdb调试,发现都不行,后来又细看了下抓包,发现卧槽,cvf是先发GET获取wsdl,再发POST发起业务。

        这样只要在服务端实现能够返回wsdl就可以了(获取wsdl要实现get的处理)。下面贴一段返回wsdl的代码段

	soap_init(&soap);
	soap_set_mode(&soap, SOAP_C_UTFSTRING);

	soap.fget = my_http_get_handler;//这里是把自己的http_get处理函数赋值给soap,这样soap接受到get请求就会走my_http_get_handler
int my_http_get_handler(struct soap *soap)
{
	std::string soapPath = soap->path;//soap->path为xxxx/ws/abcService?wsdl
	std::string token = "/";
	std::vector<std::string> soapPathVec;
	if (0 != split(soapPath, token, soapPathVec) || soapPathVec.empty())
	{
		return 404;
	}
	LogDebug("soapPathVec.size=[%d]", soapPathVec.size());
	for (size_t i = 0; i < soapPathVec.size(); i++)
	{
		LogDebug("path=[%s]", soapPathVec[i].c_str());
	}
	
	soapPath = soapPathVec[soapPathVec.size() - 1];
	token = "?";
	std::vector<std::string> cityServiceVec;
	if (0 != split(soapPath, token, cityServiceVec) || cityServiceVec.empty())
	{
		return 404;
	}

    //cityServiceVec[0]为从soap->path里获取的服务名,就是上面注释的abcService
	std::string path = "/opt/local/bin/VOS/cur/" + cityServiceVec[0] + ".wsdl";

	FILE * fd = NULL;
	fd = fopen(path.c_str(), "rb");//这里要保证wsdl文件放到相应目录下
	if (!fd)
	{
		return 404;
	}

	soap->http_content = " text/xml ";
	soap_response(soap, SOAP_FILE);
	for (;;)
	{
		size_t r = fread(soap->tmpbuf, 1, sizeof (soap->tmpbuf), fd);
		if (!r)
		{
			break;
		}
		if (soap_send_raw(soap, soap->tmpbuf, r))
		{
			break;
		}
	}

	fclose(fd);
	soap_end_send(soap);
	return SOAP_OK;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值