|
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 00039 #ifndef TCPSERVER_H 00040 #define TCPSERVER_H 00041 00042 // Standard 00043 #include <string> 00044 00045 // boost 00046 #include <boost/shared_ptr.hpp> 00047 #include <boost/enable_shared_from_this.hpp> 00048 #include <boost/asio.hpp> 00049 #include <boost/cstdint.hpp> 00050 00051 //local 00052 #include "tia/defines.h" 00053 00054 namespace tia 00055 { 00056 //----------------------------------------------------------------------------- 00057 00063 class TCPConnection : public boost::enable_shared_from_this<TCPConnection> 00064 { 00065 public: 00066 00070 typedef boost::shared_ptr<TCPConnection> pointer; 00071 00076 static pointer create(boost::asio::io_service& io_service) 00077 { 00078 return pointer(new TCPConnection(io_service)); 00079 } 00080 00081 static std::string endpointToString(const boost::asio::ip::tcp::endpoint& endpoint); 00082 00083 public: 00087 boost::asio::ip::tcp::socket& socket() { return socket_; } 00088 00089 virtual ~TCPConnection() 00090 { 00091 socket_.close(); 00092 } 00093 00094 private: 00098 TCPConnection(boost::asio::io_service& io_service) : 00099 socket_(io_service) 00100 { } 00101 00102 private: 00103 boost::asio::ip::tcp::socket socket_; 00104 }; 00105 00106 //----------------------------------------------------------------------------- 00107 00113 class TCPServer 00114 { 00115 public: 00120 TCPServer(boost::asio::io_service& io_service); 00121 00125 void bind(boost::uint16_t port); 00126 00130 void bind(const std::string& address, boost::uint16_t port); 00131 00135 void listen(); 00136 00137 protected: 00141 virtual void startAccept(); 00142 00147 virtual void handleAccept(const TCPConnection::pointer& new_connection, 00148 const boost::system::error_code& error) = 0; 00149 00150 protected: 00151 boost::asio::io_service& io_service_; 00152 boost::asio::ip::tcp::acceptor acceptor_; 00153 }; 00154 00155 } // Namespace tobiss 00156 00157 #endif //TCPSERVER_H 00158 00159 // End Of File