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



Page generated by Doxygen 1.8.14 for MRPT 2.0.5 Git: b26ce1112 Mon Jul 6 09:39:08 2020 +0200 at lun jul 6 09:45:13 CEST 2020