Main MRPT website > C++ reference for MRPT 1.4.0
CPipe.h
Go to the documentation of this file.
1/* +---------------------------------------------------------------------------+
2 | Mobile Robot Programming Toolkit (MRPT) |
3 | http://www.mrpt.org/ |
4 | |
5 | Copyright (c) 2005-2016, Individual contributors, see AUTHORS file |
6 | See: http://www.mrpt.org/Authors - All rights reserved. |
7 | Released under BSD License. See details in http://www.mrpt.org/License |
8 +---------------------------------------------------------------------------+ */
9#ifndef mrpt_synch_pipe_H
10#define mrpt_synch_pipe_H
11
14#include <mrpt/utils/CStream.h>
15#include <string>
16#include <memory> // for auto_ptr<>
17
18namespace mrpt
19{
20 namespace synch
21 {
22 class CPipeReadEndPoint;
23 class CPipeWriteEndPoint;
24
25 /** A pipe, portable across different OS.
26 * Pipes can be used as intraprocess (inter-threads) or interprocess communication mechanism.
27 * Read more on pipes here: http://www.gnu.org/software/libc/manual/html_node/Pipes-and-FIFOs.html
28 *
29 * \code
30 * std::auto_ptr<CPipeReadEndPoint> read_pipe;
31 * std::auto_ptr<CPipeWriteEndPoint> write_pipe;
32 *
33 * CPipe::createPipe(read_pipe,write_pipe);
34 *
35 * \endcode
36 *
37 * See also the example: MRPT/samples/threadsPipe/
38 *
39 * \ingroup synch_grp
40 */
42 {
43 public:
44 /** Creates a new pipe and returns the read & write end-points as newly allocated objects.
45 * \exception std::exception On any error during the pipe creation
46 */
47 static void createPipe(std::auto_ptr<CPipeReadEndPoint>& outReadPipe,std::auto_ptr<CPipeWriteEndPoint>& outWritePipe);
48
49 private:
50 CPipe(); //!< No need to create any object of this class.
52 }; // end of CPipe
53
54
55 /** Common interface of read & write pipe end-points */
59 {
60 friend class CPipe;
61 public:
64
65 /** De-serializes one end-point description, for example, from a parent process. */
66 explicit CPipeBaseEndPoint(const std::string &serialized);
67
68 /** Converts the end-point into a string suitable for reconstruction at a child process.
69 * This *invalidates* this object, since only one real end-point can exist at once.
70 */
71 std::string serialize();
72
73 unsigned int timeout_read_start_us; //!< (Default=0) Timeout for read operations: microseconds (us) to wait for the first byte. 0 means infinite timeout.
74 unsigned int timeout_read_between_us; //!< (Default=0) Timeout between burst reads operations: microseconds (us) to wait between two partial reads inside one large read. 0 means infinite timeout.
75
76 /** Returns false if the pipe was closed due to some error. */
77 inline bool isOpen() const { return m_pipe_file!=0; }
78
79 /** Closes the pipe (normally not needed to be called by users, automatically done at destructor) */
80 void close();
81
82 protected:
83#ifdef MRPT_OS_WINDOWS
84 void * m_pipe_file;
85#else
87#endif
88 virtual size_t Read(void *Buffer, size_t Count) MRPT_OVERRIDE;
89 virtual size_t Write(const void *Buffer, size_t Count) MRPT_OVERRIDE;
90
91 virtual uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning) MRPT_OVERRIDE; //!< Without effect in this class
92 virtual uint64_t getTotalBytesCount() MRPT_OVERRIDE; //!< Without effect in this class
93 virtual uint64_t getPosition() MRPT_OVERRIDE; //!< Without effect in this class
94 }; // end of CPipeBaseEndPoint
95
96 /** The read end-point in a pipe created with mrpt::synch::CPipe.
97 * Use the method mrpt::utils::CStream::ReadBuffer() of the base class CStream for blocking reading. */
99 {
100 friend class CPipe;
101 public:
102 /** De-serializes one end-point description, for example, from a parent process. */
103 explicit CPipeReadEndPoint(const std::string &serialized);
104
105 private:
107 void WriteBuffer (const void *Buffer, size_t Count); //!< Hide the write method in this read-only pipe.
108
109 }; // end of CPipeReadEndPoint
110
111 /** The write end-point in a pipe created with mrpt::synch::CPipe.
112 * Use the method mrpt::utils::CStream::WriteBuffer() of the base class CStream for blocking writing. */
114 {
115 friend class CPipe;
116 public:
117 /** De-serializes one end-point description, for example, from a parent process. */
118 explicit CPipeWriteEndPoint(const std::string &serialized);
119
120 private:
122 size_t ReadBuffer(void *Buffer, size_t Count); //!< Hide the read method in this write-only pipe.
123
124 }; // end of CPipeWriteEndPoint
125
126
127 } // End of namespace
128
129} // End of namespace
130
131#endif
Common interface of read & write pipe end-points.
Definition: CPipe.h:59
unsigned int timeout_read_between_us
(Default=0) Timeout between burst reads operations: microseconds (us) to wait between two partial rea...
Definition: CPipe.h:74
virtual size_t Read(void *Buffer, size_t Count) MRPT_OVERRIDE
Introduces a pure virtual method responsible for reading from the stream.
unsigned int timeout_read_start_us
(Default=0) Timeout for read operations: microseconds (us) to wait for the first byte....
Definition: CPipe.h:73
CPipeBaseEndPoint(const std::string &serialized)
De-serializes one end-point description, for example, from a parent process.
virtual uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) MRPT_OVERRIDE
Without effect in this class.
virtual uint64_t getTotalBytesCount() MRPT_OVERRIDE
Without effect in this class.
virtual size_t Write(const void *Buffer, size_t Count) MRPT_OVERRIDE
Introduces a pure virtual method responsible for writing to the stream.
void close()
Closes the pipe (normally not needed to be called by users, automatically done at destructor)
bool isOpen() const
Returns false if the pipe was closed due to some error.
Definition: CPipe.h:77
std::string serialize()
Converts the end-point into a string suitable for reconstruction at a child process.
A pipe, portable across different OS.
Definition: CPipe.h:42
static void createPipe(std::auto_ptr< CPipeReadEndPoint > &outReadPipe, std::auto_ptr< CPipeWriteEndPoint > &outWritePipe)
Creates a new pipe and returns the read & write end-points as newly allocated objects.
CPipe()
No need to create any object of this class.
The read end-point in a pipe created with mrpt::synch::CPipe.
Definition: CPipe.h:99
CPipeReadEndPoint(const std::string &serialized)
De-serializes one end-point description, for example, from a parent process.
void WriteBuffer(const void *Buffer, size_t Count)
Hide the write method in this read-only pipe.
The write end-point in a pipe created with mrpt::synch::CPipe.
Definition: CPipe.h:114
CPipeWriteEndPoint(const std::string &serialized)
De-serializes one end-point description, for example, from a parent process.
size_t ReadBuffer(void *Buffer, size_t Count)
Hide the read method in this write-only pipe.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: CStream.h:39
The base class of classes that cannot be copied: compile-time errors will be issued on any copy opera...
Definition: CUncopiable.h:31
#define MRPT_OVERRIDE
C++11 "override" for virtuals:
Definition: mrpt_macros.h:28
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.



Page generated by Doxygen 1.9.5 for MRPT 1.4.0 SVN: at Tue Dec 27 00:54:45 UTC 2022