1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
/*
* Copyright (C) 2011-2018 Team Kodi
* This file is part of Kodi - https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSES/README.md for more information.
*/
#include "PipesManager.h"
#include "utils/StringUtils.h"
#include <algorithm>
#include <mutex>
using namespace XFILE;
using namespace std::chrono_literals;
Pipe::Pipe(const std::string &name, int nMaxSize)
{
m_buffer.Create(nMaxSize);
m_nRefCount = 1;
m_readEvent.Reset();
m_writeEvent.Set();
m_strPipeName = name;
m_bOpen = true;
m_bEof = false;
m_nOpenThreshold = PIPE_DEFAULT_MAX_SIZE / 2;
m_bReadyForRead = true; // open threshold disabled atm
}
Pipe::~Pipe() = default;
void Pipe::SetOpenThreshold(int threshold)
{
m_nOpenThreshold = threshold;
}
const std::string &Pipe::GetName()
{
return m_strPipeName;
}
void Pipe::AddRef()
{
std::unique_lock<CCriticalSection> lock(m_lock);
m_nRefCount++;
}
void Pipe::DecRef()
{
std::unique_lock<CCriticalSection> lock(m_lock);
m_nRefCount--;
}
int Pipe::RefCount()
{
std::unique_lock<CCriticalSection> lock(m_lock);
return m_nRefCount;
}
void Pipe::SetEof()
{
m_bEof = true;
}
bool Pipe::IsEof()
{
return m_bEof;
}
bool Pipe::IsEmpty()
{
return (m_buffer.getMaxReadSize() == 0);
}
void Pipe::Flush()
{
std::unique_lock<CCriticalSection> lock(m_lock);
if (!m_bOpen || !m_bReadyForRead || m_bEof)
{
return;
}
m_buffer.Clear();
CheckStatus();
}
int Pipe::Read(char *buf, int nMaxSize, int nWaitMillis)
{
std::unique_lock<CCriticalSection> lock(m_lock);
if (!m_bOpen)
{
return -1;
}
while (!m_bReadyForRead && !m_bEof)
m_readEvent.Wait(100ms);
int nResult = 0;
if (!IsEmpty())
{
int nToRead = std::min(static_cast<int>(m_buffer.getMaxReadSize()), nMaxSize);
m_buffer.ReadData(buf, nToRead);
nResult = nToRead;
}
else if (m_bEof)
{
nResult = 0;
}
else
{
// we're leaving the guard - add ref to make sure we are not getting erased.
// at the moment we leave m_listeners unprotected which might be a problem in future
// but as long as we only have 1 listener attaching at startup and detaching on close we're fine
AddRef();
lock.unlock();
bool bHasData = false;
auto nMillisLeft = std::chrono::milliseconds(nWaitMillis);
if (nMillisLeft < 0ms)
nMillisLeft = 300000ms; // arbitrary. 5 min.
do
{
for (size_t l=0; l<m_listeners.size(); l++)
m_listeners[l]->OnPipeUnderFlow();
bHasData = m_readEvent.Wait(std::min(200ms, nMillisLeft));
nMillisLeft -= 200ms;
} while (!bHasData && nMillisLeft > 0ms && !m_bEof);
lock.lock();
DecRef();
if (!m_bOpen)
return -1;
if (bHasData)
{
int nToRead = std::min(static_cast<int>(m_buffer.getMaxReadSize()), nMaxSize);
m_buffer.ReadData(buf, nToRead);
nResult = nToRead;
}
}
CheckStatus();
return nResult;
}
bool Pipe::Write(const char *buf, int nSize, int nWaitMillis)
{
std::unique_lock<CCriticalSection> lock(m_lock);
if (!m_bOpen)
return false;
bool bOk = false;
int writeSize = m_buffer.getMaxWriteSize();
if (writeSize > nSize)
{
m_buffer.WriteData(buf, nSize);
bOk = true;
}
else
{
while ( (int)m_buffer.getMaxWriteSize() < nSize && m_bOpen )
{
lock.unlock();
for (size_t l=0; l<m_listeners.size(); l++)
m_listeners[l]->OnPipeOverFlow();
bool bClear = nWaitMillis < 0 ? m_writeEvent.Wait()
: m_writeEvent.Wait(std::chrono::milliseconds(nWaitMillis));
lock.lock();
if (bClear && (int)m_buffer.getMaxWriteSize() >= nSize)
{
m_buffer.WriteData(buf, nSize);
bOk = true;
break;
}
// FIXME: is this right? Shouldn't we see if the time limit has been reached?
if (nWaitMillis > 0)
break;
}
}
CheckStatus();
return bOk && m_bOpen;
}
void Pipe::CheckStatus()
{
if (m_bEof)
{
m_writeEvent.Set();
m_readEvent.Set();
return;
}
if (m_buffer.getMaxWriteSize() == 0)
m_writeEvent.Reset();
else
m_writeEvent.Set();
if (m_buffer.getMaxReadSize() == 0)
m_readEvent.Reset();
else
{
if (!m_bReadyForRead && (int)m_buffer.getMaxReadSize() >= m_nOpenThreshold)
m_bReadyForRead = true;
m_readEvent.Set();
}
}
void Pipe::Close()
{
std::unique_lock<CCriticalSection> lock(m_lock);
m_bOpen = false;
m_readEvent.Set();
m_writeEvent.Set();
}
void Pipe::AddListener(IPipeListener *l)
{
std::unique_lock<CCriticalSection> lock(m_lock);
for (size_t i=0; i<m_listeners.size(); i++)
{
if (m_listeners[i] == l)
return;
}
m_listeners.push_back(l);
}
void Pipe::RemoveListener(IPipeListener *l)
{
std::unique_lock<CCriticalSection> lock(m_lock);
std::vector<XFILE::IPipeListener *>::iterator i = m_listeners.begin();
while(i != m_listeners.end())
{
if ( (*i) == l)
i = m_listeners.erase(i);
else
++i;
}
}
int Pipe::GetAvailableRead()
{
std::unique_lock<CCriticalSection> lock(m_lock);
return m_buffer.getMaxReadSize();
}
PipesManager::~PipesManager() = default;
PipesManager &PipesManager::GetInstance()
{
static PipesManager instance;
return instance;
}
std::string PipesManager::GetUniquePipeName()
{
std::unique_lock<CCriticalSection> lock(m_lock);
return StringUtils::Format("pipe://{}/", m_nGenIdHelper++);
}
XFILE::Pipe *PipesManager::CreatePipe(const std::string &name, int nMaxPipeSize)
{
std::string pName = name;
if (pName.empty())
pName = GetUniquePipeName();
std::unique_lock<CCriticalSection> lock(m_lock);
if (m_pipes.find(pName) != m_pipes.end())
return NULL;
XFILE::Pipe *p = new XFILE::Pipe(pName, nMaxPipeSize);
m_pipes[pName] = p;
return p;
}
XFILE::Pipe *PipesManager::OpenPipe(const std::string &name)
{
std::unique_lock<CCriticalSection> lock(m_lock);
if (m_pipes.find(name) == m_pipes.end())
return NULL;
m_pipes[name]->AddRef();
return m_pipes[name];
}
void PipesManager::ClosePipe(XFILE::Pipe *pipe)
{
std::unique_lock<CCriticalSection> lock(m_lock);
if (!pipe)
return ;
pipe->DecRef();
if (pipe->RefCount() == 0)
{
pipe->Close();
m_pipes.erase(pipe->GetName());
delete pipe;
}
}
bool PipesManager::Exists(const std::string &name)
{
std::unique_lock<CCriticalSection> lock(m_lock);
return (m_pipes.find(name) != m_pipes.end());
}
|