Subversion Repositories HomeAutomation

Rev

Rev 1593 | Rev 1596 | 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
 
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
{
1595 runge 34
    if (this->socket_.is_open())
35
    {
36
        this->socket_.cancel();
37
        this->socket_.close();
38
    }
1592 runge 39
 
40
    if (this->acceptor_.use_count() != 0)
41
    {
1595 runge 42
        this->acceptor_->cancel();
1592 runge 43
        this->acceptor_->close();
44
    }
45
}
46
 
47
void TcpClient::Accept(AcceptorPointer acceptor)
48
{
49
    try
50
    {
51
        this->acceptor_ = acceptor;
52
 
53
        this->acceptor_->async_accept(this->socket_,
54
                                      boost::bind(&TcpClient::AcceptHandler,
55
                                                  this,
56
                                                  boost::asio::placeholders::error));
57
    }
58
    catch (std::exception e)
59
    {
60
        throw std::runtime_error("Error while opening port, " + std::string(e.what()));
61
    }
62
}
63
 
64
void TcpClient::AcceptHandler(const boost::system::error_code& error)
65
{
66
    this->Read();
67
 
1593 runge 68
    this->signal_on_new_state_(this->GetId(), this->GetServerId(), CLIENT_STATE_ACCEPTED);
1592 runge 69
}
70
 
71
TcpClient::AcceptorPointer TcpClient::ReleaseAcceptor()
72
{
73
    AcceptorPointer acceptor = this->acceptor_;
74
    this->acceptor_.reset();
75
    return acceptor;
76
}
77
 
78
void TcpClient::Connect(std::string address, unsigned int port)
79
{
80
    try
81
    {
82
        boost::asio::ip::tcp::resolver resolver(this->socket_.get_io_service());
83
        boost::asio::ip::tcp::resolver::query query(boost::asio::ip::tcp::v4(), address, boost::lexical_cast<std::string>(port));
84
        boost::asio::ip::tcp::resolver::iterator it = resolver.resolve(query);
85
 
86
        this->socket_.connect(*it);
87
    }
88
    catch (std::exception e)
89
    {
90
        throw std::runtime_error("Error while connecting to " + address + ":" + boost::lexical_cast<std::string>(port) + ", " + std::string(e.what()));
91
    }
92
 
93
    this->Read();
94
}
95
 
96
void TcpClient::Disconnect()
97
{
1595 runge 98
    if (this->socket_.is_open())
99
    {
100
        this->socket_.cancel();
101
        this->socket_.close();
102
    }
1592 runge 103
 
104
    if (this->acceptor_.use_count() != 0)
105
    {
1595 runge 106
        this->acceptor_->cancel();
1592 runge 107
        this->acceptor_->close();
108
    }
109
 
110
    Client::Disconnect();
111
}
112
 
113
void TcpClient::Send(Buffer data)
114
{
1593 runge 115
    if (this->acceptor_.use_count() != 0)
116
    {
117
        return;
118
    }
119
 
120
    if (this->socket_.is_open())
121
    {
122
        this->socket_.send(boost::asio::buffer(data));
123
    }
124
    else
125
    {
126
        this->Disconnect();
127
    }
1592 runge 128
}
129
 
130
void TcpClient::Read()
131
{
132
    Client::Read();
133
 
134
    this->socket_.async_read_some(boost::asio::buffer(this->buffer_),
135
                                  boost::bind(&TcpClient::ReadHandler,
136
                                              this,
137
                                              boost::asio::placeholders::error,
138
                                              boost::asio::placeholders::bytes_transferred));
139
}
140
 
141
 
142
}; // namespace net
143
}; // namespace atom