Subversion Repositories HomeAutomation

Rev

Rev 1959 | Rev 1987 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
1592 runge 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
 
1962 runge 25
#include "common/log.h"
26
 
1592 runge 27
namespace atom {
28
namespace net {
29
 
1962 runge 30
static const std::string log_module_ = "net::tcpclient";
1898 runge 31
 
1962 runge 32
TcpClient::TcpClient(boost::asio::io_service& io_service, SocketId id, SocketId server_id) : Client(io_service, id, server_id)
1592 runge 33
{
1962 runge 34
  LOG_DEBUG_ENTER;
35
 
36
  LOG_DEBUG_EXIT;
1592 runge 37
}
1962 runge 38
 
39
TcpClient::TcpClient(boost::asio::io_service& io_service, TcpSocketPointer socket, SocketId id, SocketId server_id) : Client(io_service, id, server_id)
1592 runge 40
{
1962 runge 41
  LOG_DEBUG_ENTER;
42
 
43
  this->socket_ = socket;
44
 
45
  this->Read();
46
 
47
  LOG_DEBUG_EXIT;
1592 runge 48
}
49
 
1962 runge 50
TcpClient::~TcpClient()
1592 runge 51
{
1962 runge 52
  LOG_DEBUG_ENTER;
53
 
54
  LOG_DEBUG_EXIT;
1592 runge 55
}
56
 
1962 runge 57
void TcpClient::Connect(std::string address, unsigned int port)
1592 runge 58
{
1962 runge 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);
1592 runge 66
 
1962 runge 67
  this->socket_->connect(*it);
1592 runge 68
 
1962 runge 69
  this->Read();
70
 
71
  LOG_DEBUG_EXIT;
1592 runge 72
}
73
 
1642 runge 74
void TcpClient::Send(common::Byteset data)
1592 runge 75
{
1962 runge 76
  LOG_DEBUG_ENTER;
77
 
78
  try
79
  {
80
    if (this->socket_->is_open())
1593 runge 81
    {
1962 runge 82
      this->socket_->send(boost::asio::buffer(data.Get(), data.GetMaxSize()));
1593 runge 83
    }
1962 runge 84
  }
85
  catch (std::exception& e)
86
  {
87
    this->Disconnect();
88
      //throw std::runtime_error(e.what());
89
  }
90
 
91
  LOG_DEBUG_EXIT;
1592 runge 92
}
93
 
94
void TcpClient::Read()
95
{
1962 runge 96
  LOG_DEBUG_ENTER;
97
 
98
  Client::Read();
1592 runge 99
 
1962 runge 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;
1592 runge 107
}
108
 
1962 runge 109
 
1592 runge 110
}; // namespace net
111
}; // namespace atom