问题描述
我正在尝试通过 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 (将#修改为@)