Subversion Repositories HomeAutomation

Rev

Rev 1962 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  *
  3.  *    Copyright (C) 2010  Mattias Runge
  4.  *
  5.  *    This program is free software; you can redistribute it and/or modify
  6.  *    it under the terms of the GNU General Public License as published by
  7.  *    the Free Software Foundation; either version 2 of the License, or
  8.  *    (at your option) any later version.
  9.  *
  10.  *    This program is distributed in the hope that it will be useful,
  11.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *    GNU General Public License for more details.
  14.  *
  15.  *    You should have received a copy of the GNU General Public License along
  16.  *    with this program; if not, write to the Free Software Foundation, Inc.,
  17.  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18.  *
  19.  */
  20.  
  21. #include "UdpClient.h"
  22.  
  23. #include <boost/lexical_cast.hpp>
  24.  
  25. #include "common/log.h"
  26.  
  27. namespace atom {
  28. namespace net {
  29.  
  30. static const std::string log_module_ = "net::udpclient";
  31.  
  32. UdpClient::UdpClient(boost::asio::io_service& io_service, SocketId id, SocketId server_id) : Client(io_service, id, server_id)
  33. {
  34.   LOG_DEBUG_ENTER;
  35.   LOG_DEBUG_EXIT;
  36. }
  37.  
  38. UdpClient::~UdpClient()
  39. {
  40.   LOG_DEBUG_ENTER;
  41.   LOG_DEBUG_EXIT;
  42. }
  43.  
  44. void UdpClient::Connect(std::string address, unsigned int port)
  45. {
  46.   LOG_DEBUG_ENTER;
  47.  
  48.   try
  49.   {
  50.     boost::asio::ip::udp::resolver resolver(this->io_service_);
  51.     boost::asio::ip::udp::resolver::query query(boost::asio::ip::udp::v4(), address, boost::lexical_cast<std::string>(port));
  52.     boost::asio::ip::udp::resolver::iterator it = resolver.resolve(query);
  53.    
  54.     this->endpoint_ = *it;
  55.    
  56.     this->socket_.reset(new boost::asio::ip::udp::socket(this->io_service_, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), port)));
  57.   }
  58.   catch (std::exception e)
  59.   {
  60.     throw std::runtime_error("Error while connecting to " + address + ":" + boost::lexical_cast<std::string>(port) + ", " + e.what());
  61.   }
  62.  
  63.   this->Read();
  64.  
  65.   LOG_DEBUG_EXIT;
  66. }
  67.  
  68. void UdpClient::Stop()
  69. {
  70.   LOG_DEBUG_ENTER;
  71.  
  72.   if (this->socket_.use_count() != 0)
  73.   {
  74.     this->socket_->cancel();
  75.     this->socket_->close();
  76.     this->socket_.reset();
  77.   }
  78.  
  79.   LOG_DEBUG_EXIT;
  80. }
  81.  
  82. void UdpClient::Send(common::Byteset data)
  83. {
  84.   LOG_DEBUG_ENTER;
  85.  
  86.   try
  87.   {
  88.     if (this->socket_->is_open())
  89.     {
  90.       this->socket_->send_to(boost::asio::buffer(data.Get(), data.GetMaxSize()), this->endpoint_);
  91.     }
  92.   }
  93.   catch (std::exception& e)
  94.   {
  95.     this->Disconnect();
  96.     //throw std::runtime_error(e.what());
  97.   }
  98.  
  99.   LOG_DEBUG_EXIT;
  100. }
  101.  
  102. void UdpClient::Read()
  103. {
  104.   LOG_DEBUG_ENTER;
  105.  
  106.   Client::Read();
  107.  
  108.   this->socket_->async_receive(boost::asio::buffer(this->buffer_.Get(), this->buffer_.GetMaxSize()),
  109.                                boost::bind(&UdpClient::ReadHandler,
  110.                                            this,
  111.                                            boost::asio::placeholders::error,
  112.                                            boost::asio::placeholders::bytes_transferred));
  113.  
  114.   LOG_DEBUG_EXIT;
  115. }
  116.  
  117. }; // namespace net
  118. }; // namespace atom
  119.