Subversion Repositories HomeAutomation

Rev

Rev 1593 | 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. namespace atom {
  26. namespace net {
  27.  
  28. UdpClient::UdpClient(boost::asio::io_service& io_service, ClientId id, ServerId server_id) : io_service_(io_service), Client(io_service, id, server_id)
  29. {
  30. }
  31.  
  32. UdpClient::~UdpClient()
  33. {
  34.     if (this->socket_.use_count() != 0)
  35.     {
  36.         this->socket_->close();
  37.         this->socket_.reset();
  38.     }
  39. }
  40.  
  41. void UdpClient::Connect(std::string address, unsigned int port)
  42. {
  43.     try
  44.     {
  45.         boost::asio::ip::udp::resolver resolver(this->io_service_);
  46.         boost::asio::ip::udp::resolver::query query(boost::asio::ip::udp::v4(), address, boost::lexical_cast<std::string>(port));
  47.         boost::asio::ip::udp::resolver::iterator it = resolver.resolve(query);
  48.        
  49.         this->endpoint_ = *it;
  50.        
  51.         this->socket_.reset(new boost::asio::ip::udp::socket(this->io_service_, boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(), port)));
  52.     }
  53.     catch (std::exception e)
  54.     {
  55.         throw std::runtime_error("Error while connecting to " + address + ":" + boost::lexical_cast<std::string>(port) + ", " + e.what());
  56.     }
  57.    
  58.     this->Read();
  59. }
  60.  
  61. void UdpClient::Disconnect()
  62. {
  63.     if (this->socket_.use_count() != 0)
  64.     {
  65.         this->socket_->close();
  66.         this->socket_.reset();
  67.     }
  68.    
  69.     Client::Disconnect();
  70. }
  71.  
  72. void UdpClient::Send(Buffer data)
  73. {
  74.     this->socket_->send_to(boost::asio::buffer(data), this->endpoint_);
  75. }
  76.  
  77. void UdpClient::Read()
  78. {
  79.     Client::Read();
  80.    
  81.     this->socket_->async_receive(boost::asio::buffer(this->buffer_),
  82.                                  boost::bind(&UdpClient::ReadHandler,
  83.                                              this,
  84.                                              boost::asio::placeholders::error,
  85.                                              boost::asio::placeholders::bytes_transferred));
  86. }
  87.  
  88.  
  89. }; // namespace net
  90. }; // namespace atom
  91.