Rev 1596 | Rev 1608 | 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 "Client.h" |
||
| 22 | |||
| 23 | #include <boost/bind.hpp> |
||
| 24 | #include <boost/lexical_cast.hpp> |
||
| 25 | |||
| 26 | namespace atom { |
||
| 27 | namespace net { |
||
| 28 | |||
| 1596 | runge | 29 | Client::Client(boost::asio::io_service& io_service, ClientId id, ServerId server_id) : buffer_(2048) |
| 1592 | runge | 30 | { |
| 31 | this->server_id_ = server_id; |
||
| 1593 | runge | 32 | this->id_ = id; |
| 1592 | runge | 33 | } |
| 34 | |||
| 35 | Client::~Client() |
||
| 36 | { |
||
| 1599 | runge | 37 | this->Stop(); |
| 1592 | runge | 38 | } |
| 39 | |||
| 40 | void Client::ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state, const SignalOnNewData::slot_type& slot_on_new_data) |
||
| 41 | { |
||
| 42 | this->signal_on_new_state_.connect(slot_on_new_state); |
||
| 43 | this->signal_on_new_data_.connect(slot_on_new_data); |
||
| 44 | } |
||
| 45 | |||
| 46 | ClientId Client::GetId() |
||
| 47 | { |
||
| 1593 | runge | 48 | return this->id_; |
| 1592 | runge | 49 | } |
| 50 | |||
| 51 | ServerId Client::GetServerId() |
||
| 52 | { |
||
| 53 | return this->server_id_; |
||
| 54 | } |
||
| 55 | |||
| 56 | void Client::Read() |
||
| 57 | { |
||
| 1596 | runge | 58 | this->buffer_.Clear(); |
| 1592 | runge | 59 | } |
| 60 | |||
| 61 | void Client::ReadHandler(const boost::system::error_code& error, size_t size) |
||
| 62 | { |
||
| 63 | if (error) |
||
| 64 | { |
||
| 1595 | runge | 65 | //std::cout << error.message() << std::endl; |
| 1592 | runge | 66 | } |
| 67 | else if (size == 0) |
||
| 68 | { |
||
| 69 | this->Disconnect(); |
||
| 70 | } |
||
| 71 | else |
||
| 72 | { |
||
| 1596 | runge | 73 | this->buffer_.SetSize(size); |
| 74 | |||
| 1593 | runge | 75 | this->signal_on_new_data_(this->id_, this->server_id_, this->buffer_); |
| 1592 | runge | 76 | |
| 77 | this->Read(); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 1599 | runge | 81 | void Client::Stop() |
| 82 | { |
||
| 83 | } |
||
| 84 | |||
| 1592 | runge | 85 | void Client::Disconnect() |
| 86 | { |
||
| 1599 | runge | 87 | this->Stop(); |
| 1593 | runge | 88 | this->signal_on_new_state_(this->id_, this->server_id_, CLIENT_STATE_DISCONNECTED); |
| 1592 | runge | 89 | } |
| 90 | |||
| 91 | }; // namespace net |
||
| 92 | }; // namespace atom |