Subversion Repositories HomeAutomation

Rev

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

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