Rev 1959 | Rev 1987 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1959 | Rev 1962 | ||
|---|---|---|---|
| Line 19... | Line 19... | ||
| 19 | */ |
19 | */ |
| 20 | 20 | ||
| 21 | #include "TcpClient.h" |
21 | #include "TcpClient.h" |
| 22 | 22 | ||
| 23 | #include <boost/lexical_cast.hpp> |
23 | #include <boost/lexical_cast.hpp> |
| - | 24 | ||
| - | 25 | #include "common/log.h" |
|
| 24 | 26 | ||
| 25 | namespace atom { |
27 | namespace atom { |
| 26 | namespace net { |
28 | namespace net { |
| 27 | 29 | ||
| 28 |
|
30 | static const std::string log_module_ = "net::tcpclient"; |
| 29 | 31 | ||
| 30 | TcpClient::TcpClient(boost::asio::io_service& io_service, SocketId id, SocketId server_id) : Client(io_service, id, server_id |
32 | TcpClient::TcpClient(boost::asio::io_service& io_service, SocketId id, SocketId server_id) : Client(io_service, id, server_id) |
| - | 33 | { |
|
| - | 34 | LOG_DEBUG_ENTER; |
|
| - | 35 | ||
| - | 36 | LOG_DEBUG_EXIT; |
|
| - | 37 | } |
|
| - | 38 | ||
| - | 39 | TcpClient::TcpClient(boost::asio::io_service& io_service, TcpSocketPointer socket, SocketId id, SocketId server_id) : Client(io_service, id, server_id) |
|
| - | 40 | { |
|
| - | 41 | LOG_DEBUG_ENTER; |
|
| - | 42 | ||
| - | 43 | this->socket_ = socket; |
|
| - | 44 | ||
| - | 45 | this->Read(); |
|
| - | 46 | ||
| - | 47 | LOG_DEBUG_EXIT; |
|
| - | 48 | } |
|
| - | 49 | ||
| - | 50 | TcpClient::~TcpClient() |
|
| 31 | { |
51 | { |
| - | 52 | LOG_DEBUG_ENTER; |
|
| - | 53 | ||
| - | 54 | LOG_DEBUG_EXIT; |
|
| 32 | } |
55 | } |
| 33 | 56 | ||
| 34 | TcpClient::~TcpClient() |
- | |
| 35 | { |
- | |
| 36 | } |
- | |
| 37 | - | ||
| 38 | void TcpClient:: |
57 | void TcpClient::Connect(std::string address, unsigned int port) |
| 39 | { |
- | |
| 40 | try |
- | |
| 41 | { |
- | |
| 42 | this->acceptor_ = acceptor; |
- | |
| 43 | - | ||
| 44 | this->acceptor_->async_accept(this->socket_, |
- | |
| 45 | boost::bind(&TcpClient::AcceptHandler, |
- | |
| 46 | this, |
- | |
| 47 | boost::asio::placeholders::error)); |
- | |
| 48 | } |
- | |
| 49 | catch (std::exception e) |
- | |
| 50 | { |
- | |
| 51 | throw std::runtime_error("Error while opening port, " + std::string(e.what())); |
- | |
| 52 | } |
- | |
| 53 | } |
- | |
| 54 | - | ||
| 55 | void TcpClient::AcceptHandler(const boost::system::error_code& error) |
- | |
| 56 | { |
58 | { |
| 57 |
|
59 | LOG_DEBUG_ENTER; |
| 58 | 60 | ||
| 59 |
|
61 | this->socket_ = TcpSocketPointer(new boost::asio::ip::tcp::socket(this->io_service_)); |
| 60 | } |
- | |
| 61 | 62 | ||
| 62 |
|
63 | boost::asio::ip::tcp::resolver resolver(this->socket_->get_io_service()); |
| 63 | { |
- | |
| 64 |
|
64 | boost::asio::ip::tcp::resolver::query query(boost::asio::ip::tcp::v4(), address, boost::lexical_cast<std::string>(port)); |
| 65 |
|
65 | boost::asio::ip::tcp::resolver::iterator it = resolver.resolve(query); |
| 66 | return acceptor; |
- | |
| 67 | } |
- | |
| 68 | 66 | ||
| 69 | void TcpClient::Connect(std::string address, unsigned int port) |
- | |
| 70 | { |
- | |
| 71 | try |
- | |
| 72 | { |
- | |
| 73 | boost::asio::ip::tcp::resolver resolver(this->socket_.get_io_service()); |
- | |
| 74 | boost::asio::ip::tcp::resolver::query query(boost::asio::ip::tcp::v4(), address, boost::lexical_cast<std::string>(port)); |
- | |
| 75 | boost::asio::ip::tcp::resolver::iterator it = resolver.resolve(query); |
- | |
| 76 | - | ||
| 77 |
|
67 | this->socket_->connect(*it); |
| 78 | } |
- | |
| 79 | catch (std::exception e) |
- | |
| 80 | { |
- | |
| 81 | throw std::runtime_error("Error while connecting to " + address + ":" + boost::lexical_cast<std::string>(port)); |
- | |
| 82 | } |
- | |
| 83 | - | ||
| 84 | this->Read(); |
- | |
| 85 | } |
- | |
| 86 | 68 | ||
| 87 | void TcpClient::Stop() |
- | |
| 88 | { |
- | |
| 89 | if (this->socket_.is_open()) |
- | |
| 90 | { |
- | |
| 91 |
|
69 | this->Read(); |
| 92 | this->socket_.close(); |
- | |
| 93 | } |
- | |
| 94 | 70 | ||
| 95 | if (this->acceptor_.use_count() != 0) |
- | |
| 96 | { |
- | |
| 97 | this->acceptor_->cancel(); |
- | |
| 98 |
|
71 | LOG_DEBUG_EXIT; |
| 99 | } |
- | |
| 100 | } |
72 | } |
| 101 | 73 | ||
| 102 | void TcpClient::Send(common::Byteset data) |
74 | void TcpClient::Send(common::Byteset data) |
| 103 | { |
75 | { |
| 104 | if (this->acceptor_.use_count() != 0) |
- | |
| 105 | { |
- | |
| 106 |
|
76 | LOG_DEBUG_ENTER; |
| 107 | } |
- | |
| 108 | 77 | ||
| 109 | try |
78 | try |
| 110 | { |
79 | { |
| 111 |
|
80 | if (this->socket_->is_open()) |
| 112 | { |
81 | { |
| 113 |
|
82 | this->socket_->send(boost::asio::buffer(data.Get(), data.GetMaxSize())); |
| 114 | } |
83 | } |
| 115 | } |
84 | } |
| 116 | catch (std::exception& e) |
85 | catch (std::exception& e) |
| 117 | { |
86 | { |
| 118 | this->Disconnect(); |
87 | this->Disconnect(); |
| 119 | //throw std::runtime_error(e.what()); |
88 | //throw std::runtime_error(e.what()); |
| 120 | } |
89 | } |
| - | 90 | ||
| - | 91 | LOG_DEBUG_EXIT; |
|
| 121 | } |
92 | } |
| 122 | 93 | ||
| 123 | void TcpClient::Read() |
94 | void TcpClient::Read() |
| 124 | { |
95 | { |
| - | 96 | LOG_DEBUG_ENTER; |
|
| - | 97 | ||
| 125 | Client::Read(); |
98 | Client::Read(); |
| 126 | 99 | ||
| 127 |
|
100 | this->socket_->async_read_some(boost::asio::buffer(this->buffer_.Get(), this->buffer_.GetMaxSize()), |
| 128 | boost::bind(&TcpClient::ReadHandler, |
101 | boost::bind(&TcpClient::ReadHandler, |
| 129 | this, |
102 | this, |
| 130 | boost::asio::placeholders::error, |
103 | boost::asio::placeholders::error, |
| 131 | boost::asio::placeholders::bytes_transferred)); |
104 | boost::asio::placeholders::bytes_transferred)); |
| - | 105 | ||
| - | 106 | LOG_DEBUG_EXIT; |
|
| 132 | } |
107 | } |
| 133 | 108 | ||
| 134 | 109 | ||
| 135 | }; // namespace net |
110 | }; // namespace net |
| 136 | }; // namespace atom |
111 | }; // namespace atom |