ecal
ecal copied to clipboard
How do I publish a topic in a callback function?
I get the data in the callback function, do some processing on it, and then I want to publish the processed data. Can I take custom parameters in the callback function?
I want to receive eight images, then do the same processing in the callback function, and then pub them according to different publishers afterwards. Can I share one callback function, just pass in different publishers?
Hi,
basically you can register any other function which may take additional arguments for a callback using C++ features, for example using a lambda:
void OnReceive(std::atomic<bool>& alive)
{
alive = true;
}
void main()
{
std::atomic<bool> alive;
eCAL::string::CSubscriber<std::string>> subscriber;
auto callback = [&alive](const char*, const std::string&, long long, long long, long long)
{
OnReceive(alive);
};
subscriber.AddReceiveCallback(callback);
}
In this example a reference to an atomic is passed to the callback, while the other arguments are ignored.
You can also use std::bind
.
If you want to receive the mentioned eight images from different publisher, I would recommend to use eight dedicated subscribers instances - assuming the eight different images are published on eight different topic names. Then every subscriber can use the same callback function with a publisher reference as additional parameter that then can be used to forward the message finally. Best practice would to put that in an OO design and the callback function is a member of this specific class.
Keep in mind to not waste too much time in a callback. You need to finish processing and forwarding before the next message arrives, otherwise there will be message drops.
Closing this as the question seems to be answered. Please reopen, if necessary!