|
TOBI Interface A
0.1
|
00001 /* 00002 This file is part of the TOBI Interface A (TiA) library. 00003 00004 Commercial Usage 00005 Licensees holding valid Graz University of Technology Commercial 00006 licenses may use this file in accordance with the Graz University 00007 of Technology Commercial License Agreement provided with the 00008 Software or, alternatively, in accordance with the terms contained in 00009 a written agreement between you and Graz University of Technology. 00010 00011 -------------------------------------------------- 00012 00013 GNU Lesser General Public License Usage 00014 Alternatively, this file may be used under the terms of the GNU Lesser 00015 General Public License version 3.0 as published by the Free Software 00016 Foundation and appearing in the file lgpl.txt included in the 00017 packaging of this file. Please review the following information to 00018 ensure the GNU General Public License version 3.0 requirements will be 00019 met: http://www.gnu.org/copyleft/lgpl.html. 00020 00021 In case of GNU Lesser General Public License Usage ,the TiA library 00022 is distributed in the hope that it will be useful, 00023 but WITHOUT ANY WARRANTY; without even the implied warranty of 00024 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00025 GNU General Public License for more details. 00026 00027 You should have received a copy of the GNU Lesser General Public License 00028 along with the TiA library. If not, see <http://www.gnu.org/licenses/>. 00029 00030 Copyright 2010 Graz University of Technology 00031 Contact: TiA@tobi-project.org 00032 */ 00033 00034 #include "tia-private/newtia/network_impl/boost_tcp_server_socket_impl.h" 00035 #include "tia-private/newtia/network_impl/boost_tcp_socket_impl.h" 00036 00037 #include <boost/bind.hpp> 00038 #include <boost/weak_ptr.hpp> 00039 00040 using namespace boost::asio::ip; 00041 using boost::shared_ptr; 00042 using boost::weak_ptr; 00043 00044 namespace tia 00045 { 00046 00047 //----------------------------------------------------------------------------- 00048 void BoostTCPServerSocketImpl::startListening (unsigned port, 00049 NewConnectionListener* new_connection_listener) 00050 { 00051 new_connection_listener_ = new_connection_listener; 00052 00053 tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), port); 00054 acceptor_.open (endpoint.protocol()); 00055 acceptor_.set_option (boost::asio::ip::tcp::acceptor::reuse_address(true)); 00056 acceptor_.bind (endpoint); 00057 acceptor_.listen (); 00058 asyncAccept (); 00059 } 00060 00061 //----------------------------------------------------------------------------- 00062 unsigned BoostTCPServerSocketImpl::startListening (NewConnectionListener* new_connection_listener) 00063 { 00064 new_connection_listener_ = new_connection_listener; 00065 00066 tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 0); 00067 acceptor_.open (endpoint.protocol()); 00068 acceptor_.set_option (boost::asio::ip::tcp::acceptor::reuse_address(true)); 00069 acceptor_.bind (endpoint); 00070 acceptor_.listen (); 00071 asyncAccept (); 00072 return acceptor_.local_endpoint().port(); 00073 } 00074 00075 //------------------------------------------------------------------------- 00076 void BoostTCPServerSocketImpl::stopListening () 00077 { 00078 new_connection_listener_ = 0; 00079 acceptor_.cancel (); 00080 } 00081 00082 //----------------------------------------------------------------------------- 00083 void BoostTCPServerSocketImpl::asyncAccept () 00084 { 00085 shared_ptr<tcp::socket> peer_socket (new tcp::socket(acceptor_.get_io_service())); 00086 acceptor_.async_accept (*peer_socket, boost::bind (&BoostTCPServerSocketImpl::handleAccept, 00087 this, peer_socket)); 00088 } 00089 00090 //----------------------------------------------------------------------------- 00091 void BoostTCPServerSocketImpl::handleAccept (shared_ptr<tcp::socket> socket) 00092 { 00093 if (new_connection_listener_) 00094 new_connection_listener_->newConnection (boost::shared_ptr<Socket>(new BoostTCPSocketImpl (socket))); 00095 asyncAccept (); 00096 } 00097 00098 00099 }