From 1e6fc933a4ae679e7d6ae2e9ef2540980960630e Mon Sep 17 00:00:00 2001 From: tom Date: Tue, 10 Dec 2019 09:47:02 -0600 Subject: [PATCH] add a parameter to subscribe callback so for wildcard subscriptions the client can see the actual subject name that fired --- CMakeLists.txt | 6 +++--- src/redisclient/impl/redisasyncclient.cpp | 8 ++++---- src/redisclient/impl/redisclientimpl.cpp | 8 ++++---- src/redisclient/impl/redisclientimpl.h | 11 +++++++---- src/redisclient/redisasyncclient.h | 8 ++++---- 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 97dca31..090c095 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,10 +20,10 @@ set(CMAKE_CXX_STANDARD 11) # Build options option(ADDRESS_SANITIZER "Enable address sanitizer" OFF) option(BENCHMARK "Build benmarks" OFF) -option(BUILD_SHARED_LIBS "Whether to build shared libraries" ON) -option(BUILD_TEST "Whether to build the unit tests" ON) +option(BUILD_SHARED_LIBS "Whether to build shared libraries" OFF) +option(BUILD_TEST "Whether to build the unit tests" OFF) option(BUILD_EXAMPLES "Whether to build the examples" ON) -option(HEADER_ONLY "Whether to build in header-only mode" OFF) +option(HEADER_ONLY "Whether to build in header-only mode" ON) # Default to Release mode if (NOT CMAKE_BUILD_TYPE) diff --git a/src/redisclient/impl/redisasyncclient.cpp b/src/redisclient/impl/redisasyncclient.cpp index e09137a..0a1d890 100644 --- a/src/redisclient/impl/redisasyncclient.cpp +++ b/src/redisclient/impl/redisasyncclient.cpp @@ -103,7 +103,7 @@ void RedisAsyncClient::command(const std::string &cmd, std::deque a RedisAsyncClient::Handle RedisAsyncClient::subscribe( const std::string &channel, - std::function msg)> msgHandler, + RedisClientImpl::MsgHandler msgHandler, std::function handler) { auto handleId = pimpl->subscribe("subscribe", channel, msgHandler, handler); @@ -112,7 +112,7 @@ RedisAsyncClient::Handle RedisAsyncClient::subscribe( RedisAsyncClient::Handle RedisAsyncClient::psubscribe( const std::string &pattern, - std::function msg)> msgHandler, + RedisClientImpl::MsgHandler msgHandler, std::function handler) { auto handleId = pimpl->subscribe("psubscribe", pattern, msgHandler, handler); @@ -130,14 +130,14 @@ void RedisAsyncClient::punsubscribe(const Handle &handle) } void RedisAsyncClient::singleShotSubscribe(const std::string &channel, - std::function msg)> msgHandler, + RedisClientImpl::MsgHandler msgHandler, std::function handler) { pimpl->singleShotSubscribe("subscribe", channel, msgHandler, handler); } void RedisAsyncClient::singleShotPSubscribe(const std::string &pattern, - std::function msg)> msgHandler, + RedisClientImpl::MsgHandler msgHandler, std::function handler) { pimpl->singleShotSubscribe("psubscribe", pattern, msgHandler, handler); diff --git a/src/redisclient/impl/redisclientimpl.cpp b/src/redisclient/impl/redisclientimpl.cpp index fc43856..e079902 100644 --- a/src/redisclient/impl/redisclientimpl.cpp +++ b/src/redisclient/impl/redisclientimpl.cpp @@ -276,7 +276,7 @@ void RedisClientImpl::doProcessMessage(RedisValue v) SingleShotHandlersMap::iterator it = singleShotMsgHandlers.find(pattern.toString()); if( it != singleShotMsgHandlers.end() ) { - strand.post(std::bind(it->second, value.toByteArray())); + strand.post(std::bind(it->second, value.toByteArray(), queueName.toByteArray())); singleShotMsgHandlers.erase(it); } @@ -285,7 +285,7 @@ void RedisClientImpl::doProcessMessage(RedisValue v) for(MsgHandlersMap::iterator handlerIt = pair.first; handlerIt != pair.second; ++handlerIt) { - strand.post(std::bind(handlerIt->second.second, value.toByteArray())); + strand.post(std::bind(handlerIt->second.second, value.toByteArray(), queueName.toByteArray())); } } else if( handlers.empty() == false && @@ -555,7 +555,7 @@ void RedisClientImpl::defaulErrorHandler(const std::string &s) size_t RedisClientImpl::subscribe( const std::string &command, const std::string &channel, - std::function msg)> msgHandler, + MsgHandler msgHandler, std::function handler) { assert(state == State::Connected || @@ -586,7 +586,7 @@ size_t RedisClientImpl::subscribe( void RedisClientImpl::singleShotSubscribe( const std::string &command, const std::string &channel, - std::function msg)> msgHandler, + MsgHandler msgHandler, std::function handler) { assert(state == State::Connected || diff --git a/src/redisclient/impl/redisclientimpl.h b/src/redisclient/impl/redisclientimpl.h index 16a9b4a..bf43f3d 100644 --- a/src/redisclient/impl/redisclientimpl.h +++ b/src/redisclient/impl/redisclientimpl.h @@ -36,6 +36,11 @@ class RedisClientImpl : public std::enable_shared_from_this { Subscribed, Closed }; + + typedef std::function &buf,const std::vector &p)> MsgHandler; + typedef std::pair MsgHandlerType; + typedef std::function &buf,const std::vector &p)> SingleShotHandlerType; + REDIS_CLIENT_DECL RedisClientImpl(boost::asio::io_service &ioService); REDIS_CLIENT_DECL ~RedisClientImpl(); @@ -46,12 +51,12 @@ class RedisClientImpl : public std::enable_shared_from_this { REDIS_CLIENT_DECL size_t subscribe(const std::string &command, const std::string &channel, - std::function msg)> msgHandler, + MsgHandler msgHandler, std::function handler); REDIS_CLIENT_DECL void singleShotSubscribe(const std::string &command, const std::string &channel, - std::function msg)> msgHandler, + MsgHandler msgHandler, std::function handler); REDIS_CLIENT_DECL void unsubscribe(const std::string &command, @@ -98,8 +103,6 @@ class RedisClientImpl : public std::enable_shared_from_this { size_t bufSize; // only for sync size_t subscribeSeq; - typedef std::pair &buf)> > MsgHandlerType; - typedef std::function &buf)> SingleShotHandlerType; typedef std::multimap MsgHandlersMap; typedef std::multimap SingleShotHandlersMap; diff --git a/src/redisclient/redisasyncclient.h b/src/redisclient/redisasyncclient.h index 7cf6f6a..3b6aff6 100644 --- a/src/redisclient/redisasyncclient.h +++ b/src/redisclient/redisasyncclient.h @@ -69,12 +69,12 @@ class RedisAsyncClient : boost::noncopyable { // when someone publish message on channel. Call unsubscribe // to stop the subscription. REDIS_CLIENT_DECL Handle subscribe(const std::string &channelName, - std::function msg)> msgHandler, + RedisClientImpl::MsgHandler msgHandler, std::function handler = &dummyHandler); REDIS_CLIENT_DECL Handle psubscribe(const std::string &pattern, - std::function msg)> msgHandler, + RedisClientImpl::MsgHandler msgHandler, std::function handler = &dummyHandler); // Unsubscribe @@ -86,12 +86,12 @@ class RedisAsyncClient : boost::noncopyable { // unsubscribed after call. REDIS_CLIENT_DECL void singleShotSubscribe( const std::string &channel, - std::function msg)> msgHandler, + RedisClientImpl::MsgHandler msgHandler, std::function handler = &dummyHandler); REDIS_CLIENT_DECL void singleShotPSubscribe( const std::string &channel, - std::function msg)> msgHandler, + RedisClientImpl::MsgHandler msgHandler, std::function handler = &dummyHandler); // Publish message on channel.