I need to initialize thread with a function that will just call a method of UDPClient class that I pass to the thread
I've got that code:
class UDPClient
{
private:
boost::asio::io_service& io_service_;
udp::socket socket_;
udp::endpoint endpoint_;
public:
UDPClient(boost::asio::io_service& io_service, std::string host, std::string port) : io_service_(io_service), socket_(io_service, udp::endpoint(udp::v4(), 0)) {
udp::resolver resolver(io_service_);
udp::resolver::query query(udp::v4(), host, port);
udp::resolver::iterator iter = resolver.resolve(query);
endpoint_ = *iter;
}
void send(std::vector<unsigned char> msg) {
socket_.send_to(boost::asio::buffer(msg, msg.size()), endpoint_);
std::cout << "sent" << std::endl;
}
std::vector<unsigned char> receive() {
std::vector<unsigned char> receive_data;
receive_data.resize(65495);
udp::endpoint sender;
size_t len = socket_.receive_from(boost::asio::buffer(receive_data), sender);
receive_data.resize(len);
return receive_data;
}
};
void getMsg(UDPClient client) {
while (true) {
auto x = client.receive();
cout << "received "<< std::hex << x[0] << endl;
}
}
int main()
{
boost::asio::io_service io_service;
UDPClient client(io_service, std::string("localhost"), std::string("5213"));
std::thread thr1(getMsg, client);
thr1.join();
}
The following error occurs during compilation
C2661 'std::tuple<void (__cdecl *)(UDPClient),UDPClient>::tuple': no overloaded function takes 2 arguments
C2672 'invoke': no matching overloaded function found
I tried to pass the class object by pointer, but it got rid of only one of the errors. Code:
void getMsg(UDPClient *client) {
while (true) {
auto x = client->receive();
cout << "received "<< std::hex << x[0] << endl;
}
}
int main()
{
boost::asio::io_service io_service;
UDPClient client(io_service, std::string("localhost"), std::string("5213"));
std::thread thr1(getMsg, std::ref(client));
thr1.join();
}
Errors:
Error C2672 'invoke': no matching overloaded function found