Rev 1962 | Rev 1971 | Go to most recent revision | Show entire file | Ignore 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 |
|
39 | this->server_id_ = server_id; |
| 34 |
|
40 | this->id_ = id; |
| - | 41 | ||
| - | 42 | LOG_DEBUG_EXIT; |
|
| 35 | } |
43 | } |
| 36 | 44 | ||
| 37 | Client::~Client() |
45 | Client::~Client() |
| 38 | { |
46 | { |
| 39 |
|
47 | LOG_DEBUG_ENTER; |
| 40 |
|
48 | |
| 41 | 49 | this->Stop(); |
|
| 42 | void Client::ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state, const SignalOnNewData::slot_type& slot_on_new_data) |
- | |
| 43 |
|
50 | |
| 44 | this->signal_on_new_state_.connect(slot_on_new_state); |
- | |
| 45 |
|
51 | LOG_DEBUG_EXIT; |
| 46 | } |
52 | } |
| 47 | 53 | ||
| 48 |
|
54 | void Client::ConnectSlots(const SignalOnNewState::slot_type& slot_on_new_state, const SignalOnNewData::slot_type& slot_on_new_data) |
| 49 | { |
55 | { |
| 50 |
|
56 | LOG_DEBUG_ENTER; |
| - | 57 | ||
| - | 58 | this->signal_on_new_state_.connect(slot_on_new_state); |
|
| - | 59 | this->signal_on_new_data_.connect(slot_on_new_data); |
|
| - | 60 | ||
| - | 61 | LOG_DEBUG_EXIT; |
|
| 51 | } |
62 | } |
| 52 | 63 | ||
| 53 | SocketId Client:: |
64 | SocketId Client::GetId() |
| 54 | { |
65 | { |
| - | 66 | LOG_DEBUG_ENTER; |
|
| - | 67 | LOG_DEBUG_EXIT; |
|
| - | 68 | ||
| - | 69 | return this->id_; |
|
| - | 70 | } |
|
| - | 71 | ||
| - | 72 | SocketId Client::GetServerId() |
|
| - | 73 | { |
|
| - | 74 | LOG_DEBUG_ENTER; |
|
| - | 75 | LOG_DEBUG_EXIT; |
|
| - | 76 | ||
| 55 |
|
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 |
|
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 |
|
93 | if (error) |
| 66 |
|
94 | { |
| 67 |
|
95 | this->Disconnect(); |
| 68 |
|
96 | } |
| 69 |
|
97 | else if (size == 0) |
| 70 |
|
98 | { |
| 71 |
|
99 | this->Disconnect(); |
| 72 |
|
100 | } |
| 73 |
|
101 | else |
| 74 |
|
102 | { |
| 75 |
|
103 | this->buffer_.SetSize(size); |
| 76 | 104 | ||
| 77 |
|
105 | this->signal_on_new_data_(this->id_, this->server_id_, this->buffer_); |
| 78 | 106 | ||
| 79 |
|
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 |