Subversion Repositories HomeAutomation

Rev

Rev 1962 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. /*
  2.  
  3.     Copyright (C) 2010  Mattias Runge
  4.  
  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
  7.     the Free Software Foundation; either version 2 of the License, or
  8.     (at your option) any later version.
  9.  
  10.     This program is distributed in the hope that it will be useful,
  11.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.     GNU General Public License for more details.
  14.  
  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.,
  17.     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18.  
  19. */
  20.  
  21. #include "TcpServer.h"
  22.  
  23. #include "common/log.h"
  24.  
  25. namespace atom {
  26. namespace net {
  27.  
  28. static const std::string log_module_ = "net::tcpserver";
  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))
  31. {
  32.   LOG_DEBUG_ENTER;
  33.  
  34.   this->port_ = port;
  35.   this->id_   = id;
  36.  
  37.   LOG_DEBUG_EXIT;
  38. }
  39.  
  40. TcpServer::~TcpServer()
  41. {
  42.   LOG_DEBUG_ENTER;
  43.  
  44.   if (this->new_client_socket_.use_count() != 0)
  45.   {
  46.     if (this->new_client_socket_->is_open())
  47.     {
  48.       this->new_client_socket_->cancel();
  49.       this->new_client_socket_->close();
  50.     }
  51.    
  52.     this->new_client_socket_.reset();
  53.   }
  54.    
  55.   this->acceptor_.cancel();
  56.   this->acceptor_.close();
  57.  
  58.   LOG_DEBUG_EXIT;
  59. }
  60.  
  61. void TcpServer::Accept()
  62. {
  63.   LOG_DEBUG_ENTER;
  64.  
  65.   this->new_client_socket_ = TcpSocketPointer(new boost::asio::ip::tcp::socket(this->acceptor_.get_io_service()));
  66.  
  67.   this->acceptor_.async_accept(*this->new_client_socket_,
  68.                                boost::bind(&TcpServer::AcceptHandler,
  69.                                            this,
  70.                                            boost::asio::placeholders::error));
  71.  
  72.   LOG_DEBUG_EXIT;
  73. }
  74.  
  75. void TcpServer::AcceptHandler(const boost::system::error_code& error)
  76. {
  77.   LOG_DEBUG_ENTER;
  78.  
  79.   this->signal_on_new_client_(this->id_, this->new_client_socket_);
  80.  
  81.   this->new_client_socket_.reset();
  82.  
  83.   this->Accept();
  84.  
  85.   LOG_DEBUG_EXIT;
  86. }
  87.  
  88. SocketId TcpServer::GetId()
  89. {
  90.   LOG_DEBUG_ENTER;
  91.  
  92.   LOG_DEBUG_EXIT;
  93.  
  94.   return this->id_;
  95. }
  96.  
  97. void TcpServer::ConnectSlots(const SignalOnNewClient::slot_type& slot_on_new_client)
  98. {
  99.   LOG_DEBUG_ENTER;
  100.  
  101.   this->signal_on_new_client_.connect(slot_on_new_client);
  102.  
  103.   LOG_DEBUG_EXIT;
  104. }
  105.  
  106.  
  107. }; // namespace net
  108. }; // namespace atom
  109.