connection.h
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2008, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage, Inc. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  */
00034 
00035 #ifndef ROSCPP_CONNECTION_H
00036 #define ROSCPP_CONNECTION_H
00037 
00038 #include "ros/header.h"
00039 #include "common.h"
00040 
00041 #include <boost/signals.hpp>
00042 #include <boost/function.hpp>
00043 #include <boost/shared_ptr.hpp>
00044 #include <boost/shared_array.hpp>
00045 #include <boost/enable_shared_from_this.hpp>
00046 #include <boost/thread/mutex.hpp>
00047 #include <boost/thread/recursive_mutex.hpp>
00048 
00049 #define READ_BUFFER_SIZE (1024*64)
00050 
00051 namespace ros
00052 {
00053 
00054 class Transport;
00055 typedef boost::shared_ptr<Transport> TransportPtr;
00056 class Connection;
00057 typedef boost::shared_ptr<Connection> ConnectionPtr;
00058 typedef boost::function<void(const ConnectionPtr&, const boost::shared_array<uint8_t>&, uint32_t, bool)> ReadFinishedFunc;
00059 typedef boost::function<void(const ConnectionPtr&)> WriteFinishedFunc;
00060 
00061 typedef boost::function<bool(const ConnectionPtr&, const Header&)> HeaderReceivedFunc;
00062 
00069 class ROSCPP_DECL Connection : public boost::enable_shared_from_this<Connection>
00070 {
00071 public:
00072   enum DropReason
00073   {
00074     TransportDisconnect,
00075     HeaderError,
00076     Destructing,
00077   };
00078 
00079   Connection();
00080   ~Connection();
00081 
00085   void initialize(const TransportPtr& transport, bool is_server, const HeaderReceivedFunc& header_func);
00090   void drop(DropReason reason);
00091 
00095   bool isDropped();
00096 
00100   bool isSendingHeaderError() { return sending_header_error_; }
00101 
00106   void sendHeaderError(const std::string& error_message);
00112   void writeHeader(const M_string& key_vals, const WriteFinishedFunc& finished_callback);
00113 
00127   void read(uint32_t size, const ReadFinishedFunc& finished_callback);
00144   void write(const boost::shared_array<uint8_t>& buffer, uint32_t size, const WriteFinishedFunc& finished_callback, bool immedate = true);
00145 
00146   typedef boost::signal<void(const ConnectionPtr&, DropReason reason)> DropSignal;
00147   typedef boost::function<void(const ConnectionPtr&, DropReason reason)> DropFunc;
00151   boost::signals::connection addDropListener(const DropFunc& slot);
00152   void removeDropListener(const boost::signals::connection& c);
00153 
00157   void setHeaderReceivedCallback(const HeaderReceivedFunc& func);
00158 
00162   const TransportPtr& getTransport() { return transport_; }
00166   Header& getHeader() { return header_; }
00167 
00172   void setHeader(const Header& header) { header_ = header; }
00173 
00174   std::string getCallerId();
00175   std::string getRemoteString();
00176 
00177 private:
00181   void onReadable(const TransportPtr& transport);
00185   void onWriteable(const TransportPtr& transport);
00190   void onDisconnect(const TransportPtr& transport);
00191 
00192 
00193   void onHeaderWritten(const ConnectionPtr& conn);
00194   void onErrorHeaderWritten(const ConnectionPtr& conn);
00195   void onHeaderLengthRead(const ConnectionPtr& conn, const boost::shared_array<uint8_t>& buffer, uint32_t size, bool success);
00196   void onHeaderRead(const ConnectionPtr& conn, const boost::shared_array<uint8_t>& buffer, uint32_t size, bool success);
00197 
00202   void readTransport();
00206   void writeTransport();
00207 
00209   bool is_server_;
00211   bool dropped_;
00213   Header header_;
00215   TransportPtr transport_;
00217   HeaderReceivedFunc header_func_;
00218 
00220   boost::shared_array<uint8_t> read_buffer_;
00222   uint32_t read_filled_;
00224   uint32_t read_size_;
00226   ReadFinishedFunc read_callback_;
00228   boost::recursive_mutex read_mutex_;
00230   bool reading_;
00234   volatile uint32_t has_read_callback_;
00235 
00237   boost::shared_array<uint8_t> write_buffer_;
00239   uint32_t write_sent_;
00241   uint32_t write_size_;
00243   WriteFinishedFunc write_callback_;
00244   boost::mutex write_callback_mutex_;
00246   boost::recursive_mutex write_mutex_;
00248   bool writing_;
00252   volatile uint32_t has_write_callback_;
00253 
00255   WriteFinishedFunc header_written_callback_;
00256 
00258   DropSignal drop_signal_;
00259 
00261   boost::recursive_mutex drop_mutex_;
00262 
00264   bool sending_header_error_;
00265 };
00266 typedef boost::shared_ptr<Connection> ConnectionPtr;
00267 
00268 } // namespace ros
00269 
00270 #endif // ROSCPP_CONNECTION_H


roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim
autogenerated on Mon Oct 6 2014 11:46:44