MRPT  2.0.0
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 #include <memory> // for unique_ptr<>
13 #include <string>
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  ~CPipeBaseEndPoint() override;
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  */
82  std::string serialize();
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{0};
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. */
90  unsigned int timeout_read_between_us{0};
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{0};
103 #endif
104  public:
105  size_t Read(void* Buffer, size_t Count) override;
106  size_t Write(const void* Buffer, size_t Count) override;
107 
108  /** Without effect in this class */
109  uint64_t Seek(int64_t of, CStream::TSeekOrigin o = sFromBeginning) override;
110  /** Without effect in this class */
111  uint64_t getTotalBytesCount() const override;
112  /** Without effect in this class */
113  uint64_t getPosition() const override;
114 }; // end of CPipeBaseEndPoint
115 static_assert(
116  !std::is_copy_constructible_v<CPipeBaseEndPoint> &&
117  !std::is_copy_assignable_v<CPipeBaseEndPoint>,
118  "Copy Check");
119 
120 /** The read end-point in a pipe created with mrpt::synch::CPipe.
121  * Use the method CStream::Read() of the base class CStream
122  * for blocking reading.
123  * \ingroup mrpt_io_grp
124  */
126 {
127  friend class CPipe;
128 
129  public:
130  /** De-serializes one end-point description, for example, from a parent
131  * process. */
132  explicit CPipeReadEndPoint(const std::string& serialized);
133 
134  /** Read-only pipe, don't call this method */
135  size_t Write(const void* Buffer, size_t Count) override
136  {
137  throw std::runtime_error("CPipeReadEndPoint::Write() cant be called.");
138  }
139 
140  private:
142 
143 }; // end of CPipeReadEndPoint
144 
145 /** The write end-point in a pipe created with mrpt::synch::CPipe.
146  * Use the method CStream::Write() of the base class CStream
147  * for blocking writing. */
149 {
150  friend class CPipe;
151 
152  public:
153  /** De-serializes one end-point description, for example, from a parent
154  * process. */
155  explicit CPipeWriteEndPoint(const std::string& serialized);
156 
157  /** Write-only pipe: read launches exception */
158  size_t Read(void* Buffer, size_t Count) override
159  {
160  throw std::runtime_error("CPipeWriteEndPoint::Read() cant be called.");
161  }
162 
163  private:
165 
166 }; // end of CPipeWriteEndPoint
167 
168 template <typename ReadPtr, typename WritePtr>
169 void CPipe::createPipe(ReadPtr& outReadPipe, WritePtr& outWritePipe)
170 {
171  outReadPipe = ReadPtr(new CPipeReadEndPoint);
172  outWritePipe = WritePtr(new CPipeWriteEndPoint);
173  CPipe::initializePipe(*outReadPipe, *outWritePipe);
174 }
175 } // 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:158
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:135
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
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:38
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:148
~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:86
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:169
The read end-point in a pipe created with mrpt::synch::CPipe.
Definition: CPipe.h:125
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
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 2.0.0 Git: b38439d21 Tue Mar 31 19:58:06 2020 +0200 at miƩ abr 1 00:50:30 CEST 2020