Rev 1962 | Rev 1971 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1962 | Rev 1963 | ||
|---|---|---|---|
| Line 22... | Line 22... | ||
| 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 | ||
| - | 28 | #include "common/log.h" |
|
| 27 | 29 | ||
| 28 | namespace atom { |
30 | namespace atom { |
| 29 | namespace net { |
31 | namespace net { |
| - | 32 | ||
| - | 33 | static const std::string log_module_ = "net::client"; |
|
| 30 | 34 | ||
| 31 | 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) |
| 32 | { |
36 | { |
| - | 37 | LOG_DEBUG_ENTER; |
|
| - | 38 | ||
| 33 | this->server_id_ = server_id; |
39 | this->server_id_ = server_id; |
| 34 | this->id_ = id; |
40 | this->id_ = id; |
| - | 41 | ||
| - | 42 | LOG_DEBUG_EXIT; |
|
| 35 | } |
43 | } |
| 36 | 44 | ||
| 37 | Client::~Client() |
45 | Client::~Client() |
| 38 | { |
46 | { |
| - | 47 | LOG_DEBUG_ENTER; |
|
| - | 48 | ||
| 39 | this->Stop(); |
49 | this->Stop(); |
| - | 50 | ||
| - | 51 | LOG_DEBUG_EXIT; |
|
| 40 | } |
52 | } |
| 41 | 53 | ||
| 42 | 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) |
| 43 | { |
55 | { |
| - | 56 | LOG_DEBUG_ENTER; |
|
| - | 57 | ||
| 44 | this->signal_on_new_state_.connect(slot_on_new_state); |
58 | this->signal_on_new_state_.connect(slot_on_new_state); |
| 45 | this->signal_on_new_data_.connect(slot_on_new_data); |
59 | this->signal_on_new_data_.connect(slot_on_new_data); |
| - | 60 | ||
| - | 61 | LOG_DEBUG_EXIT; |
|
| 46 | } |
62 | } |
| 47 | 63 | ||
| 48 | SocketId Client::GetId() |
64 | SocketId Client::GetId() |
| 49 | { |
65 | { |
| - | 66 | LOG_DEBUG_ENTER; |
|
| - | 67 | LOG_DEBUG_EXIT; |
|
| - | 68 | ||
| 50 | return this->id_; |
69 | return this->id_; |
| 51 | } |
70 | } |
| 52 | 71 | ||
| 53 | SocketId Client::GetServerId() |
72 | SocketId Client::GetServerId() |
| 54 | { |
73 | { |
| - | 74 | LOG_DEBUG_ENTER; |
|
| - | 75 | LOG_DEBUG_EXIT; |
|
| - | 76 | ||
| 55 | return this->server_id_; |
77 | return this->server_id_; |
| 56 | } |
78 | } |
| 57 | 79 | ||
| 58 | void Client::Read() |
80 | void Client::Read() |
| 59 | { |
81 | { |
| - | 82 | LOG_DEBUG_ENTER; |
|
| - | 83 | ||
| 60 | this->buffer_.Clear(); |
84 | this->buffer_.Clear(); |
| - | 85 | ||
| - | 86 | LOG_DEBUG_EXIT; |
|
| 61 | } |
87 | } |
| 62 | 88 | ||
| 63 | void Client::ReadHandler(const boost::system::error_code& error, size_t size) |
89 | void Client::ReadHandler(const boost::system::error_code& error, size_t size) |
| 64 | { |
90 | { |
| - | 91 | LOG_DEBUG_ENTER; |
|
| - | 92 | ||
| 65 | if (error) |
93 | if (error) |
| 66 | { |
94 | { |
| 67 | this->Disconnect(); |
95 | this->Disconnect(); |
| 68 | } |
96 | } |
| 69 | else if (size == 0) |
97 | else if (size == 0) |
| 70 | { |
98 | { |
| 71 | this->Disconnect(); |
99 | this->Disconnect(); |
| 72 | } |
100 | } |
| 73 | else |
101 | else |
| 74 | { |
102 | { |
| 75 | this->buffer_.SetSize(size); |
103 | this->buffer_.SetSize(size); |
| 76 | 104 | ||
| 77 | this->signal_on_new_data_(this->id_, this->server_id_, this->buffer_); |
105 | this->signal_on_new_data_(this->id_, this->server_id_, this->buffer_); |
| 78 | 106 | ||
| 79 | this->Read(); |
107 | this->Read(); |
| 80 | } |
108 | } |
| - | 109 | ||
| - | 110 | LOG_DEBUG_EXIT; |
|
| 81 | } |
111 | } |
| 82 | 112 | ||
| 83 | void Client::Stop() |
113 | void Client::Stop() |
| 84 | { |
114 | { |
| - | 115 | LOG_DEBUG_ENTER; |
|
| - | 116 | LOG_DEBUG_EXIT; |
|
| 85 | } |
117 | } |
| 86 | 118 | ||
| 87 | void Client::Disconnect() |
119 | void Client::Disconnect() |
| 88 | { |
120 | { |
| - | 121 | LOG_DEBUG_ENTER; |
|
| - | 122 | ||
| 89 | if (this->id_ != 0) |
123 | if (this->id_ != 0) |
| 90 | { |
124 | { |
| 91 | SocketId id = this->id_; |
125 | SocketId id = this->id_; |
| 92 | 126 | ||
| 93 | this->Stop(); |
127 | this->Stop(); |
| 94 | 128 | ||
| 95 | this->id_ = 0; |
129 | this->id_ = 0; |
| 96 | 130 | ||
| 97 | this->signal_on_new_state_(id, this->server_id_, CLIENT_STATE_DISCONNECTED); |
131 | this->signal_on_new_state_(id, this->server_id_, CLIENT_STATE_DISCONNECTED); |
| 98 | } |
132 | } |
| - | 133 | ||
| - | 134 | LOG_DEBUG_EXIT; |
|
| 99 | } |
135 | } |
| 100 | 136 | ||
| 101 | }; // namespace net |
137 | }; // namespace net |
| 102 | }; // namespace atom |
138 | }; // namespace atom |