Communication.h revision c4f55fee15b66ea53da092ca50400ac5d8b0692d
1//===-- Communication.h -----------------------------------------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#ifndef liblldb_Communication_h_ 11#define liblldb_Communication_h_ 12 13// C Includes 14// C++ Includes 15#include <string> 16 17// Other libraries and framework includes 18// Project includes 19#include "lldb/lldb-private.h" 20#include "lldb/Core/Broadcaster.h" 21#include "lldb/Core/Error.h" 22#include "lldb/Host/Host.h" 23#include "lldb/Host/Mutex.h" 24#include "lldb/Host/Predicate.h" 25#include "lldb/lldb-private.h" 26 27namespace lldb_private { 28 29//---------------------------------------------------------------------- 30/// @class Communication Communication.h "lldb/Core/Communication.h" 31/// @brief An abstract communications class. 32/// 33/// Communication is an class that handles data communication 34/// between two data sources. It uses a Connection class to do the 35/// real communication. This approach has a couple of advantages: it 36/// allows a single instance of this class to be used even though its 37/// connection can change. Connections could negotiate for different 38/// connections based on abilities like starting with Bluetooth and 39/// negotiating up to WiFi if available. It also allows this class to be 40/// subclassed by any interfaces that don't want to give bytes but want 41/// to validate and give out packets. This can be done by overriding: 42/// 43/// AppendBytesToCache (const uint8_t *src, size_t src_len, bool broadcast); 44/// 45/// Communication inherits from Broadcaster which means it can be 46/// used in conjunction with Listener to wait for multiple broadcaster 47/// objects and multiple events from each of those objects. 48/// Communication defines a set of pre-defined event bits (see 49/// enumerations definitions that start with "eBroadcastBit" below). 50/// 51/// There are two modes in which communications can occur: 52/// @li single-threaded 53/// @li multi-threaded 54/// 55/// In single-threaded mode, all reads and writes happen synchronously 56/// on the calling thread. 57/// 58/// In multi-threaded mode, a read thread is spawned that continually 59/// reads data and caches any received bytes. To start the read thread 60/// clients call: 61/// 62/// bool Communication::StartReadThread (Error *); 63/// 64/// If true is returned a read thead has been spawned that will 65/// continually execute a call to the pure virtual DoRead function: 66/// 67/// size_t Communication::ReadFromConnection (void *, size_t, uint32_t); 68/// 69/// When bytes are received the data gets cached in \a m_bytes and this 70/// class will broadcast a \b eBroadcastBitReadThreadGotBytes event. 71/// Clients that want packet based communication should override 72/// AppendBytesToCache. The subclasses can choose to call the 73/// built in AppendBytesToCache with the \a broadcast parameter set to 74/// false. This will cause the \b eBroadcastBitReadThreadGotBytes event 75/// not get broadcast, and then the subclass can post a \b 76/// eBroadcastBitPacketAvailable event when a full packet of data has 77/// been received. 78/// 79/// If the connection is disconnected a \b eBroadcastBitDisconnected 80/// event gets broadcast. If the read thread exits a \b 81/// eBroadcastBitReadThreadDidExit event will be broadcast. Clients 82/// can also post a \b eBroadcastBitReadThreadShouldExit event to this 83/// object which will cause the read thread to exit. 84//---------------------------------------------------------------------- 85class Communication : public Broadcaster 86{ 87public: 88 enum { 89 eBroadcastBitDisconnected = (1 << 0), ///< Sent when the communications connection is lost. 90 eBroadcastBitReadThreadGotBytes = (1 << 1), ///< Sent by the read thread when bytes become available. 91 eBroadcastBitReadThreadDidExit = (1 << 2), ///< Sent by the read thread when it exits to inform clients. 92 eBroadcastBitReadThreadShouldExit = (1 << 3), ///< Sent by clients that need to cancel the read thread. 93 eBroadcastBitPacketAvailable = (1 << 4), ///< Sent when data received makes a complete packet. 94 kLoUserBroadcastBit = (1 << 16),///< Subclasses can used bits 31:16 for any needed events. 95 kHiUserBroadcastBit = (1 << 31), 96 eAllEventBits = 0xffffffff 97 }; 98 99 typedef void (*ReadThreadBytesReceived) (void *baton, const void *src, size_t src_len); 100 101 102 //------------------------------------------------------------------ 103 /// Construct the Communication object with the specified name for 104 /// the Broadcaster that this object inherits from. 105 /// 106 /// @param[in] broadcaster_name 107 /// The name of the broadcaster object. This name should be as 108 /// complete as possible to uniquely identify this object. The 109 /// broadcaster name can be updated after the connect function 110 /// is called. 111 //------------------------------------------------------------------ 112 Communication(const char * broadcaster_name); 113 114 //------------------------------------------------------------------ 115 /// Destructor. 116 /// 117 /// The destructor is virtual since this class gets subclassed. 118 //------------------------------------------------------------------ 119 virtual 120 ~Communication(); 121 122 void 123 Clear (); 124 125 //------------------------------------------------------------------ 126 /// Poll for bytes available if the communications supports it. 127 /// 128 /// @param[in] timeout_usec 129 /// A timeout value in micro-seconds, UINT32_MAX for infinite 130 /// wait. 131 /// 132 /// @return 133 /// \b True if the bytes are, or became available within the 134 /// timeout period, \b false otherwise. 135 //------------------------------------------------------------------ 136 virtual lldb::ConnectionStatus 137 BytesAvailable (uint32_t timeout_usec, Error *error_ptr); 138 139 //------------------------------------------------------------------ 140 /// Connect using the current connection by passing \a url to its 141 /// connect function. 142 /// string. 143 /// 144 /// @param[in] url 145 /// A string that contains all information needed by the 146 /// subclass to connect to another client. 147 /// 148 /// @return 149 /// \b True if the connect succeeded, \b false otherwise. The 150 /// internal error object should be filled in with an 151 /// appropriate value based on the result of this function. 152 /// 153 /// @see Error& Communication::GetError (); 154 /// @see bool Connection::Connect (const char *url); 155 //------------------------------------------------------------------ 156 lldb::ConnectionStatus 157 Connect (const char *url, Error *error_ptr); 158 159 //------------------------------------------------------------------ 160 /// Disconnect the communications connection if one is currently 161 /// connected. 162 /// 163 /// @return 164 /// \b True if the disconnect succeeded, \b false otherwise. The 165 /// internal error object should be filled in with an 166 /// appropriate value based on the result of this function. 167 /// 168 /// @see Error& Communication::GetError (); 169 /// @see bool Connection::Disconnect (); 170 //------------------------------------------------------------------ 171 lldb::ConnectionStatus 172 Disconnect (Error *error_ptr = NULL); 173 174 //------------------------------------------------------------------ 175 /// Check if the connection is valid. 176 /// 177 /// @return 178 /// \b True if this object is currently connected, \b false 179 /// otherwise. 180 //------------------------------------------------------------------ 181 bool 182 IsConnected () const; 183 184 bool 185 HasConnection () const; 186 //------------------------------------------------------------------ 187 /// Read bytes from the current connection. 188 /// 189 /// If no read thread is running, this function call the 190 /// connection's Connection::BytesAvailable(uint32_t) method to 191 /// wait for available data, and then call the 192 /// Connection::Read(void *, size_t) function to get any available 193 /// bytes if Connection::BytesAvailable(uint32_t) returned true. 194 /// 195 /// If a read thread has been started, this function will check for 196 /// any cached bytes that have already been read and return any 197 /// currently available bytes. If no bytes are cached, it will wait 198 /// for the bytes to become available by listening for the \a 199 /// eBroadcastBitReadThreadGotBytes event. If this function consumes 200 /// all of the bytes in the cache, it will reset the 201 /// \a eBroadcastBitReadThreadGotBytes event bit. 202 /// 203 /// @param[in] dst 204 /// A destination buffer that must be at least \a dst_len bytes 205 /// long. 206 /// 207 /// @param[in] dst_len 208 /// The number of bytes to attempt to read, and also the max 209 /// number of bytes that can be placed into \a dst. 210 /// 211 /// @param[in] timeout_usec 212 /// A timeout value in micro-seconds. 213 /// 214 /// @return 215 /// The number of bytes actually read. 216 /// 217 /// @see bool Connection::BytesAvailable (uint32_t); 218 /// @see size_t Connection::Read (void *, size_t); 219 //------------------------------------------------------------------ 220 size_t 221 Read (void *dst, 222 size_t dst_len, 223 uint32_t timeout_usec, 224 lldb::ConnectionStatus &status, 225 Error *error_ptr); 226 227 //------------------------------------------------------------------ 228 /// The actual write function that attempts to write to the 229 /// communications protocol. 230 /// 231 /// Subclasses must override this function. 232 /// 233 /// @param[in] src 234 /// A source buffer that must be at least \a src_len bytes 235 /// long. 236 /// 237 /// @param[in] src_len 238 /// The number of bytes to attempt to write, and also the 239 /// number of bytes are currently available in \a src. 240 /// 241 /// @return 242 /// The number of bytes actually Written. 243 //------------------------------------------------------------------ 244 size_t 245 Write (const void *src, 246 size_t src_len, 247 lldb::ConnectionStatus &status, 248 Error *error_ptr); 249 250 //------------------------------------------------------------------ 251 /// Sets the connection that it to be used by this class. 252 /// 253 /// By making a communication class that uses different connections 254 /// it allows a single communication interface to negotiate and 255 /// change its connection without any interruption to the client. 256 /// It also allows the Communication class to be subclassed for 257 /// packet based communication. 258 /// 259 /// @param[in] connection 260 /// A connection that this class will own and destroy. 261 /// 262 /// @see 263 /// class Connection 264 //------------------------------------------------------------------ 265 void 266 SetConnection (Connection *connection); 267 268 //------------------------------------------------------------------ 269 /// Starts a read thread whose sole purpose it to read bytes from 270 /// the current connection. This function will call connection's 271 /// read function: 272 /// 273 /// size_t Connection::Read (void *, size_t); 274 /// 275 /// When bytes are read and cached, this function will call: 276 /// 277 /// Communication::AppendBytesToCache (const uint8_t * bytes, size_t len, bool broadcast); 278 /// 279 /// Subclasses should override this function if they wish to override 280 /// the default action of caching the bytes and broadcasting a \b 281 /// eBroadcastBitReadThreadGotBytes event. 282 /// 283 /// @return 284 /// \b True if the read thread was successfully started, \b 285 /// false otherwise. 286 /// 287 /// @see size_t Connection::Read (void *, size_t); 288 /// @see void Communication::AppendBytesToCache (const uint8_t * bytes, size_t len, bool broadcast); 289 //------------------------------------------------------------------ 290 virtual bool 291 StartReadThread (Error *error_ptr = NULL); 292 293 //------------------------------------------------------------------ 294 /// Stops the read thread by cancelling it. 295 /// 296 /// @return 297 /// \b True if the read thread was successfully canceled, \b 298 /// false otherwise. 299 //------------------------------------------------------------------ 300 virtual bool 301 StopReadThread (Error *error_ptr = NULL); 302 303 //------------------------------------------------------------------ 304 /// Checks if there is a currently running read thread. 305 /// 306 /// @return 307 /// \b True if the read thread is running, \b false otherwise. 308 //------------------------------------------------------------------ 309 bool 310 ReadThreadIsRunning (); 311 312 //------------------------------------------------------------------ 313 /// The static read thread function. This function will call 314 /// the "DoRead" function continuously and wait for data to become 315 /// avaialble. When data is received it will append the available 316 /// data to the internal cache and broadcast a 317 /// \b eBroadcastBitReadThreadGotBytes event. 318 /// 319 /// @param[in] comm_ptr 320 /// A pointer to an instance of this class. 321 /// 322 /// @return 323 /// \b NULL. 324 /// 325 /// @see void Communication::ReadThreadGotBytes (const uint8_t *, size_t); 326 //------------------------------------------------------------------ 327 static lldb::thread_result_t 328 ReadThread (lldb::thread_arg_t comm_ptr); 329 330 void 331 SetReadThreadBytesReceivedCallback (ReadThreadBytesReceived callback, 332 void *callback_baton); 333 334 static const char * 335 ConnectionStatusAsCString (lldb::ConnectionStatus status); 336 337private: 338 //------------------------------------------------------------------ 339 // For Communication only 340 //------------------------------------------------------------------ 341 DISALLOW_COPY_AND_ASSIGN (Communication); 342 343protected: 344 std::auto_ptr<Connection> m_connection_ap; ///< The connection that is current in use by this communications class. 345 lldb::thread_t m_read_thread; ///< The read thread handle in case we need to cancel the thread. 346 bool m_read_thread_enabled; 347 std::string m_bytes; ///< A buffer to cache bytes read in the ReadThread function. 348 Mutex m_bytes_mutex; ///< A mutex to protect multi-threaded access to the cached bytes. 349 ReadThreadBytesReceived m_callback; 350 void *m_callback_baton; 351 352 size_t 353 ReadFromConnection (void *dst, 354 size_t dst_len, 355 lldb::ConnectionStatus &status, 356 Error *error_ptr); 357 //------------------------------------------------------------------ 358 /// Append new bytes that get read from the read thread into the 359 /// internal object byte cache. This will cause a \b 360 /// eBroadcastBitReadThreadGotBytes event to be broadcast if \a 361 /// broadcast is true. 362 /// 363 /// Subclasses can override this function in order to inspect the 364 /// received data and check if a packet is available. 365 /// 366 /// Subclasses can also still call this function from the 367 /// overridden method to allow the caching to correctly happen and 368 /// suppress the broadcasting of the \a eBroadcastBitReadThreadGotBytes 369 /// event by setting \a broadcast to false. 370 /// 371 /// @param[in] src 372 /// A source buffer that must be at least \a src_len bytes 373 /// long. 374 /// 375 /// @param[in] src_len 376 /// The number of bytes to append to the cache. 377 //------------------------------------------------------------------ 378 virtual void 379 AppendBytesToCache (const uint8_t *src, size_t src_len, bool broadcast, lldb::ConnectionStatus status); 380 381 //------------------------------------------------------------------ 382 /// Get any available bytes from our data cache. If this call 383 /// empties the data cache, the \b eBroadcastBitReadThreadGotBytes event 384 /// will be reset to signify no more bytes are available. 385 /// 386 /// @param[in] dst 387 /// A destination buffer that must be at least \a dst_len bytes 388 /// long. 389 /// 390 /// @param[in] dst_len 391 /// The number of bytes to attempt to read from the cache, 392 /// and also the max number of bytes that can be placed into 393 /// \a dst. 394 /// 395 /// @return 396 /// The number of bytes extracted from the data cache. 397 //------------------------------------------------------------------ 398 size_t 399 GetCachedBytes (void *dst, size_t dst_len); 400}; 401 402} // namespace lldb_private 403 404#endif // liblldb_Communication_h_ 405