async_comm
                                
                                 async_comm copied to clipboard
                                
                                    async_comm copied to clipboard
                            
                            
                            
                        send_bytes in receive callback
Hi,
I am trying to call the send_bytes method in a registered receive_callback; however, the data is never sent. Am I doing something wrong or what could be the issue here?
Setup:
void callback(const uint8_t* buf, size_t len)
{
  char message[] = "hello world!";
  udp.send_bytes((uint8_t*) message, std::strlen(message));
}
// open UDP ports
async_comm::UDP udp("localhost", 14620, "localhost", 14625);
udp.register_receive_callback(&callback);
Thanks,
Rein
Hi Rein, a couple of thoughts come to mind:
- In this snippet, udpis not defined in thecallback()scope
- Are you sure that you're actually receiving the data and that the callback is being called?
For reference, making the following changes to examples/udp_hello_world.cpp works for me:
void ack_callback(async_comm::UDP &udp, const uint8_t* buf, size_t len)
{
  char ack_message[] = "Acknowledge received";
  udp.send_bytes(reinterpret_cast<uint8_t*>(ack_message), std::strlen(ack_message));
  for (size_t i = 0; i < len; i++)
  {
    std::cout << buf[i];
  }
}
int main()
{
  // open UDP ports
  async_comm::UDP udp1("localhost", 14620, "localhost", 14625);
  udp1.register_receive_callback(std::bind(ack_callback, std::ref(udp1), std::placeholders::_1, std::placeholders::_2));
  // ...
}
You could also do it with a lambda capture, like this:
async_comm::UDP udp1("localhost", 14620, "localhost", 14625);
std::function<void(const uint8_t*, size_t)> ack_callback = [&udp1](const uint8_t* buf, size_t len) {
  char ack_message[] = "Acknowledge received\n";
  udp1.send_bytes(reinterpret_cast<uint8_t*>(ack_message), std::strlen(ack_message));
  for (size_t i = 0; i < len; i++)
  {
      std::cout << buf[i];
  }
};
udp1.register_receive_callback(ack_callback);
Or if the UDP object and the callback were both members of some containing class, then obviously you'd have access to the UDP object from within the callback
Hi @dpkoch , thanks for your quick response. It was a simple snippet so don't bother about scoped etc. I looked a little further and tried your example and it gives me the following output:
$ nc -u localhost 14620
dsa
Acknowledge received
So the Acknowledge received message is sent back to the netcat client, how does this work? Why isn't it sent to the remote_port as specified (14625). When I listen to the 14625 port, it is not sending any data:
$ nc -kul 14625
No message is coming in here. I thought data was being received on the bind address/port and we are sending data to the remote address/port. Maybe I am totally misunderstandings things here ..
Thanks again for your help!
I think I've found the problem here:
https://github.com/dpkoch/async_comm/blob/12e172dfe9ad2a02785adce61289a85def0c27e6/src/udp.cpp#L107
Is it logical to update the remote_endpoint_ here?