ecal icon indicating copy to clipboard operation
ecal copied to clipboard

How do I publish a topic in a callback function?

Open Y-pandaman opened this issue 2 years ago • 2 comments

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?

Y-pandaman avatar Jul 22 '22 01:07 Y-pandaman

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.

Kerstin-Keller avatar Jul 22 '22 11:07 Kerstin-Keller

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.

rex-schilasky avatar Jul 22 '22 11:07 rex-schilasky

Closing this as the question seems to be answered. Please reopen, if necessary!

FlorianReimold avatar Sep 23 '22 08:09 FlorianReimold