|
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_udp_read_socket.h" 00035 #include "tia-private/newtia/messages/tia_control_message_tags_1_0.h" 00036 #include "tia-private/newtia/tia_exceptions.h" 00037 00038 #include <boost/asio.hpp> 00039 #include <iostream> 00040 00041 using std::string; 00042 00043 namespace tia 00044 { 00045 00046 00047 //----------------------------------------------------------------------------- 00048 BoostUDPReadSocket::BoostUDPReadSocket (boost::asio::io_service& io_service, boost::asio::ip::udp::endpoint const& endpoint, unsigned buffer_size) 00049 : socket_ (new boost::asio::ip::udp::socket (io_service)), 00050 buffer_size_ (buffer_size), 00051 rec_buffer_ (buffer_size) 00052 { 00053 boost::system::error_code error; 00054 boost::asio::socket_base::receive_buffer_size rec_buffer_size (buffer_size); 00055 00056 boost::asio::socket_base::broadcast bcast(true); 00057 00058 socket_->open (boost::asio::ip::udp::v4(), error); 00059 if (error) 00060 throw TiAException ("Could not open UDP socket."); 00061 00062 socket_->bind (endpoint, error); 00063 socket_->set_option (bcast); 00064 socket_->set_option (rec_buffer_size); 00065 00066 if (error) 00067 throw TiAException ("Could not bind UDP socket."); 00068 else 00069 std::cout << "UDP socket connect to " << endpoint << " successfull." << std::endl; 00070 } 00071 00072 00073 //----------------------------------------------------------------------------- 00074 00075 void BoostUDPReadSocket::setReceiveBufferSize (unsigned size) 00076 { 00077 boost::asio::socket_base::receive_buffer_size option(size); 00078 socket_->set_option (option); 00079 } 00080 00081 //----------------------------------------------------------------------------- 00082 00083 string BoostUDPReadSocket::readUntil (char delimiter) 00084 { 00085 // boost::system::error_code error; 00086 00087 // size_t transfered = boost::asio::read_until (socket_, stream_buffer_, delimiter,error ); 00088 00089 // if(error) 00090 // throw TiALostConnection ("InputStreamSocket::readUntil error read_until: " 00091 // + string (error.category().name()) + error.message()); 00092 // str_buffer_.clear(); 00093 00094 // char c; 00095 // size_t n = 1; 00096 // while(n != transfered) 00097 // { 00098 // input_stream_.get(c); 00099 // str_buffer_.append(&c); 00100 // n++; 00101 // } 00102 // input_stream_.get(c); 00103 00104 return(buffered_string_); 00105 } 00106 00107 //----------------------------------------------------------------------------- 00108 00109 string BoostUDPReadSocket::readUntil (std::string delimiter) 00110 { 00111 00112 // boost::system::error_code error; 00113 00114 // size_t transfered = boost::asio::read_until (socket_, stream_buffer_, delimiter,error ); 00115 00116 // if(error) 00117 // throw TiALostConnection ("InputStreamSocket::readLine error read_until: " 00118 // + string (error.category().name()) + error.message()); 00119 // str_buffer_.clear(); 00120 00121 // char c; 00122 // size_t n = delimiter.size(); 00123 // while(n != transfered) 00124 // { 00125 // input_stream_.get(c); 00126 // str_buffer_.append(&c); 00127 // n++; 00128 // } 00129 00130 // for(unsigned int s = 0; s < delimiter.size(); s++) 00131 // input_stream_.get(); 00132 00133 return(buffered_string_); 00134 } 00135 00136 00137 //----------------------------------------------------------------------------- 00138 string BoostUDPReadSocket::readString (unsigned length) 00139 { 00140 if (length > buffered_string_.size()) 00141 readBytes (length - buffered_string_.size ()); 00142 00143 string str = buffered_string_.substr (0, length); 00144 buffered_string_.erase (0, length); 00145 00146 return str; 00147 } 00148 00149 //----------------------------------------------------------------------------- 00150 char BoostUDPReadSocket::readCharacter () 00151 { 00152 if (!buffered_string_.size ()) 00153 readBytes (1); 00154 char character = buffered_string_[0]; 00155 buffered_string_.erase (0, 1); 00156 return character; 00157 } 00158 00159 //----------------------------------------------------------------------------- 00160 void BoostUDPReadSocket::waitForData () 00161 { 00162 if (!buffered_string_.size ()) 00163 readBytes (1); 00164 } 00165 00166 //----------------------------------------------------------------------------- 00167 void BoostUDPReadSocket::readBytes (unsigned num_bytes) 00168 { 00169 boost::system::error_code error; 00170 00171 if (error) 00172 throw TiALostConnection ("BoostUDPReadSocket"); 00173 00174 unsigned received = socket_->receive (boost::asio::buffer (rec_buffer_), 0, error); 00175 //std::cout << __FUNCTION__ << " received: " << received << "; data size = " << rec_buffer_.size() << std::endl; 00176 if (error) 00177 { 00178 throw TiALostConnection ("BoostUDPReadSocket"); 00179 } 00180 buffered_string_.append (rec_buffer_.data(), received); 00181 } 00182 00183 00184 } 00185