Rev 1962 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed
| Rev 1962 | Rev 1971 | ||
|---|---|---|---|
| 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 "TcpServer.h" |
21 | #include "TcpServer.h" |
| 22 | 22 | ||
| 23 | #include "common/log.h" |
23 | #include "common/log.h" |
| 24 | 24 | ||
| 25 | namespace atom { |
25 | namespace atom { |
| 26 | namespace net { |
26 | namespace net { |
| 27 | 27 | ||
| 28 | static const std::string log_module_ = "net::tcpserver"; |
28 | static const std::string log_module_ = "net::tcpserver"; |
| 29 | 29 | ||
| 30 | TcpServer::TcpServer(boost::asio::io_service& io_service, unsigned int port, SocketId id) : acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)) |
30 | TcpServer::TcpServer(boost::asio::io_service& io_service, unsigned int port, SocketId id) : acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)) |
| 31 | { |
31 | { |
| 32 | LOG_DEBUG_ENTER; |
32 | LOG_DEBUG_ENTER; |
| 33 | 33 | ||
| 34 | this->port_ = port; |
34 | this->port_ = port; |
| 35 | this->id_ = id; |
35 | this->id_ = id; |
| 36 | 36 | ||
| 37 | LOG_DEBUG_EXIT; |
37 | LOG_DEBUG_EXIT; |
| 38 | } |
38 | } |
| 39 | 39 | ||
| 40 | TcpServer::~TcpServer() |
40 | TcpServer::~TcpServer() |
| 41 | { |
41 | { |
| 42 | LOG_DEBUG_ENTER; |
42 | LOG_DEBUG_ENTER; |
| 43 | 43 | ||
| 44 | if (this->new_client_socket_.use_count() != 0) |
44 | if (this->new_client_socket_.use_count() != 0) |
| 45 | { |
45 | { |
| 46 | if (this->new_client_socket_->is_open()) |
46 | if (this->new_client_socket_->is_open()) |
| 47 | { |
47 | { |
| 48 | this->new_client_socket_->cancel(); |
48 | this->new_client_socket_->cancel(); |
| 49 | this->new_client_socket_->close(); |
49 | this->new_client_socket_->close(); |
| 50 | } |
50 | } |
| 51 | 51 | ||
| 52 | this->new_client_socket_.reset(); |
52 | this->new_client_socket_.reset(); |
| 53 | } |
53 | } |
| 54 | 54 | ||
| 55 | this->acceptor_.cancel(); |
55 | this->acceptor_.cancel(); |
| 56 | this->acceptor_.close(); |
56 | this->acceptor_.close(); |
| 57 | 57 | ||
| 58 | LOG_DEBUG_EXIT; |
58 | LOG_DEBUG_EXIT; |
| 59 | } |
59 | } |
| 60 | 60 | ||
| 61 | void TcpServer::Accept() |
61 | void TcpServer::Accept() |
| 62 | { |
62 | { |
| 63 | LOG_DEBUG_ENTER; |
63 | LOG_DEBUG_ENTER; |
| 64 | 64 | ||
| 65 | this->new_client_socket_ = TcpSocketPointer(new boost::asio::ip::tcp::socket(this->acceptor_.get_io_service())); |
65 | this->new_client_socket_ = TcpSocketPointer(new boost::asio::ip::tcp::socket(this->acceptor_.get_io_service())); |
| 66 | 66 | ||
| 67 | this->acceptor_.async_accept(*this->new_client_socket_, |
67 | this->acceptor_.async_accept(*this->new_client_socket_, |
| 68 | boost::bind(&TcpServer::AcceptHandler, |
68 | boost::bind(&TcpServer::AcceptHandler, |
| 69 | this, |
69 | this, |
| 70 | boost::asio::placeholders::error)); |
70 | boost::asio::placeholders::error)); |
| 71 | 71 | ||
| 72 | LOG_DEBUG_EXIT; |
72 | LOG_DEBUG_EXIT; |
| 73 | } |
73 | } |
| 74 | 74 | ||
| 75 | void TcpServer::AcceptHandler(const boost::system::error_code& error) |
75 | void TcpServer::AcceptHandler(const boost::system::error_code& error) |
| 76 | { |
76 | { |
| 77 | LOG_DEBUG_ENTER; |
77 | LOG_DEBUG_ENTER; |
| 78 | 78 | ||
| 79 | this->signal_on_new_client_(this->id_, this->new_client_socket_); |
79 | this->signal_on_new_client_(this->id_, this->new_client_socket_); |
| 80 | 80 | ||
| 81 | this->new_client_socket_.reset(); |
81 | this->new_client_socket_.reset(); |
| - | 82 | ||
| - | 83 | this->Accept(); |
|
| 82 | 84 | ||
| 83 | LOG_DEBUG_EXIT; |
85 | LOG_DEBUG_EXIT; |
| 84 | } |
86 | } |
| 85 | 87 | ||
| 86 | SocketId TcpServer::GetId() |
88 | SocketId TcpServer::GetId() |
| 87 | { |
89 | { |
| 88 | LOG_DEBUG_ENTER; |
90 | LOG_DEBUG_ENTER; |
| 89 | 91 | ||
| 90 | LOG_DEBUG_EXIT; |
92 | LOG_DEBUG_EXIT; |
| 91 | 93 | ||
| 92 | return this->id_; |
94 | return this->id_; |
| 93 | } |
95 | } |
| 94 | 96 | ||
| 95 | void TcpServer::ConnectSlots(const SignalOnNewClient::slot_type& slot_on_new_client) |
97 | void TcpServer::ConnectSlots(const SignalOnNewClient::slot_type& slot_on_new_client) |
| 96 | { |
98 | { |
| 97 | LOG_DEBUG_ENTER; |
99 | LOG_DEBUG_ENTER; |
| 98 | 100 | ||
| 99 | this->signal_on_new_client_.connect(slot_on_new_client); |
101 | this->signal_on_new_client_.connect(slot_on_new_client); |
| 100 | 102 | ||
| 101 | LOG_DEBUG_EXIT; |
103 | LOG_DEBUG_EXIT; |
| 102 | } |
104 | } |
| 103 | 105 | ||
| 104 | 106 | ||
| 105 | }; // namespace net |
107 | }; // namespace net |
| 106 | }; // namespace atom |
108 | }; // namespace atom |
| 107 | 109 | ||