Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
 All Classes Files Functions Variables Typedefs 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();}
24  bool setup(const std::string& pipeName = defaultName, size_t size = 65536 * 128, bool newBlockingRt = false, bool newBlockingNonRt = false);
25  void cleanup();
29  void setBlockingRt(bool blocking);
33  void setBlockingNonRt(bool blocking);
37  void setTimeoutMsRt(double timeoutMs);
41  void setTimeoutMsNonRt(double timeoutMs);
45  template<typename T> bool writeNonRt(const T& data);
49  template<typename T> bool writeNonRt(T* ptr, size_t count);
53  template<typename T> bool writeRt(const T& data);
57  template<typename T> bool writeRt(T* ptr, size_t count);
61  template<typename T> ssize_t readNonRt(T & dest);
65  template<typename T> ssize_t readRt(T & dest);
69  template<typename T> ssize_t readNonRt(T* dest, size_t count);
73  template<typename T> ssize_t readRt(T* dest, size_t count);
74 private:
75  bool _writeNonRt(void* ptr, size_t size);
76  bool _writeRt(void* ptr, size_t size);
77  ssize_t _readNonRt(void* ptr, size_t size);
78  ssize_t _readRt(void* ptr, size_t size);
79  static std::string defaultName;
80  std::string name;
81  std::string path;
82  int pipeSocket;
83  int fd;
84  int pipeSize;
85  double timeoutMsRt = 0;
86  double timeoutMsNonRt = 0;
87  bool blockingRt = false;
88  bool blockingNonRt = false;
89 };
90 
91 template<typename T> bool Pipe::writeNonRt(const T& data)
92 {
93  return writeNonRt(&data, 1);
94 }
95 
96 template <typename T> bool Pipe::writeNonRt(T* data, size_t count)
97 {
98  size_t size = count * sizeof(*data);
99  return _writeNonRt((void*)data, size);
100 }
101 
102 template<typename T> bool Pipe::writeRt(const T& data)
103 {
104  return writeRt(&data, 1);
105 }
106 
107 template <typename T> bool Pipe::writeRt(T* ptr, size_t count)
108 {
109  size_t size = count * sizeof(*ptr);
110  return _writeRt((void*)ptr, size);
111 }
112 
113 template<typename T> ssize_t Pipe::readRt(T & dest)
114 {
115  return readRt(&dest, 1);
116 }
117 
118 template<typename T> ssize_t Pipe::readRt(T* dest, size_t count)
119 {
120  ssize_t ret = _readRt((void*)dest, count * sizeof(*dest));
121  if(ret >= 0)
122  return ret / sizeof(*dest);
123  else
124  return ret;
125 }
126 
127 template<typename T> ssize_t Pipe::readNonRt(T & dest)
128 {
129  return readNonRt(&dest, 1);
130 }
131 
132 template<typename T> ssize_t Pipe::readNonRt(T* dest, size_t count)
133 {
134  ssize_t ret = _readNonRt((void*)dest, count * sizeof(*dest));
135  if(ret >= 0)
136  return ret / sizeof(*dest);
137  else
138  return ret;
139 }
bool writeNonRt(const T &data)
Definition: Pipe.h:91
void setTimeoutMsRt(double timeoutMs)
Definition: Pipe.cpp:75
void setTimeoutMsNonRt(double timeoutMs)
Definition: Pipe.cpp:80
void setBlockingRt(bool blocking)
Definition: Pipe.cpp:43
ssize_t readNonRt(T &dest)
Definition: Pipe.h:127
ssize_t readRt(T &dest)
Definition: Pipe.h:113
void setBlockingNonRt(bool blocking)
Definition: Pipe.cpp:59
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:102
Definition: Pipe.h:9