From 467ac2b93ae592eb58d7e140e8d144b6bd3ba99d Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 25 Nov 2008 15:47:14 +0000 Subject: anyrtpproxy: commandhanlder uses now boost::asio and boost::thread --- src/anyrtpproxy/commandHandler.cpp | 47 ++++++++++++++++++++------------------ src/anyrtpproxy/commandHandler.h | 12 ++++++---- 2 files changed, 33 insertions(+), 26 deletions(-) (limited to 'src/anyrtpproxy') diff --git a/src/anyrtpproxy/commandHandler.cpp b/src/anyrtpproxy/commandHandler.cpp index f95b3ab..495f6e6 100644 --- a/src/anyrtpproxy/commandHandler.cpp +++ b/src/anyrtpproxy/commandHandler.cpp @@ -35,6 +35,8 @@ #include #include +#include + #include "commandHandler.h" #include "../buffer.h" #include "../log.h" @@ -46,56 +48,57 @@ #define MAX_COMMAND_LENGTH 1000 -CommandHandler::CommandHandler(SyncQueue& q, std::string lp,PortWindow & pw) : queue_(q), running_(true), control_sock_(lp), - local_address_(""), local_port_(lp),port_window_(pw) -{ - pthread_create(&thread_, NULL, run, this); -} - -CommandHandler::CommandHandler(SyncQueue& q, string la, std::string lp, PortWindow & pw) : queue_(q), running_(true), control_sock_(la, lp), - local_address_(la), local_port_(lp),port_window_(pw) +CommandHandler::CommandHandler(SyncQueue& q, std::string lp,PortWindow & pw) : thread_(boost::bind(run,this)), + queue_(q), running_(true), control_sock_(io_service_), + local_address_(""), local_port_(lp),port_window_(pw) { - pthread_create(&thread_, NULL, run, this); + proto::resolver resolver(io_service_); + proto::resolver::query query(local_port_); + proto::endpoint e = *resolver.resolve(query); + control_sock_.open(e.protocol()); + control_sock_.bind(e); } -CommandHandler::~CommandHandler() +CommandHandler::CommandHandler(SyncQueue& q, string la, std::string lp, PortWindow & pw) : thread_(boost::bind(run,this)), + queue_(q), running_(true), control_sock_(io_service_), + local_address_(la), local_port_(lp),port_window_(pw) { - pthread_cancel(thread_); - pthread_join(thread_, NULL); + proto::resolver resolver(io_service_); + proto::resolver::query query(local_address_, local_port_); + proto::endpoint e = *resolver.resolve(query); + control_sock_.open(e.protocol()); + control_sock_.bind(e); } -void* CommandHandler::run(void* s) +void CommandHandler::run(void* s) { CommandHandler* self = reinterpret_cast(s); Buffer buf(u_int32_t(MAX_COMMAND_LENGTH)); try { - string remote_host; - u_int16_t remote_port; - + proto::endpoint remote_end; + int len; while(1) { buf.setLength(MAX_COMMAND_LENGTH); - len = self->control_sock_.recvFrom(buf.getBuf(), buf.getLength(), remote_host, remote_port); + + len = self->control_sock_.receive_from(boost::asio::buffer(buf.getBuf(), buf.getLength()), remote_end); buf.setLength(len); std::string ret = self->handle(std::string(reinterpret_cast(buf.getBuf()), buf.getLength())); // TODO: reinterpret is ugly - cLog.msg(Log::PRIO_DEBUG) << "CommandHandler received Command from " << remote_host << ":" << remote_port - << ", ret='" << ret << "'"; + cLog.msg(Log::PRIO_DEBUG) << "CommandHandler received Command from " << remote_end << ", ret='" << ret << "'"; - self->control_sock_.sendTo(ret.c_str(), ret.length(), remote_host, remote_port); + self->control_sock_.send_to(boost::asio::buffer(ret.c_str(), ret.length()), remote_end); } } catch(SocketException &e) { self->running_ = false; - pthread_exit(NULL); } self->running_ = false; - pthread_exit(NULL); } bool CommandHandler::isRunning() diff --git a/src/anyrtpproxy/commandHandler.h b/src/anyrtpproxy/commandHandler.h index b7a565a..6bd802a 100644 --- a/src/anyrtpproxy/commandHandler.h +++ b/src/anyrtpproxy/commandHandler.h @@ -32,6 +32,8 @@ #ifndef _COMMAND_HANDLER_H_ #define _COMMAND_HANDLER_H_ +#include + #include #include "../datatypes.h" #include "../PracticalSocket.h" @@ -41,9 +43,10 @@ class CommandHandler { public: + typedef boost::asio::ip::udp proto; + CommandHandler(SyncQueue& q, std::string lp, PortWindow &); CommandHandler(SyncQueue& q, std::string la, std::string lp, PortWindow &); - ~CommandHandler(); bool isRunning(); @@ -64,7 +67,7 @@ private: CommandHandler(const CommandHandler &c); void operator=(const CommandHandler &c); - static void* run(void* s); + static void run(void* s); std::string handle(std::string command); std::string handleRequest(std::string modifiers, std::string call_id, std::string addr, std::string port, std::string from_tag, std::string to_tag); @@ -74,11 +77,12 @@ private: std::string handleVersionF(std::string date_code); std::string handleInfo(); - pthread_t thread_; + boost::thread thread_; SyncQueue& queue_; bool running_; - UDPSocket control_sock_; + boost::asio::io_service io_service_; + proto::socket control_sock_; std::string local_address_; std::string local_port_; PortWindow& port_window_; -- cgit v1.2.3