Subversion Repositories HomeAutomation

Rev

Rev 1963 | 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.   log::Debug(log_module_, "data=\"%s\", size=%u", std::string(data.begin(), data.end()).data(), data.size());
  87.  
  88.   try
  89.   {
  90.     if (this->socket_->is_open())
  91.     {
  92.       this->socket_->send_to(boost::asio::buffer(data.data(), data.size()), this->endpoint_);
  93.     }
  94.   }
  95.   catch (std::exception& e)
  96.   {
  97.     this->Disconnect();
  98.     //throw std::runtime_error(e.what());
  99.   }
  100.  
  101.   LOG_DEBUG_EXIT;
  102. }
  103.  
  104. void UdpClient::Read()
  105. {
  106.   LOG_DEBUG_ENTER;
  107.  
  108.   Client::Read();
  109.  
  110.   log::Debug(log_module_, "this->buffer_.capacity()=%u", this->buffer_.capacity());
  111.  
  112.   this->socket_->async_receive(boost::asio::buffer(this->buffer_.data(), this->buffer_.capacity()),
  113.                                boost::bind(&UdpClient::ReadHandler,
  114.                                            this,
  115.                                            boost::asio::placeholders::error,
  116.                                            boost::asio::placeholders::bytes_transferred));
  117.  
  118.   LOG_DEBUG_EXIT;
  119. }
  120.  
  121. }; // namespace net
  122. }; // namespace atom
  123.