|
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 00041 // STL 00042 #include <sstream> 00043 #include <iostream> 00044 00045 // Boost 00046 #include <boost/bind.hpp> 00047 00048 // local 00049 #include "tia-private/network/tcp_server.h" 00050 00051 namespace tia 00052 { 00053 using boost::uint16_t; 00054 using boost::uint32_t; 00055 00056 //----------------------------------------------------------------------------- 00057 00058 std::string TCPConnection::endpointToString(const boost::asio::ip::tcp::endpoint& endpoint) 00059 { 00060 std::ostringstream ss; 00061 ss << endpoint.address().to_string(); 00062 ss << ":"; 00063 ss << endpoint.port(); 00064 return ss.str(); 00065 } 00066 00067 //----------------------------------------------------------------------------- 00068 00069 TCPServer::TCPServer(boost::asio::io_service& io_service) 00070 : io_service_(io_service), 00071 acceptor_(io_service) 00072 {} 00073 00074 //----------------------------------------------------------------------------- 00075 00076 void TCPServer::bind(uint16_t port) 00077 { 00078 boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), port); 00079 acceptor_.open(endpoint.protocol()); 00080 acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); 00081 acceptor_.bind(endpoint); 00082 } 00083 00084 //----------------------------------------------------------------------------- 00085 00086 void TCPServer::bind(const std::string& address, uint16_t port) 00087 { 00088 // Open the acceptor with the option to reuse the address (i.e. SO_REUSEADDR). 00089 boost::asio::ip::tcp::resolver resolver(io_service_); 00090 std::ostringstream ss; 00091 ss << port; 00092 boost::asio::ip::tcp::resolver::query query(address, ss.str()); 00093 boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query); 00094 acceptor_.open(endpoint.protocol()); 00095 acceptor_.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true)); 00096 acceptor_.bind(endpoint); 00097 } 00098 00099 //----------------------------------------------------------------------------- 00100 00101 void TCPServer::listen() 00102 { 00103 acceptor_.listen(); 00104 startAccept(); 00105 } 00106 00107 //----------------------------------------------------------------------------- 00108 00109 void TCPServer::startAccept() 00110 { 00111 TCPConnection::pointer new_connection = TCPConnection::create(io_service_); 00112 00113 acceptor_.async_accept(new_connection->socket(), boost::bind( 00114 &TCPServer::handleAccept, this, new_connection, 00115 boost::asio::placeholders::error)); 00116 } 00117 00118 //----------------------------------------------------------------------------- 00119 00120 } //Namespace tobiss 00121 00122 // End Of File