Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

send_bytes in receive callback #18

Open
reinzor opened this issue May 7, 2021 · 4 comments
Open

send_bytes in receive callback #18

reinzor opened this issue May 7, 2021 · 4 comments

Comments

@reinzor
Copy link
Contributor

reinzor commented May 7, 2021

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

@dpkoch
Copy link
Owner

dpkoch commented May 8, 2021

Hi Rein, a couple of thoughts come to mind:

  • In this snippet, udp is not defined in the callback() 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));

  // ...
}

@dpkoch
Copy link
Owner

dpkoch commented May 8, 2021

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

@reinzor
Copy link
Contributor Author

reinzor commented May 10, 2021

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!

@reinzor
Copy link
Contributor Author

reinzor commented May 10, 2021

I think I've found the problem here:

socket_.async_receive_from(buffer, remote_endpoint_, handler);

Is it logical to update the remote_endpoint_ here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants