Subversion Repositories HomeAutomation

Rev

Rev 1987 | 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.   //log::Debug(log_module_, "data=\"%s\", size=%u", std::string(data.begin(), data.end()).data(), data.size());
  79.  
  80.   try
  81.   {
  82.     if (this->socket_->is_open())
  83.     {
  84.       this->socket_->send(boost::asio::buffer(data.data(), data.size()));
  85.     }
  86.   }
  87.   catch (std::exception& e)
  88.   {
  89.     this->Disconnect();
  90.       //throw std::runtime_error(e.what());
  91.   }
  92.  
  93.   LOG_DEBUG_EXIT;
  94. }
  95.  
  96. void TcpClient::Read()
  97. {
  98.   LOG_DEBUG_ENTER;
  99.  
  100.   Client::Read();
  101.  
  102.   //log::Debug(log_module_, "this->buffer_.capacity()=%u", this->buffer_.capacity());
  103.    
  104.   this->socket_->async_read_some(boost::asio::buffer(this->buffer_.data(), this->buffer_.capacity()),
  105.                                  boost::bind(&TcpClient::ReadHandler,
  106.                                              this,
  107.                                              boost::asio::placeholders::error,
  108.                                              boost::asio::placeholders::bytes_transferred));
  109.  
  110.   LOG_DEBUG_EXIT;
  111. }
  112.  
  113.  
  114. }; // namespace net
  115. }; // namespace atom
  116.