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