RSB  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BusConnection.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSB project
4  *
5  * Copyright (C) 2011 Jan Moringen <jmoringe@techfak.uni-bielefeld.de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #pragma once
28 
29 #include <string>
30 
31 #include <boost/enable_shared_from_this.hpp>
32 
33 #include <boost/thread/recursive_mutex.hpp>
34 
35 #include <boost/asio.hpp>
36 
37 #include <rsc/logging/Logger.h>
38 #include <rsc/runtime/Printable.h>
39 
40 #include "../../Event.h"
41 
42 #include "../../protocol/Notification.h"
43 
44 #include "rsb/rsbexports.h"
45 
46 namespace rsb {
47 namespace transport {
48 namespace socket {
49 
50 // forward declaration
51 class Bus;
52 typedef boost::shared_ptr<Bus> BusPtr;
53 
74 class RSB_EXPORT BusConnection : public boost::enable_shared_from_this<BusConnection>,
75  public rsc::runtime::Printable {
76 public:
77  typedef boost::shared_ptr<boost::asio::ip::tcp::socket> SocketPtr;
78 
80  SocketPtr socket,
81  bool client,
82  bool tcpNoDelay = false);
83 
84  ~BusConnection();
85 
86  void shutdown();
87 
88  void startReceiving();
89 
90  void sendEvent(EventPtr event,
91  const std::string& wireSchema);
92 private:
93  typedef boost::weak_ptr<Bus> WeakBusPtr;
94 
95  rsc::logging::LoggerPtr logger;
96 
98 
100 
101  volatile bool disconnecting;
102  volatile bool activeShutdown;
103 
104  boost::recursive_mutex mutex;
105 
106  // Receive buffers
107  protocol::Notification notification;
108  std::string lengthReceiveBuffer;
109  std::string messageReceiveBuffer;
110 
111  // Send buffers
112  std::string lengthSendBuffer;
113  std::string messageSendBuffer;
114 
115  void performSafeCleanup(const std::string& context);
116 
117  void receiveEvent();
118 
119  void handleReadLength(const boost::system::error_code& error,
120  size_t bytesTransferred);
121 
122  void handleReadBody(const boost::system::error_code& error,
123  size_t bytesTransferred,
124  size_t expected);
125 
126  void printContents(std::ostream& stream) const;
127 
128  void disconnect();
129 
130 };
131 
132 typedef boost::shared_ptr<BusConnection> BusConnectionPtr;
133 
134 }
135 }
136 }
Instances of this class provide access to a socket-based bus.
Definition: Bus.h:71
boost::shared_ptr< BusConnection > BusConnectionPtr
boost::weak_ptr< Bus > WeakBusPtr
Definition: BusConnection.h:93
rsc::logging::LoggerPtr logger
Definition: BusConnection.h:95
Instances of this class implement connections to a socket-based bus.
Definition: BusConnection.h:74
boost::shared_ptr< boost::asio::ip::tcp::socket > SocketPtr
Definition: BusConnection.h:77
protocol::Notification notification
boost::shared_ptr< Event > EventPtr
Definition: Event.h:251
boost::shared_ptr< Bus > BusPtr
Definition: BusConnection.h:51