[cosmetics] update date in GPL header
[vuplus_xbmc] / xbmc / filesystem / PipesManager.h
1 /*
2  * Many concepts and protocol are taken from
3  * the Boxee project. http://www.boxee.tv
4  * 
5  *      Copyright (C) 2011-2013 Team XBMC
6  *      http://www.xbmc.org
7  *
8  *  This Program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 2, or (at your option)
11  *  any later version.
12  *
13  *  This Program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with XBMC; see the file COPYING.  If not, see
20  *  <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #ifndef __PIPES_MANAGER__H__
25 #define __PIPES_MANAGER__H__
26
27 #include "threads/CriticalSection.h"
28 #include "threads/Event.h"
29 #include "utils/StdString.h"
30 #include "utils/RingBuffer.h"
31
32 #include <map>
33
34 #define PIPE_DEFAULT_MAX_SIZE (6 * 1024 * 1024)
35
36 namespace XFILE
37 {
38  
39 class IPipeListener
40 {
41 public:
42   virtual ~IPipeListener() {}
43   virtual void OnPipeOverFlow() = 0;
44   virtual void OnPipeUnderFlow() = 0;
45 };
46   
47 class Pipe
48   {
49   public:
50     Pipe(const CStdString &name, int nMaxSize = PIPE_DEFAULT_MAX_SIZE ); 
51     virtual ~Pipe();
52     const CStdString &GetName();
53     
54     void AddRef();
55     void DecRef();   // a pipe does NOT delete itself with ref-count 0. 
56     int  RefCount(); 
57     
58     bool IsEmpty();
59
60     /**
61      * Read into the buffer from the Pipe the num of bytes asked for
62      * blocking forever (which happens to be 5 minutes in this case).
63      *
64      * In the case where nWaitMillis is provided block for that number
65      * of milliseconds instead.
66      */
67     int  Read(char *buf, int nMaxSize, int nWaitMillis = -1);
68
69     /**
70      * Write into the Pipe from the buffer the num of bytes asked for
71      * blocking forever.
72      *
73      * In the case where nWaitMillis is provided block for that number
74      * of milliseconds instead.
75      */
76     bool Write(const char *buf, int nSize, int nWaitMillis = -1);
77
78     void Flush();
79     
80     void CheckStatus();
81     void Close();
82     
83     void AddListener(IPipeListener *l);
84     void RemoveListener(IPipeListener *l);
85     
86     void SetEof();
87     bool IsEof();
88     
89     int GetAvailableRead();
90     void SetOpenThreashold(int threashold);
91
92   protected:
93     
94     bool        m_bOpen;
95     bool        m_bReadyForRead;
96
97     bool        m_bEof;
98     CRingBuffer m_buffer;
99     CStdString  m_strPipeName;  
100     int         m_nRefCount;
101     int         m_nOpenThreashold;
102
103     CEvent     m_readEvent;
104     CEvent     m_writeEvent;
105     
106     std::vector<XFILE::IPipeListener *> m_listeners;
107     
108     CCriticalSection m_lock;
109   };
110
111   
112 class PipesManager
113 {
114 public:
115   virtual ~PipesManager();
116   static PipesManager &GetInstance();
117
118   CStdString   GetUniquePipeName();
119   XFILE::Pipe *CreatePipe(const CStdString &name="", int nMaxPipeSize = PIPE_DEFAULT_MAX_SIZE);
120   XFILE::Pipe *OpenPipe(const CStdString &name);
121   void         ClosePipe(XFILE::Pipe *pipe);
122   bool         Exists(const CStdString &name);
123   
124 protected:
125   PipesManager();
126   int    m_nGenIdHelper;
127   std::map<CStdString, XFILE::Pipe *> m_pipes;  
128   
129   CCriticalSection m_lock;
130 };
131
132 }
133
134 #endif
135