C ++ Boost: инициализация конечной точки после конструктора

Я пытаюсь сделать класс для использования UDP. Я хочу инициализировать конечную точку после конструктора, поэтому я изменяю ее следующим образом:

class UdpSender
{
private:
boost::asio::ip::udp::endpoint endpoint;
boost::asio::ip::udp::socket socket;

string multicast_address;
unsigned short multicast_port;
boost::thread_group threads;    // thread group
boost::thread* thread_main;     // main thread
boost::thread* thread_listen;   // listen thread
boost::thread* thread_getsend;  // get/send thread
boost::mutex stopMutex;
bool initialize = false;
bool stop, showBroadcast;
int i_getsend, i_listen, i_main, i_message, interval;
string message;
public:
// constructor
UdpSender(boost::asio::io_service& io_service, std::string multicast_address, unsigned short multicast_port, int interval, bool show = false)
:
//endpoint(boost::asio::ip::address::from_string(multicast_address), multicast_port),
//socket(io_service, endpoint.protocol()),
multicast_address(multicast_address),
multicast_port(multicast_port),
interval(interval),
showBroadcast(show)
{
initialize = true;
Initialize(io_service);
}

UdpSender(boost::asio::io_service& io_service)
{
initialize = true;
Initialize();
}
// destructor
~UdpSender()
{
// show exit message
cout << "Exiting UDP Communicator." << endl;
}

// initialize
void Initialize(boost::asio::io_service& io_service)
{
if (initialize == false)
{
GetInfo();
}

boost::asio::ip::udp::endpoint endpoint(boost::asio::ip::make_address(multicast_address), multicast_port);
boost::asio::ip::udp::socket socket(io_service, endpoint.protocol());
socket.set_option(boost::asio::ip::udp::socket::reuse_address(true));   // no need

thread_main = new boost::thread(boost::ref(*this));
//thread_listen = new boost::thread(&UdpSender::Callable_Listen, this, interval, boost::ref(i_listen));
//threads.add_thread(thread_listen);    // listen thread
thread_getsend = new boost::thread(&UdpSender::Callable_GetSend, this, interval, boost::ref(i_listen), boost::ref(message));
threads.add_thread(thread_getsend); // get/send thread
stop = false;
i_getsend = 0;
i_listen = 0;
i_main = 0;
i_message = 0;
message.clear();

initialize = true;
}
// ============ other things removed to shorten code ============
};

Видимо, ему не нравятся эти две строки, закомментированные из исходного конструктора:

//endpoint(boost::asio::ip::address::from_string(multicast_address), multicast_port),
//socket(io_service, endpoint.protocol()),

Также не нравится новый конструктор:

UdpSender(boost::asio::io_service& io_service)

Что я могу сделать, чтобы сделать все это возможным? Я действительно ценю твою помощь. Благодарю.

0

Решение

Нет. Согласно справке Asio, объект конечной точки должен быть настроен через его конструкторы.

Тем не менее, вы могли бы держать endpoint указателем [smart-] и создайте его после UdpSender строительство.

// defining:
private:
boost::shared_ptr<boost::asio::ip::udp::endpoint> endpoint;

// creating in Initialize()
endpoint = boost::make_shared<boost::asio::ip::udp::endpoint>(boost::asio::ip::address::from_string(multicast_address), multicast_port);
2

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector