MRPT  1.9.9
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-2018, 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 #pragma once
10 
11 #include <mrpt/io/CStream.h>
12 #include <string>
13 #include <memory> // for unique_ptr<>
14 
15 namespace mrpt::io
16 {
17 class CPipeReadEndPoint;
18 class CPipeWriteEndPoint;
19 
20 /** A pipe, portable across different OS.
21  * Pipes can be used as intraprocess (inter-threads) or interprocess
22  * communication mechanism.
23  * Read more on pipes here:
24  * http://www.gnu.org/software/libc/manual/html_node/Pipes-and-FIFOs.html
25  *
26  * \code
27  * std::unique_ptr<CPipeReadEndPoint> read_pipe;
28  * std::unique_ptr<CPipeWriteEndPoint> write_pipe;
29  *
30  * CPipe::createPipe(read_pipe,write_pipe);
31  *
32  * \endcode
33  *
34  * See also the example: MRPT/samples/threadsPipe/
35  *
36  * \ingroup mrpt_io_grp
37  */
38 class CPipe
39 {
40  public:
41  /** Create via createPipe() instead */
42  CPipe() = delete;
43  ~CPipe() = delete;
44 
45  /** Creates a new pipe and returns the read & write end-points as newly
46  * allocated objects.
47  * \exception std::exception On any error during the pipe creation
48  */
49  /** Creates a new pipe and returns the read & write end-points as newly
50  * allocated objects. */
51  template <typename ReadPtr, typename WritePtr>
52  static void createPipe(ReadPtr& outReadPipe, WritePtr& outWritePipe);
53 
54  static void initializePipe(
55  CPipeReadEndPoint& outReadPipe, CPipeWriteEndPoint& outWritePipe);
56 }; // end of CPipe
57 
58 /** Common interface of read & write pipe end-points
59  * \ingroup mrpt_io_grp
60  */
62 {
63  friend class CPipe;
64 
65  public:
67 
68  CPipeBaseEndPoint(const CPipeBaseEndPoint&) = delete;
70 
71  virtual ~CPipeBaseEndPoint();
72 
73  /** De-serializes one end-point description, for example, from a parent
74  * process. */
75  explicit CPipeBaseEndPoint(const std::string& serialized);
76 
77  /** Converts the end-point into a string suitable for reconstruction at a
78  * child process.
79  * This *invalidates* this object, since only one real end-point can exist
80  * at once.
81  */
83 
84  /** (Default=0) Timeout for read operations: microseconds (us) to wait for
85  * the first byte. 0 means infinite timeout. */
86  unsigned int timeout_read_start_us;
87  /** (Default=0) Timeout between burst reads operations: microseconds (us) to
88  * wait between two partial reads inside one large read. 0 means infinite
89  * timeout. */
91 
92  /** Returns false if the pipe was closed due to some error. */
93  inline bool isOpen() const { return m_pipe_file != 0; }
94  /** Closes the pipe (normally not needed to be called by users,
95  * automatically done at destructor) */
96  void close();
97 
98  protected:
99 #ifdef _WIN32
100  void* m_pipe_file;
101 #else
102  int m_pipe_file;
103 #endif
104  public:
105  virtual size_t Read(void* Buffer, size_t Count) override;
106  virtual size_t Write(const void* Buffer, size_t Count) override;
107 
108  /** Without effect in this class */
109  virtual uint64_t Seek(
110  int64_t of, CStream::TSeekOrigin o = sFromBeginning) override;
111  /** Without effect in this class */
112  virtual uint64_t getTotalBytesCount() const override;
113  /** Without effect in this class */
114  virtual uint64_t getPosition() const override;
115 }; // end of CPipeBaseEndPoint
116 static_assert(
119  "Copy Check");
120 
121 /** The read end-point in a pipe created with mrpt::synch::CPipe.
122  * Use the method CStream::Read() of the base class CStream
123  * for blocking reading.
124  * \ingroup mrpt_io_grp
125  */
127 {
128  friend class CPipe;
129 
130  public:
131  /** De-serializes one end-point description, for example, from a parent
132  * process. */
133  explicit CPipeReadEndPoint(const std::string& serialized);
134 
135  /** Read-only pipe, don't call this method */
136  size_t Write(const void* Buffer, size_t Count)
137  {
138  throw std::runtime_error("CPipeReadEndPoint::Write() cant be called.");
139  }
140 
141  private:
143 
144 }; // end of CPipeReadEndPoint
145 
146 /** The write end-point in a pipe created with mrpt::synch::CPipe.
147  * Use the method CStream::Write() of the base class CStream
148  * for blocking writing. */
150 {
151  friend class CPipe;
152 
153  public:
154  /** De-serializes one end-point description, for example, from a parent
155  * process. */
156  explicit CPipeWriteEndPoint(const std::string& serialized);
157 
158  /** Write-only pipe: read launches exception */
159  size_t Read(void* Buffer, size_t Count)
160  {
161  throw std::runtime_error("CPipeWriteEndPoint::Read() cant be called.");
162  }
163 
164  private:
166 
167 }; // end of CPipeWriteEndPoint
168 
169 template <typename ReadPtr, typename WritePtr>
170 void CPipe::createPipe(ReadPtr& outReadPipe, WritePtr& outWritePipe)
171 {
172  outReadPipe = ReadPtr(new CPipeReadEndPoint);
173  outWritePipe = WritePtr(new CPipeWriteEndPoint);
174  CPipe::initializePipe(*outReadPipe, *outWritePipe);
175 }
176 } // End of namespace
177 
178 
TSeekOrigin
Used in CStream::Seek.
Definition: io/CStream.h:32
virtual size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
Definition: CPipe.cpp:120
virtual ~CPipeBaseEndPoint()
Definition: CPipe.cpp:57
virtual uint64_t getPosition() const override
Without effect in this class.
Definition: CPipe.cpp:118
size_t Read(void *Buffer, size_t Count)
Write-only pipe: read launches exception.
Definition: CPipe.h:159
void close()
Closes the pipe (normally not needed to be called by users, automatically done at destructor) ...
Definition: CPipe.cpp:59
CPipeBaseEndPoint & operator=(const CPipeBaseEndPoint &)=delete
CPipe()=delete
Create via createPipe() instead.
This base class is used to provide a unified interface to files,memory buffers,..Please see the deriv...
Definition: io/CStream.h:28
bool isOpen() const
Returns false if the pipe was closed due to some error.
Definition: CPipe.h:93
virtual size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
Definition: CPipe.cpp:219
std::string serialize()
Converts the end-point into a string suitable for reconstruction at a child process.
Definition: CPipe.cpp:105
__int64 int64_t
Definition: rptypes.h:49
A pipe, portable across different OS.
Definition: CPipe.h:38
virtual uint64_t Seek(int64_t of, CStream::TSeekOrigin o=sFromBeginning) override
Without effect in this class.
Definition: CPipe.cpp:116
GLsizei const GLchar ** string
Definition: glext.h:4101
The write end-point in a pipe created with mrpt::synch::CPipe.
Definition: CPipe.h:149
unsigned __int64 uint64_t
Definition: rptypes.h:50
unsigned int timeout_read_start_us
(Default=0) Timeout for read operations: microseconds (us) to wait for the first byte.
Definition: CPipe.h:86
virtual uint64_t getTotalBytesCount() const override
Without effect in this class.
Definition: CPipe.cpp:117
static void createPipe(ReadPtr &outReadPipe, WritePtr &outWritePipe)
Creates a new pipe and returns the read & write end-points as newly allocated objects.
Definition: CPipe.h:170
The read end-point in a pipe created with mrpt::synch::CPipe.
Definition: CPipe.h:126
GLsizei const GLfloat * value
Definition: glext.h:4117
unsigned int timeout_read_between_us
(Default=0) Timeout between burst reads operations: microseconds (us) to wait between two partial rea...
Definition: CPipe.h:90
size_t Write(const void *Buffer, size_t Count)
Read-only pipe, don&#39;t call this method.
Definition: CPipe.h:136
static void initializePipe(CPipeReadEndPoint &outReadPipe, CPipeWriteEndPoint &outWritePipe)
Creates a new pipe and returns the read & write end-points as newly allocated objects.
Definition: CPipe.cpp:30
Common interface of read & write pipe end-points.
Definition: CPipe.h:61



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020