gSoap传递结构体数组之上网不易

gSoap传递结构体数组之上网不易

每次需要用到网络,无论PC,Mac或是移动设备,一阵愤怒涌上来。Google全线被封,Dropbox被封,Line被封,Amazon WS 被封,One Drive 被封,手机上百分之八十应用挂了……就在墙内死去吧!F**k GFW!!顺便祝福Fang Sir。如今的封锁已经到了丧心病狂的地步,当局没有任何下线和尺度!我的这个日记本也快挂了。不说了,我要砸电脑了……
GFWcomic

——————图片引自http://factsanddetails.com/

现在见到最多的已经不是”Connection is reset(连接被重置)”,而是”This webpage is not available”,果然没有最牛最有最傻X。
最近用到强大的C版本gSoap库处理Web service服务端和客户端。虽然以前用过,但这次需要用到数据库,需要返回结果集,这里就直接贴出代码,分别返回单条结构体和多条的结构体数组。
接口interface.h是这样定义的:

//interface.h
//soapcpp2 -c -L -S -x  interface.h 
//soapcpp2 -c -C -L -x   interface.h 
struct  OnlWS__FuckGFWResp
{
   char *Google;
   char *MrFang;
   char *GreatWall;
   char *Blocks;
   char *DnsPollution;
   char *CNShit;
};

struct  OnlWS__GetFreeNet
{
    char *Free;
    char *FreeAgain;
    char *FreeEver;
};

struct OnlWS__GetFreeNetResp
{
    struct OnlWS__GetFreeNet * __ptr;
    int  __size;
}; 
int OnlWS__GetShitGFW(char *option,struct OnlWS__FuckGFWResp  *resp);
int OnlWS__GetFreeInfo(char *option,struct OnlWS__GetFreeNetResp  *resp);

接口定义好后使用gsoap提供的工具生成服务端代码:

soapcpp2 -c -L -S -x  interface.h

接下来实现服务端代码和接口的逻辑:(略去一些代码),接口处理逻辑需要返回结构体数组,因此需要开辟一定长度的空间,这里使用链表来动态开辟。

    //........
    resp->__size=nRecCnt;
    resp->__ptr= soap_malloc(soap,(resp->__size+1)*sizeof(*resp->__ptr));
    
    pLink->ptAnyNode= soap_malloc(SOME_DATA_STRUCT);
    pLink->pNext=NULL;
    tAnyLink *p ,*phead=pLink;
    for(i=0;i<resp->__size-1;i++)
    {
        p=(tAnyLink *)soap_malloc(soap,sizeof(tAnyLink));
        p->ptAnyNode=(SOME_DATA_STRUCT *)soap_malloc(soap,sizeof(SOME_DATA_STRUCT));
        p->pNext=NULL;
        phead->pNext=p;
        phead=p;
    }

进行赋值操作,返回结果。

客户端实现比较简单,因为可以直接取到结构体数组的长度,直接使用数组下标即可获取到结果。这里列出Python的测试程序:使用suds 包来完成客户端处理web service .

# -*- coding: utf-8 -*-
import suds
import logging
import sys

def main():
    print sys.stdout.encoding
    url="http://192.168.101.181:11080" 
    client=suds.client.Client(url)
    rst=client.service.GetShitGFW('WALL')
    print client.last_received()
    rs=client.service.GetFreeInfo('FREE')
    print rs
    print client.last_received()
    
if __name__=='__main__':
    main()


看看返回的情况:

(GetFreeNetResp){
   item[] = 
      (GetFreeNet){
         Free = "01"
         FreeAgain = "A"
         FreeEver = "2014-07-18 07:37:51.110000"
      },
      (GetFreeNet){
         Free = "10"
         FreeAgain = "B"
         FreeEver = "2014-07-18 07:37:51.110000"
      },
      (GetFreeNet){
         Free = "15"
         FreeAgain = "G"
         FreeEver = "2014-07-18 07:37:51.110000"
      },
      (GetFreeNet){
         Free = "20"
         FreeAgain = "C"
         FreeEver = "2014-07-18 07:37:51.110000"
      },
 }

好了,上网不易,且行且珍惜。

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.