Subversion Repositories HomeAutomation

Rev

Rev 1593 | Rev 1596 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 1593 Rev 1595
1
/*
1
/*
2
 
2
 
3
    Copyright (C) 2010  Mattias Runge
3
    Copyright (C) 2010  Mattias Runge
4
 
4
 
5
    This program is free software; you can redistribute it and/or modify
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
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
7
    the Free Software Foundation; either version 2 of the License, or
8
    (at your option) any later version.
8
    (at your option) any later version.
9
 
9
 
10
    This program is distributed in the hope that it will be useful,
10
    This program is distributed in the hope that it will be useful,
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
    GNU General Public License for more details.
13
    GNU General Public License for more details.
14
 
14
 
15
    You should have received a copy of the GNU General Public License along
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.,
16
    with this program; if not, write to the Free Software Foundation, Inc.,
17
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18
 
18
 
19
*/
19
*/
20
 
20
 
21
#include "TcpClient.h"
21
#include "TcpClient.h"
22
 
22
 
23
#include <boost/lexical_cast.hpp>
23
#include <boost/lexical_cast.hpp>
24
 
24
 
25
namespace atom {
25
namespace atom {
26
namespace net {
26
namespace net {
27
 
27
 
28
TcpClient::TcpClient(boost::asio::io_service& io_service, ClientId id, ServerId server_id) : socket_(io_service), Client(io_service, id, server_id)
28
TcpClient::TcpClient(boost::asio::io_service& io_service, ClientId id, ServerId server_id) : socket_(io_service), Client(io_service, id, server_id)
29
{
29
{
30
}
30
}
31
 
31
 
32
TcpClient::~TcpClient()
32
TcpClient::~TcpClient()
33
{
33
{
-
 
34
    if (this->socket_.is_open())
-
 
35
    {
-
 
36
        this->socket_.cancel();
34
    this->socket_.close();
37
        this->socket_.close();
-
 
38
    }
35
   
39
   
36
    if (this->acceptor_.use_count() != 0)
40
    if (this->acceptor_.use_count() != 0)
37
    {
41
    {
-
 
42
        this->acceptor_->cancel();
38
        this->acceptor_->close();
43
        this->acceptor_->close();
39
    }
44
    }
40
}
45
}
41
 
46
 
42
void TcpClient::Accept(AcceptorPointer acceptor)
47
void TcpClient::Accept(AcceptorPointer acceptor)
43
{
48
{
44
    try
49
    try
45
    {
50
    {
46
        this->acceptor_ = acceptor;
51
        this->acceptor_ = acceptor;
47
       
52
       
48
        this->acceptor_->async_accept(this->socket_,
53
        this->acceptor_->async_accept(this->socket_,
49
                                      boost::bind(&TcpClient::AcceptHandler,
54
                                      boost::bind(&TcpClient::AcceptHandler,
50
                                                  this,
55
                                                  this,
51
                                                  boost::asio::placeholders::error));
56
                                                  boost::asio::placeholders::error));
52
    }
57
    }
53
    catch (std::exception e)
58
    catch (std::exception e)
54
    {
59
    {
55
        throw std::runtime_error("Error while opening port, " + std::string(e.what()));
60
        throw std::runtime_error("Error while opening port, " + std::string(e.what()));
56
    }
61
    }
57
}
62
}
58
 
63
 
59
void TcpClient::AcceptHandler(const boost::system::error_code& error)
64
void TcpClient::AcceptHandler(const boost::system::error_code& error)
60
{
65
{
61
    this->Read();
66
    this->Read();
62
   
67
   
63
    this->signal_on_new_state_(this->GetId(), this->GetServerId(), CLIENT_STATE_ACCEPTED);
68
    this->signal_on_new_state_(this->GetId(), this->GetServerId(), CLIENT_STATE_ACCEPTED);
64
}
69
}
65
 
70
 
66
TcpClient::AcceptorPointer TcpClient::ReleaseAcceptor()
71
TcpClient::AcceptorPointer TcpClient::ReleaseAcceptor()
67
{
72
{
68
    AcceptorPointer acceptor = this->acceptor_;
73
    AcceptorPointer acceptor = this->acceptor_;
69
    this->acceptor_.reset();
74
    this->acceptor_.reset();
70
    return acceptor;
75
    return acceptor;
71
}
76
}
72
 
77
 
73
void TcpClient::Connect(std::string address, unsigned int port)
78
void TcpClient::Connect(std::string address, unsigned int port)
74
{
79
{
75
    try
80
    try
76
    {
81
    {
77
        boost::asio::ip::tcp::resolver resolver(this->socket_.get_io_service());
82
        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));
83
        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);
84
        boost::asio::ip::tcp::resolver::iterator it = resolver.resolve(query);
80
       
85
       
81
        this->socket_.connect(*it);
86
        this->socket_.connect(*it);
82
    }
87
    }
83
    catch (std::exception e)
88
    catch (std::exception e)
84
    {
89
    {
85
        throw std::runtime_error("Error while connecting to " + address + ":" + boost::lexical_cast<std::string>(port) + ", " + std::string(e.what()));
90
        throw std::runtime_error("Error while connecting to " + address + ":" + boost::lexical_cast<std::string>(port) + ", " + std::string(e.what()));
86
    }
91
    }
87
   
92
   
88
    this->Read();
93
    this->Read();
89
}
94
}
90
 
95
 
91
void TcpClient::Disconnect()
96
void TcpClient::Disconnect()
92
{
97
{
-
 
98
    if (this->socket_.is_open())
-
 
99
    {
-
 
100
        this->socket_.cancel();
93
    this->socket_.close();
101
        this->socket_.close();
-
 
102
    }
94
   
103
   
95
    if (this->acceptor_.use_count() != 0)
104
    if (this->acceptor_.use_count() != 0)
96
    {
105
    {
-
 
106
        this->acceptor_->cancel();
97
        this->acceptor_->close();
107
        this->acceptor_->close();
98
    }
108
    }
99
   
109
   
100
    Client::Disconnect();
110
    Client::Disconnect();
101
}
111
}
102
 
112
 
103
void TcpClient::Send(Buffer data)
113
void TcpClient::Send(Buffer data)
104
{
114
{
105
    if (this->acceptor_.use_count() != 0)
115
    if (this->acceptor_.use_count() != 0)
106
    {
116
    {
107
        return;
117
        return;
108
    }
118
    }
109
   
119
   
110
    if (this->socket_.is_open())
120
    if (this->socket_.is_open())
111
    {
121
    {
112
        this->socket_.send(boost::asio::buffer(data));
122
        this->socket_.send(boost::asio::buffer(data));
113
    }
123
    }
114
    else
124
    else
115
    {
125
    {
116
        this->Disconnect();
126
        this->Disconnect();
117
    }
127
    }
118
}
128
}
119
 
129
 
120
void TcpClient::Read()
130
void TcpClient::Read()
121
{
131
{
122
    Client::Read();
132
    Client::Read();
123
   
133
   
124
    this->socket_.async_read_some(boost::asio::buffer(this->buffer_),
134
    this->socket_.async_read_some(boost::asio::buffer(this->buffer_),
125
                                  boost::bind(&TcpClient::ReadHandler,
135
                                  boost::bind(&TcpClient::ReadHandler,
126
                                              this,
136
                                              this,
127
                                              boost::asio::placeholders::error,
137
                                              boost::asio::placeholders::error,
128
                                              boost::asio::placeholders::bytes_transferred));
138
                                              boost::asio::placeholders::bytes_transferred));
129
}
139
}
130
 
140
 
131
   
141
   
132
}; // namespace net
142
}; // namespace net
133
}; // namespace atom
143
}; // namespace atom
134
 
144