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