Subversion Repositories HomeAutomation

Rev

Rev 1959 | 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 "TcpClient.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::tcpclient";
  31.  
  32. TcpClient::TcpClient(boost::asio::io_service& io_service, SocketId id, SocketId server_id) : Client(io_service, id, server_id)
  33. {
  34.   LOG_DEBUG_ENTER;
  35.  
  36.   LOG_DEBUG_EXIT;
  37. }
  38.  
  39. TcpClient::TcpClient(boost::asio::io_service& io_service, TcpSocketPointer socket, SocketId id, SocketId server_id) : Client(io_service, id, server_id)
  40. {
  41.   LOG_DEBUG_ENTER;
  42.  
  43.   this->socket_ = socket;
  44.  
  45.   this->Read();
  46.  
  47.   LOG_DEBUG_EXIT;
  48. }
  49.  
  50. TcpClient::~TcpClient()
  51. {
  52.   LOG_DEBUG_ENTER;
  53.  
  54.   LOG_DEBUG_EXIT;
  55. }
  56.  
  57. void TcpClient::Connect(std::string address, unsigned int port)
  58. {
  59.   LOG_DEBUG_ENTER;
  60.  
  61.   this->socket_ = TcpSocketPointer(new boost::asio::ip::tcp::socket(this->io_service_));
  62.  
  63.   boost::asio::ip::tcp::resolver resolver(this->socket_->get_io_service());
  64.   boost::asio::ip::tcp::resolver::query query(boost::asio::ip::tcp::v4(), address, boost::lexical_cast<std::string>(port));
  65.   boost::asio::ip::tcp::resolver::iterator it = resolver.resolve(query);
  66.  
  67.   this->socket_->connect(*it);
  68.  
  69.   this->Read();
  70.  
  71.   LOG_DEBUG_EXIT;
  72. }
  73.  
  74. void TcpClient::Send(common::Byteset data)
  75. {
  76.   LOG_DEBUG_ENTER;
  77.  
  78.   try
  79.   {
  80.     if (this->socket_->is_open())
  81.     {
  82.       this->socket_->send(boost::asio::buffer(data.Get(), data.GetMaxSize()));
  83.     }
  84.   }
  85.   catch (std::exception& e)
  86.   {
  87.     this->Disconnect();
  88.       //throw std::runtime_error(e.what());
  89.   }
  90.  
  91.   LOG_DEBUG_EXIT;
  92. }
  93.  
  94. void TcpClient::Read()
  95. {
  96.   LOG_DEBUG_ENTER;
  97.  
  98.   Client::Read();
  99.    
  100.   this->socket_->async_read_some(boost::asio::buffer(this->buffer_.Get(), this->buffer_.GetMaxSize()),
  101.                                  boost::bind(&TcpClient::ReadHandler,
  102.                                              this,
  103.                                              boost::asio::placeholders::error,
  104.                                              boost::asio::placeholders::bytes_transferred));
  105.  
  106.   LOG_DEBUG_EXIT;
  107. }
  108.  
  109.  
  110. }; // namespace net
  111. }; // namespace atom
  112.