自学内容网 自学内容网

Linux c++ onvif客户端开发(7):struct soap包装类

 本文是Linux c++ onvif客户端开发系列文章之一:

 可以先先看一下gsoap内存管理这篇文章。

 先定义一个fault结构

struct OnvifFault {
    std::string code;
    std::string subcode;
    std::string str; // string
    std::string detail;
};

再对struct soap进行包装

class OnvifSoap {
public:
    OnvifSoap(int timeout);
    ~OnvifSoap();

    void InitHeader();
    void InitProbeType(struct wsdd__ProbeType *probe);

    int Error() { return soap_->error; }

    // 返回的xml中soap:Fault
    std::string FaultString();
    std::string FaultCode();
    std::string FaultSubcode();
    std::string FaultDetail();

    struct soap *Soap() const;

    void Destroy();

    // 填写Fault信息
    void FillFault(OnvifFault *fault = nullptr);

private:
    struct soap *soap_;
};

 实现


#define SOAP_PROBE_TO "urn:schemas-xmlsoap-org:ws:2005:04:discovery"
#define SOAP_PROBE_ACTION                                                      \
    "http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe"

#define SOAP_MCAST_ADDR "soap.udp://239.255.255.250:3702" // onvif规定的组播地址

#define SOAP_ITEM ""                            // 寻找的设备范围
#define SOAP_TYPES "dn:NetworkVideoTransmitter" // 寻找的设备类型

#define SOAP_SOCK_TIMEOUT (2) // socket超时时间(单秒秒)


OnvifSoap::OnvifSoap(int timeout) {
    // There is no need to call soap_init to initialize the context
    // allocated with soap_new, since soap_new initializes the allocated
    // context.
    // https://www.genivia.com/doc/guide/html/group__group__context.html#ga87c20488b2dc680aaa7689b1d024989c
    soap_ = soap_new();
    if (!soap_)
        throw std::runtime_error("soap_new() fail");

    soap_set_namespaces(soap_, namespaces); // 设置soap的namespaces

    // 不正常数据设置成5s
    if (timeout <= 0)
        timeout = SOAP_SOCK_TIMEOUT;

    soap_->recv_timeout = timeout; // 设置超时(超过指定时间没有数据就退出)
    soap_->send_timeout = timeout;
    soap_->connect_timeout = timeout;

#if defined(__linux__) ||                                                      \
    defined(__linux) // 参考https://www.genivia.com/dev.html#client-c的修改:
    soap_->socket_flags = MSG_NOSIGNAL; // To prevent connection reset errors
#endif

    soap_set_mode(soap_,
                  SOAP_C_UTFSTRING); // 设置为UTF-8编码,否则叠加中文OSD会乱码
}

OnvifSoap::~OnvifSoap() {
    soap_destroy(soap_); // delete managed C++ objects
    soap_end(soap_);     // delete managed memory。soap_malloc
    soap_done(soap_);    // stacked
    soap_free(soap_);    /* we're done with the context */
}

/**
 * @brief 填充Header, 用于Probe操作
 *
<SOAP-ENV:Header>
    <wsa:MessageID>urn:uuid:dc8f9f8a-05b2-45c2-a63e-f5b47636af83</wsa:MessageID>
    <wsa:To
SOAP-ENV:mustUnderstand="true">urn:schemas-xmlsoap-org:ws:2005:04:discovery</wsa:To>
    <wsa:Action
SOAP-ENV:mustUnderstand="true">http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe</wsa:Action>
</SOAP-ENV:Header>
 */
void OnvifSoap::InitHeader() {
    // T * soap_new_T(struct soap*) allocates and initializes data of type T
    // in context-managed heap memory, managed data is deleted with
    // soap_destroy (deletes C++ objects) and soap_end (deletes all other
    // data), and you can also use soap_malloc to allocate uninitialized
    // context-managed memory.
    struct SOAP_ENV__Header *header = soap_new_SOAP_ENV__Header(soap_);

    header->wsa__MessageID = (char *)soap_wsa_rand_uuid(soap_);
    header->wsa__To = soap_strdup(soap_, SOAP_PROBE_TO);
    header->wsa__Action = soap_strdup(soap_, SOAP_PROBE_ACTION);
    soap_->header = header;
}

/**
 * @brief 填充body Probe数据, 用于Probe操作
 *     <SOAP-ENV:Body>
        <wsdd:Probe>
            <wsdd:Types>dn:NetworkVideoTransmitter</wsdd:Types>
            <wsdd:Scopes/>
        </wsdd:Probe>
    </SOAP-ENV:Body>
 * @param probe
 */
void OnvifSoap::InitProbeType(struct wsdd__ProbeType *probe) {
    // 用于描述查找哪类的Web服务
    struct wsdd__ScopesType *scope = soap_new_wsdd__ScopesType(soap_);
    // soap_default_wsdd__ScopesType(soap_, scope); // 设置寻找设备的范围
    scope->__item = soap_strdup(soap_, "");

    probe->Scopes = scope;
    probe->Types = soap_strdup(soap_, SOAP_TYPES); // 设置寻找设备的类型
}

std::string OnvifSoap::FaultString() {
    const char *fault_string = soap_fault_string(soap_);
    if (!fault_string)
        return std::string();
    else
        return std::string(fault_string);
}

std::string OnvifSoap::FaultCode() {
    const char **code = soap_faultcode(soap_);
    if (code && *code)
        return std::string(*code);
    else
        return std::string();
}

std::string OnvifSoap::FaultSubcode() {
    const char *subcode = soap_fault_subcode(soap_);
    if (!subcode)
        return std::string();
    else
        return std::string(subcode);
}

std::string OnvifSoap::FaultDetail() {
    const char *detail = soap_fault_detail(soap_);
    if (!detail)
        return std::string();
    else
        return std::string(detail);
}

struct soap *OnvifSoap::Soap() const {
    return soap_;
}

void OnvifSoap::Destroy() { soap_->destroy(); }

void OnvifSoap::FillFault(OnvifFault *fault) {
    if (!fault)
        return;

    // str
    const char *fault_string = soap_fault_string(soap_);
    if (fault_string)
        fault->str.assign(fault_string);

    // code
    const char **code = soap_faultcode(soap_);
    if (code && *code)
        fault->code.assign(*code);

    // subcode
    const char *subcode = soap_fault_subcode(soap_);
    if (subcode)
        fault->subcode.assign(subcode);

    // detail
    const char *detail = soap_fault_detail(soap_);
    if (detail)
        fault->detail.assign(detail);
}


原文地址:https://blog.csdn.net/noevil/article/details/137884849

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!