RSB  0.7.0
 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/asio.hpp>
34 
35 #include <rsc/logging/Logger.h>
36 #include <rsc/runtime/Printable.h>
37 
38 #include "../../Event.h"
39 
40 #include "../../protocol/Notification.h"
41 
42 #include "rsb/rsbexports.h"
43 
44 namespace rsb {
45 namespace transport {
46 namespace socket {
47 
48 // forward declaration
49 class Bus;
50 typedef boost::shared_ptr<Bus> BusPtr;
51 
72 class RSB_EXPORT BusConnection : public boost::enable_shared_from_this<BusConnection>,
73  public rsc::runtime::Printable {
74 public:
75  typedef boost::shared_ptr<boost::asio::ip::tcp::socket> SocketPtr;
76 
78  SocketPtr socket,
79  bool client,
80  bool tcpNoDelay = false);
81 
82  ~BusConnection();
83 
84  void disconnect();
85 
86  void startReceiving();
87 
88  void sendEvent(EventPtr event,
89  const std::string& wireSchema);
90 private:
91  typedef boost::weak_ptr<Bus> WeakBusPtr;
92 
93  rsc::logging::LoggerPtr logger;
94 
96 
98 
99  volatile bool disconnecting;
100 
101  // Receive buffers
102  protocol::Notification notification;
103  std::string lengthReceiveBuffer;
104  std::string messageReceiveBuffer;
105 
106  // Send buffers
107  std::string lengthSendBuffer;
108  std::string messageSendBuffer;
109 
110  void performSafeCleanup(const std::string& context);
111 
112  void receiveEvent();
113 
114  void handleReadLength(const boost::system::error_code& error,
115  size_t bytesTransferred);
116 
117  void handleReadBody(const boost::system::error_code& error,
118  size_t bytesTransferred,
119  size_t expected);
120 
121  void printContents(std::ostream& stream) const;
122 };
123 
124 typedef boost::shared_ptr<BusConnection> BusConnectionPtr;
125 
126 }
127 }
128 }