xbmc
PipesManager.h
1 /*
2  * Many concepts and protocol are taken from
3  * the Boxee project. http://www.boxee.tv
4  *
5  * Copyright (C) 2011-2018 Team Kodi
6  * This file is part of Kodi - https://kodi.tv
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  * See LICENSES/README.md for more information.
10  */
11 
12 #pragma once
13 
14 #include "threads/CriticalSection.h"
15 #include "threads/Event.h"
16 #include "utils/RingBuffer.h"
17 
18 #include <map>
19 #include <string>
20 #include <vector>
21 
22 #define PIPE_DEFAULT_MAX_SIZE (6 * 1024 * 1024)
23 
24 namespace XFILE
25 {
26 
28 {
29 public:
30  virtual ~IPipeListener() = default;
31  virtual void OnPipeOverFlow() = 0;
32  virtual void OnPipeUnderFlow() = 0;
33 };
34 
35 class Pipe
36  {
37  public:
38  Pipe(const std::string &name, int nMaxSize = PIPE_DEFAULT_MAX_SIZE );
39  virtual ~Pipe();
40  const std::string &GetName();
41 
42  void AddRef();
43  void DecRef(); // a pipe does NOT delete itself with ref-count 0.
44  int RefCount();
45 
46  bool IsEmpty();
47 
55  int Read(char *buf, int nMaxSize, int nWaitMillis = -1);
56 
64  bool Write(const char *buf, int nSize, int nWaitMillis = -1);
65 
66  void Flush();
67 
68  void CheckStatus();
69  void Close();
70 
71  void AddListener(IPipeListener *l);
72  void RemoveListener(IPipeListener *l);
73 
74  void SetEof();
75  bool IsEof();
76 
77  int GetAvailableRead();
78  void SetOpenThreshold(int threshold);
79 
80  protected:
81 
82  bool m_bOpen;
83  bool m_bReadyForRead;
84 
85  bool m_bEof;
86  CRingBuffer m_buffer;
87  std::string m_strPipeName;
88  int m_nRefCount;
89  int m_nOpenThreshold;
90 
91  CEvent m_readEvent;
92  CEvent m_writeEvent;
93 
94  std::vector<XFILE::IPipeListener *> m_listeners;
95 
96  CCriticalSection m_lock;
97  };
98 
99 
101 {
102 public:
103  virtual ~PipesManager();
104  static PipesManager &GetInstance();
105 
106  std::string GetUniquePipeName();
107  XFILE::Pipe *CreatePipe(const std::string &name="", int nMaxPipeSize = PIPE_DEFAULT_MAX_SIZE);
108  XFILE::Pipe *OpenPipe(const std::string &name);
109  void ClosePipe(XFILE::Pipe *pipe);
110  bool Exists(const std::string &name);
111 
112 protected:
113  int m_nGenIdHelper = 1;
114  std::map<std::string, XFILE::Pipe *> m_pipes;
115 
116  CCriticalSection m_lock;
117 };
118 
119 }
120 
This is an Event class built from a ConditionVariable.
Definition: Event.h:35
Definition: PipesManager.h:100
Definition: PipesManager.h:27
Definition: Scraper.h:41
Definition: RingBuffer.h:13
Definition: PipesManager.h:35