微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用虚函数作为主题订阅者的安慰回调

如何解决使用虚函数作为主题订阅者的安慰回调

我正在尝试通过 C++ 类使用 Solace c API。

在现有项目中已经创建了引用 Base 和 Derived 类的框架,其中派生的虚函数被相应地调用为运行时引用的对象。

我的最后一个任务是将 Solace 实现为“订阅主题”,其中我将使用与现有基类的回调方法相同的虚函数

为了做到这一点,我将任务划分为 stepd,第一步我创建了简单的类并尝试使用成员函数作为回调方法,但我收到了错误。请建议我应该遵循的方法

在下面的代码中,sessionMessageReceiveCallback 是回调方法,每当将 messaged 发布到 TestTopic 时,都会调用 sessionMessageReceiveCallback

我需要在下面的代码行中调用虚拟方法

sessionFuncInfo.rxmsgInfo.callback_p = sessionMessageReceiveCallback;
sessionFuncInfo.rxmsgInfo.user_p = NULL;

完整代码如下:

os.h

#pragma once

/** @example Intro/os.h
 */

 /*
  * OS-specific methods for abstracting.
  * copyright 2008-2019 Solace Corporation. All rights reserved.
  */

#ifndef ___OS_H_
#define ___OS_H_

#ifdef __cplusplus
extern          "C"
{
#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


#ifdef WIN32
#define _WIN32_WINNT 0x400      /* Require Windows NT5 (2000,XP,2003) for SignalObjectAndWait */
#include <winsock2.h>
#include <windows.h>
#include <winbase.h>

#define SLEEP(sec)  Sleep ( (sec) * 1000 )
#define strcasecmp (_stricmp)
#define strncasecmp (_strnicmp)
#else
#include <unistd.h>

#define SLEEP(sec) sleep ( (sec) )
#endif



#ifdef __cplusplus
}
#endif
#endif

TopicSubscr.h

#include "os.h"
#include "solclient/solClient.h"
#include "solclient/solClientMsg.h"

class TopicSubscr
{
private :
    solClient_rxmsgCallback_returnCode_t
        sessionMessageReceiveCallback(solClient_opaqueSession_pt opaqueSession_p,solClient_opaqueMsg_pt msg_p,void* user_p);
public :
    solClient_opaqueSession_pt session_p;
    static int msgCount;
    explicit TopicSubscr();
     ~TopicSubscr();
     
};

TopicSubscr.cpp

#include "TopicSubscr.h"

  /* Message Count */
static int msgCount = 0;

/*****************************************************************************
 * sessionMessageReceiveCallback
 *
 * The message callback is invoked for each Direct message received by
 * the Session. In this sample,the message is printed to the screen.
 *****************************************************************************/
solClient_rxmsgCallback_returnCode_t
TopicSubscr::sessionMessageReceiveCallback(solClient_opaqueSession_pt opaqueSession_p,void* user_p)
{
    printf("Received message:\n");
    solClient_msg_dump(msg_p,NULL,0);
    printf("\n");

    //msgCount++;

    return SOLCLIENT_CALLBACK_OK;
}

/*****************************************************************************
 * sessionEventCallback
 *
 * The event callback function is mandatory for session creation.
 *****************************************************************************/
void
sessionEventCallback(solClient_opaqueSession_pt opaqueSession_p,solClient_session_eventCallbackInfo_pt eventInfo_p,void* user_p)
{
}

TopicSubscr::TopicSubscr()
{
    /* Context */
    solClient_opaqueContext_pt context_p;
    solClient_context_createFuncInfo_t contextFuncInfo = SOLCLIENT_CONTEXT_CREATEFUNC_INITIALIZER;

    /* Session */
    
    solClient_session_createFuncInfo_t sessionFuncInfo = SOLCLIENT_SESSION_CREATEFUNC_INITIALIZER;

    /* Session Properties */
    const char* sessionProps[25] = { 0,};
    int             propIndex = 0;

    /*************************************************************************
     * Initialize the API (and setup logging level)
     *************************************************************************/

     /* solClient needs to be initialized before any other API calls. */
    solClient_initialize(SOLCLIENT_LOG_DEFAULT_FILTER,NULL);
    printf("TopicSubscriber initializing...\n");

    /*************************************************************************
     * Create a Context
     *************************************************************************/

     /*
      * Create a Context,and specify that the Context thread be created
      * automatically instead of having the application create its own
      * Context thread.
      */
    solClient_context_create(SOLCLIENT_CONTEXT_PROPS_DEFAULT_WITH_CREATE_THREAD,&context_p,&contextFuncInfo,sizeof(contextFuncInfo));

    /*************************************************************************
     * Create and connect a Session
     *************************************************************************/

     /* Configure the Session function information. */
    sessionFuncInfo.rxmsgInfo.callback_p = sessionMessageReceiveCallback;
    sessionFuncInfo.rxmsgInfo.user_p = NULL;
    sessionFuncInfo.eventInfo.callback_p = sessionEventCallback; // <-- Error here
    sessionFuncInfo.eventInfo.user_p = NULL;

    /* Configure the Session properties. */
    propIndex = 0;

    sessionProps[propIndex++] = SOLCLIENT_SESSION_PROP_HOST;
    sessionProps[propIndex++] = "127.0.0.1:55555";

    sessionProps[propIndex++] = SOLCLIENT_SESSION_PROP_VPN_NAME;
    sessionProps[propIndex++] = "default";

    sessionProps[propIndex++] = SOLCLIENT_SESSION_PROP_USERNAME;
    sessionProps[propIndex++] = "admin";

    sessionProps[propIndex++] = SOLCLIENT_SESSION_PROP_PASSWORD;
    sessionProps[propIndex++] = "admin";

    /* Create the Session. */
    solClient_session_create(sessionProps,context_p,&session_p,&sessionFuncInfo,sizeof(sessionFuncInfo));

    /* Connect the Session. */
    solClient_session_connect(session_p);

    /*************************************************************************
     * Subscribe
     *************************************************************************/

    solClient_session_topicSubscribeExt(session_p,SOLCLIENT_SUBSCRIBE_FLAGS_WAITFORCONFIRM,"TestTopic");


    /*************************************************************************
     * Wait for message
     *************************************************************************/

    printf("Waiting for message......\n");
    fflush(stdout);
    while (true) {
        SLEEP(1);
    }

    printf("Exiting.\n");

}


TopicSubscr::~TopicSubscr()
{
    /*************************************************************************
     * Unsubscribe
     *************************************************************************/

    solClient_session_topicUnsubscribeExt(session_p,"TestTopic");

    /*************************************************************************
     * Cleanup
     *************************************************************************/

     /* Cleanup solClient. */
    solClient_cleanup();
}

Main.cpp

#include "TopicSubscr.h"
int main()
{
    TopicSubscr subsc;

    return 0;
}

我收到编译错误

>    C:\SolaceCCode\solace-samples-c-master\build\intro\win\VS2008\TopicSubscriber\TopicSubscr.cpp(76,73): error C3867: 'TopicSubscr::sessionMessageReceiveCallback': non-standard Syntax; use '&' to create a pointer to member
2>    C:\SolaceCCode\solace-samples-c-master\build\intro\win\VS2008\TopicSubscriber\TopicSubscr.cpp(76,73): error C2440: '=': cannot convert from 'solClient_rxmsgCallback_returnCode_t (__thiscall TopicSubscr::* )(solClient_opaqueSession_pt,solClient_opaqueMsg_pt,void *)' to 'solClient_session_rxmsgCallbackFunc_t'
2>    C:\SolaceCCode\solace-samples-c-master\build\intro\win\VS2008\TopicSubscriber\TopicSubscr.cpp(76,44): message : There is no context in which this conversion is possible

solclient.h的引用:

solClientMsg.h的引用:

Example I used for reference :

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。