Subversion Repositories HomeAutomation

Rev

Rev 1593 | Go to most recent revision | Details | 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
 
25
namespace atom {
26
namespace net {
27
 
28
TcpClient::TcpClient(boost::asio::io_service& io_service, ClientId id, ServerId server_id) : socket_(io_service), Client(io_service, id, server_id)
29
{
30
}
31
 
32
TcpClient::~TcpClient()
33
{
34
    this->socket_.close();
35
 
36
    if (this->acceptor_.use_count() != 0)
37
    {
38
        this->acceptor_->close();
39
    }
40
}
41
 
42
void TcpClient::Accept(AcceptorPointer acceptor)
43
{
44
    try
45
    {
46
        this->acceptor_ = acceptor;
47
 
48
        this->acceptor_->async_accept(this->socket_,
49
                                      boost::bind(&TcpClient::AcceptHandler,
50
                                                  this,
51
                                                  boost::asio::placeholders::error));
52
    }
53
    catch (std::exception e)
54
    {
55
        throw std::runtime_error("Error while opening port, " + std::string(e.what()));
56
    }
57
}
58
 
59
void TcpClient::AcceptHandler(const boost::system::error_code& error)
60
{
61
    this->Read();
62
 
63
    this->signal_on_new_state_(this->GetId(), CLIENT_STATE_ACCEPTED);
64
}
65
 
66
TcpClient::AcceptorPointer TcpClient::ReleaseAcceptor()
67
{
68
    AcceptorPointer acceptor = this->acceptor_;
69
    this->acceptor_.reset();
70
    return acceptor;
71
}
72
 
73
void TcpClient::Connect(std::string address, unsigned int port)
74
{
75
    try
76
    {
77
        boost::asio::ip::tcp::resolver resolver(this->socket_.get_io_service());
78
        boost::asio::ip::tcp::resolver::query query(boost::asio::ip::tcp::v4(), address, boost::lexical_cast<std::string>(port));
79
        boost::asio::ip::tcp::resolver::iterator it = resolver.resolve(query);
80
 
81
        this->socket_.connect(*it);
82
    }
83
    catch (std::exception e)
84
    {
85
        throw std::runtime_error("Error while connecting to " + address + ":" + boost::lexical_cast<std::string>(port) + ", " + std::string(e.what()));
86
    }
87
 
88
    this->Read();
89
}
90
 
91
void TcpClient::Disconnect()
92
{
93
    this->socket_.close();
94
 
95
    if (this->acceptor_.use_count() != 0)
96
    {
97
        this->acceptor_->close();
98
    }
99
 
100
    Client::Disconnect();
101
}
102
 
103
void TcpClient::Send(Buffer data)
104
{
105
    this->socket_.send(boost::asio::buffer(data));
106
}
107
 
108
void TcpClient::Read()
109
{
110
    Client::Read();
111
 
112
    this->socket_.async_read_some(boost::asio::buffer(this->buffer_),
113
                                  boost::bind(&TcpClient::ReadHandler,
114
                                              this,
115
                                              boost::asio::placeholders::error,
116
                                              boost::asio::placeholders::bytes_transferred));
117
}
118
 
119
 
120
}; // namespace net
121
}; // namespace atom