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