Rev 1962 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1962 | Rev 1987 | ||
|---|---|---|---|
| 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 | #include "common/log.h" |
25 | #include "common/log.h" |
| 26 | 26 | ||
| 27 | namespace atom { |
27 | namespace atom { |
| 28 | namespace net { |
28 | namespace net { |
| 29 | 29 | ||
| 30 | static const std::string log_module_ = "net::tcpclient"; |
30 | static const std::string log_module_ = "net::tcpclient"; |
| 31 | 31 | ||
| 32 | TcpClient::TcpClient(boost::asio::io_service& io_service, SocketId id, SocketId server_id) : Client(io_service, id, server_id) |
32 | TcpClient::TcpClient(boost::asio::io_service& io_service, SocketId id, SocketId server_id) : Client(io_service, id, server_id) |
| 33 | { |
33 | { |
| 34 | LOG_DEBUG_ENTER; |
34 | LOG_DEBUG_ENTER; |
| 35 | 35 | ||
| 36 | LOG_DEBUG_EXIT; |
36 | LOG_DEBUG_EXIT; |
| 37 | } |
37 | } |
| 38 | 38 | ||
| 39 | TcpClient::TcpClient(boost::asio::io_service& io_service, TcpSocketPointer socket, SocketId id, SocketId server_id) : Client(io_service, id, server_id) |
39 | TcpClient::TcpClient(boost::asio::io_service& io_service, TcpSocketPointer socket, SocketId id, SocketId server_id) : Client(io_service, id, server_id) |
| 40 | { |
40 | { |
| 41 | LOG_DEBUG_ENTER; |
41 | LOG_DEBUG_ENTER; |
| 42 | 42 | ||
| 43 | this->socket_ = socket; |
43 | this->socket_ = socket; |
| 44 | 44 | ||
| 45 | this->Read(); |
45 | this->Read(); |
| 46 | 46 | ||
| 47 | LOG_DEBUG_EXIT; |
47 | LOG_DEBUG_EXIT; |
| 48 | } |
48 | } |
| 49 | 49 | ||
| 50 | TcpClient::~TcpClient() |
50 | TcpClient::~TcpClient() |
| 51 | { |
51 | { |
| 52 | LOG_DEBUG_ENTER; |
52 | LOG_DEBUG_ENTER; |
| 53 | 53 | ||
| 54 | LOG_DEBUG_EXIT; |
54 | LOG_DEBUG_EXIT; |
| 55 | } |
55 | } |
| 56 | 56 | ||
| 57 | void TcpClient::Connect(std::string address, unsigned int port) |
57 | void TcpClient::Connect(std::string address, unsigned int port) |
| 58 | { |
58 | { |
| 59 | LOG_DEBUG_ENTER; |
59 | LOG_DEBUG_ENTER; |
| 60 | 60 | ||
| 61 | this->socket_ = TcpSocketPointer(new boost::asio::ip::tcp::socket(this->io_service_)); |
61 | this->socket_ = TcpSocketPointer(new boost::asio::ip::tcp::socket(this->io_service_)); |
| 62 | 62 | ||
| 63 | boost::asio::ip::tcp::resolver resolver(this->socket_->get_io_service()); |
63 | boost::asio::ip::tcp::resolver resolver(this->socket_->get_io_service()); |
| 64 | boost::asio::ip::tcp::resolver::query query(boost::asio::ip::tcp::v4(), address, boost::lexical_cast<std::string>(port)); |
64 | boost::asio::ip::tcp::resolver::query query(boost::asio::ip::tcp::v4(), address, boost::lexical_cast<std::string>(port)); |
| 65 | boost::asio::ip::tcp::resolver::iterator it = resolver.resolve(query); |
65 | boost::asio::ip::tcp::resolver::iterator it = resolver.resolve(query); |
| 66 | 66 | ||
| 67 | this->socket_->connect(*it); |
67 | this->socket_->connect(*it); |
| 68 | 68 | ||
| 69 | this->Read(); |
69 | this->Read(); |
| 70 | 70 | ||
| 71 | LOG_DEBUG_EXIT; |
71 | LOG_DEBUG_EXIT; |
| 72 | } |
72 | } |
| 73 | 73 | ||
| 74 | void TcpClient::Send(common::Byteset data) |
74 | void TcpClient::Send(common::Byteset data) |
| 75 | { |
75 | { |
| 76 | LOG_DEBUG_ENTER; |
76 | LOG_DEBUG_ENTER; |
| - | 77 | ||
| - | 78 | log::Debug(log_module_, "data=\"%s\", size=%u", std::string(data.begin(), data.end()).data(), data.size()); |
|
| 77 | 79 | ||
| 78 | try |
80 | try |
| 79 | { |
81 | { |
| 80 | if (this->socket_->is_open()) |
82 | if (this->socket_->is_open()) |
| 81 | { |
83 | { |
| 82 | this->socket_->send(boost::asio::buffer(data. |
84 | this->socket_->send(boost::asio::buffer(data.data(), data.size())); |
| 83 | } |
85 | } |
| 84 | } |
86 | } |
| 85 | catch (std::exception& e) |
87 | catch (std::exception& e) |
| 86 | { |
88 | { |
| 87 | this->Disconnect(); |
89 | this->Disconnect(); |
| 88 | //throw std::runtime_error(e.what()); |
90 | //throw std::runtime_error(e.what()); |
| 89 | } |
91 | } |
| 90 | 92 | ||
| 91 | LOG_DEBUG_EXIT; |
93 | LOG_DEBUG_EXIT; |
| 92 | } |
94 | } |
| 93 | 95 | ||
| 94 | void TcpClient::Read() |
96 | void TcpClient::Read() |
| 95 | { |
97 | { |
| 96 | LOG_DEBUG_ENTER; |
98 | LOG_DEBUG_ENTER; |
| 97 | 99 | ||
| 98 | Client::Read(); |
100 | Client::Read(); |
| 99 | 101 | ||
| - | 102 | log::Debug(log_module_, "this->buffer_.capacity()=%u", this->buffer_.capacity()); |
|
| - | 103 | ||
| 100 | this->socket_->async_read_some(boost::asio::buffer(this->buffer_. |
104 | this->socket_->async_read_some(boost::asio::buffer(this->buffer_.data(), this->buffer_.capacity()), |
| 101 | boost::bind(&TcpClient::ReadHandler, |
105 | boost::bind(&TcpClient::ReadHandler, |
| 102 | this, |
106 | this, |
| 103 | boost::asio::placeholders::error, |
107 | boost::asio::placeholders::error, |
| 104 | boost::asio::placeholders::bytes_transferred)); |
108 | boost::asio::placeholders::bytes_transferred)); |
| 105 | 109 | ||
| 106 | LOG_DEBUG_EXIT; |
110 | LOG_DEBUG_EXIT; |
| 107 | } |
111 | } |
| 108 | 112 | ||
| 109 | 113 | ||
| 110 | }; // namespace net |
114 | }; // namespace net |
| 111 | }; // namespace atom |
115 | }; // namespace atom |
| 112 | 116 | ||