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