Rev 1971 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1971 | 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 "Client.h" |
21 | #include "Client.h" |
| 22 | 22 | ||
| 23 | #include <iostream> |
23 | #include <iostream> |
| 24 | 24 | ||
| 25 | #include <boost/bind.hpp> |
25 | #include <boost/bind.hpp> |
| 26 | #include <boost/lexical_cast.hpp> |
26 | #include <boost/lexical_cast.hpp> |
| 27 | 27 | ||
| 28 | #include "common/log.h" |
28 | #include "common/log.h" |
| 29 | 29 | ||
| 30 | namespace atom { |
30 | namespace atom { |
| 31 | namespace net { |
31 | namespace net { |
| 32 | 32 | ||
| 33 | static const std::string log_module_ = "net::client"; |
33 | static const std::string log_module_ = "net::client"; |
| 34 | 34 | ||
| 35 | Client::Client(boost::asio::io_service& io_service, SocketId id, SocketId server_id) : buffer_(2048), io_service_(io_service) |
35 | Client::Client(boost::asio::io_service& io_service, SocketId id, SocketId server_id) : buffer_(2048), io_service_(io_service) |
| 36 | { |
36 | { |
| 37 | LOG_DEBUG_ENTER; |
37 | LOG_DEBUG_ENTER; |
| 38 | 38 | ||
| 39 | this->server_id_ = server_id; |
39 | this->server_id_ = server_id; |
| 40 | this->id_ = id; |
40 | this->id_ = id; |
| 41 | 41 | ||
| 42 | LOG_DEBUG_EXIT; |
42 | LOG_DEBUG_EXIT; |
| 43 | } |
43 | } |
| 44 | 44 | ||
| 45 | Client::~Client() |
45 | Client::~Client() |
| 46 | { |
46 | { |
| 47 | LOG_DEBUG_ENTER; |
47 | LOG_DEBUG_ENTER; |
| 48 | 48 | ||
| 49 | this->Stop(); |
49 | this->Stop(); |
| 50 | 50 | ||
| 51 | LOG_DEBUG_EXIT; |
51 | LOG_DEBUG_EXIT; |
| 52 | } |
52 | } |
| 53 | 53 | ||
| 54 | void Client::ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state, const SignalOnNewData::slot_type& slot_on_new_data) |
54 | void Client::ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state, const SignalOnNewData::slot_type& slot_on_new_data) |
| 55 | { |
55 | { |
| 56 | LOG_DEBUG_ENTER; |
56 | LOG_DEBUG_ENTER; |
| 57 | 57 | ||
| 58 | this->signal_on_new_state_.connect(slot_on_new_state); |
58 | this->signal_on_new_state_.connect(slot_on_new_state); |
| 59 | this->signal_on_new_data_.connect(slot_on_new_data); |
59 | this->signal_on_new_data_.connect(slot_on_new_data); |
| 60 | 60 | ||
| 61 | LOG_DEBUG_EXIT; |
61 | LOG_DEBUG_EXIT; |
| 62 | } |
62 | } |
| 63 | 63 | ||
| 64 | SocketId Client::GetId() |
64 | SocketId Client::GetId() |
| 65 | { |
65 | { |
| 66 | LOG_DEBUG_ENTER; |
66 | LOG_DEBUG_ENTER; |
| 67 | LOG_DEBUG_EXIT; |
67 | LOG_DEBUG_EXIT; |
| 68 | 68 | ||
| 69 | return this->id_; |
69 | return this->id_; |
| 70 | } |
70 | } |
| 71 | 71 | ||
| 72 | SocketId Client::GetServerId() |
72 | SocketId Client::GetServerId() |
| 73 | { |
73 | { |
| 74 | LOG_DEBUG_ENTER; |
74 | LOG_DEBUG_ENTER; |
| 75 | LOG_DEBUG_EXIT; |
75 | LOG_DEBUG_EXIT; |
| 76 | 76 | ||
| 77 | return this->server_id_; |
77 | return this->server_id_; |
| 78 | } |
78 | } |
| 79 | 79 | ||
| 80 | void Client::Read() |
80 | void Client::Read() |
| 81 | { |
81 | { |
| 82 | LOG_DEBUG_ENTER; |
82 | LOG_DEBUG_ENTER; |
| 83 | 83 | ||
| - | 84 | common::Byteset empty_vector(8192); |
|
| 84 | this->buffer_. |
85 | this->buffer_.swap(empty_vector); |
| 85 | 86 | ||
| 86 | LOG_DEBUG_EXIT; |
87 | LOG_DEBUG_EXIT; |
| 87 | } |
88 | } |
| 88 | 89 | ||
| 89 | void Client::ReadHandler(const boost::system::error_code& error, size_t size) |
90 | void Client::ReadHandler(const boost::system::error_code& error, size_t size) |
| 90 | { |
91 | { |
| 91 | LOG_DEBUG_ENTER; |
92 | LOG_DEBUG_ENTER; |
| 92 | 93 | ||
| 93 | 94 | ||
| 94 | if (error) |
95 | if (error) |
| 95 | { |
96 | { |
| 96 | log::Debug(log_module_, "error %d", error); |
97 | log::Debug(log_module_, "error %d", error); |
| 97 | 98 | ||
| 98 | if (boost::system::errc::operation_canceled != error) |
99 | if (boost::system::errc::operation_canceled != error) |
| 99 | { |
100 | { |
| 100 | this->Disconnect(); |
101 | this->Disconnect(); |
| 101 | } |
102 | } |
| 102 | } |
103 | } |
| 103 | else if (size == 0) |
104 | else if (size == 0) |
| 104 | { |
105 | { |
| 105 | log::Debug(log_module_, "size 0"); |
106 | log::Debug(log_module_, "size 0"); |
| 106 | 107 | ||
| 107 | this->Disconnect(); |
108 | this->Disconnect(); |
| 108 | } |
109 | } |
| 109 | else |
110 | else |
| 110 | { |
111 | { |
| 111 |
|
112 | log::Debug(log_module_, "size was %u", size); |
| 112 | 113 | ||
| 113 | this->signal_on_new_data_(this->id_, this->server_id_, this |
114 | this->signal_on_new_data_(this->id_, this->server_id_, common::Byteset(this->buffer_.begin(), this->buffer_.begin() + size)); |
| 114 | 115 | ||
| 115 | this->Read(); |
116 | this->Read(); |
| 116 | } |
117 | } |
| 117 | 118 | ||
| 118 | LOG_DEBUG_EXIT; |
119 | LOG_DEBUG_EXIT; |
| 119 | } |
120 | } |
| 120 | 121 | ||
| 121 | void Client::Stop() |
122 | void Client::Stop() |
| 122 | { |
123 | { |
| 123 | LOG_DEBUG_ENTER; |
124 | LOG_DEBUG_ENTER; |
| 124 | LOG_DEBUG_EXIT; |
125 | LOG_DEBUG_EXIT; |
| 125 | } |
126 | } |
| 126 | 127 | ||
| 127 | void Client::Disconnect() |
128 | void Client::Disconnect() |
| 128 | { |
129 | { |
| 129 | LOG_DEBUG_ENTER; |
130 | LOG_DEBUG_ENTER; |
| 130 | 131 | ||
| 131 | if (this->id_ != 0) |
132 | if (this->id_ != 0) |
| 132 | { |
133 | { |
| 133 | SocketId id = this->id_; |
134 | SocketId id = this->id_; |
| 134 | 135 | ||
| 135 | this->Stop(); |
136 | this->Stop(); |
| 136 | 137 | ||
| 137 | this->id_ = 0; |
138 | this->id_ = 0; |
| 138 | 139 | ||
| 139 | this->signal_on_new_state_(id, this->server_id_, CLIENT_STATE_DISCONNECTED); |
140 | this->signal_on_new_state_(id, this->server_id_, CLIENT_STATE_DISCONNECTED); |
| 140 | } |
141 | } |
| 141 | 142 | ||
| 142 | LOG_DEBUG_EXIT; |
143 | LOG_DEBUG_EXIT; |
| 143 | } |
144 | } |
| 144 | 145 | ||
| 145 | }; // namespace net |
146 | }; // namespace net |
| 146 | }; // namespace atom |
147 | }; // namespace atom |
| 147 | 148 | ||