Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
Pipe.h
1 #pragma once
2 #include <string>
3 
9 class Pipe
10 {
11 public:
12  Pipe() {};
13  Pipe(const std::string& pipeName, size_t size = 65536 * 128, bool newBlockingRt = false, bool newBlockingNonRt = false);
14  ~Pipe() {cleanup();}
15  Pipe(Pipe&&) = delete;
26  bool setup(const std::string& pipeName = defaultName, size_t size = 65536 * 128, bool newBlockingRt = false, bool newBlockingNonRt = false);
27  void cleanup();
31  void setBlockingRt(bool blocking);
35  void setBlockingNonRt(bool blocking);
39  void setTimeoutMsRt(double timeoutMs);
43  void setTimeoutMsNonRt(double timeoutMs);
47  template<typename T> bool writeNonRt(const T& data);
51  template<typename T> bool writeNonRt(T* ptr, size_t count);
55  template<typename T> bool writeRt(const T& data);
59  template<typename T> bool writeRt(T* ptr, size_t count);
63  template<typename T> ssize_t readNonRt(T & dest);
67  template<typename T> ssize_t readNonRt(T* dest, size_t count);
71  template<typename T> ssize_t readRt(T & dest);
75  template<typename T> ssize_t readRt(T* dest, size_t count);
76 private:
77  bool _writeNonRt(void* ptr, size_t size);
78  bool _writeRt(void* ptr, size_t size);
79  ssize_t _readRtNonRt(void* ptr, size_t size, bool rt);
80  ssize_t _readNonRt(void* ptr, size_t size);
81  ssize_t _readRt(void* ptr, size_t size);
82  static std::string defaultName;
83  std::string name;
84  std::string path;
85  int pipeSocket;
86  int fd = 0;
87  int pipeSize;
88  double timeoutMsRt = 0;
89  double timeoutMsNonRt = 0;
90  bool blockingRt = false;
91  bool blockingNonRt = false;
92 };
93 
94 template<typename T> bool Pipe::writeNonRt(const T& data)
95 {
96  return writeNonRt(&data, 1);
97 }
98 
99 template <typename T> bool Pipe::writeNonRt(T* data, size_t count)
100 {
101  size_t size = count * sizeof(*data);
102  return _writeNonRt((void*)data, size);
103 }
104 
105 template<typename T> bool Pipe::writeRt(const T& data)
106 {
107  return writeRt(&data, 1);
108 }
109 
110 template <typename T> bool Pipe::writeRt(T* ptr, size_t count)
111 {
112  size_t size = count * sizeof(*ptr);
113  return _writeRt((void*)ptr, size);
114 }
115 
116 template<typename T> ssize_t Pipe::readRt(T & dest)
117 {
118  return readRt(&dest, 1);
119 }
120 
121 template<typename T> ssize_t Pipe::readRt(T* dest, size_t count)
122 {
123  ssize_t ret = _readRt((void*)dest, count * sizeof(*dest));
124  if(ret >= 0)
125  return ret / sizeof(*dest);
126  else
127  return ret;
128 }
129 
130 template<typename T> ssize_t Pipe::readNonRt(T & dest)
131 {
132  return readNonRt(&dest, 1);
133 }
134 
135 template<typename T> ssize_t Pipe::readNonRt(T* dest, size_t count)
136 {
137  ssize_t ret = _readNonRt((void*)dest, count * sizeof(*dest));
138  if(ret >= 0)
139  return ret / sizeof(*dest);
140  else
141  return ret;
142 }
bool writeNonRt(const T &data)
Definition: Pipe.h:94
void setTimeoutMsRt(double timeoutMs)
Definition: Pipe.cpp:78
void setTimeoutMsNonRt(double timeoutMs)
Definition: Pipe.cpp:83
void setBlockingRt(bool blocking)
Definition: Pipe.cpp:46
ssize_t readNonRt(T &dest)
Definition: Pipe.h:130
ssize_t readRt(T &dest)
Definition: Pipe.h:116
void setBlockingNonRt(bool blocking)
Definition: Pipe.cpp:62
bool setup(const std::string &pipeName=defaultName, size_t size=65536 *128, bool newBlockingRt=false, bool newBlockingNonRt=false)
Definition: Pipe.cpp:13
bool writeRt(const T &data)
Definition: Pipe.h:105
Definition: Pipe.h:9