Merge pull request #5039 from CEikermann/patch-1
[vuplus_xbmc] / xbmc / filesystem / PipeFile.cpp
1 /*
2  *      Copyright (C) 2011-2013 Team XBMC
3  *      http://xbmc.org
4  *
5  *  This Program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2, or (at your option)
8  *  any later version.
9  *
10  *  This Program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with XBMC; see the file COPYING.  If not, see
17  *  <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 #include "PipeFile.h"
22 #include "threads/SingleLock.h"
23 #include "PipesManager.h"
24 #include "utils/StringUtils.h"
25 #include "URL.h"
26
27 using namespace XFILE;
28
29 CPipeFile::CPipeFile() : m_pos(0), m_length(-1), m_pipe(NULL)
30 {
31 }
32
33 CPipeFile::~CPipeFile()
34 {
35   Close();
36 }
37
38 int64_t CPipeFile::GetPosition()
39 {
40   return m_pos;
41 }
42
43 int64_t CPipeFile::GetLength()
44 {
45   return m_length;
46 }
47
48 void CPipeFile::SetLength(int64_t len)
49 {
50   m_length = len;
51 }
52
53 bool CPipeFile::Open(const CURL& url)
54 {
55   CStdString name = url.Get();
56   m_pipe = PipesManager::GetInstance().OpenPipe(name);
57   if (m_pipe)
58     m_pipe->AddListener(this);
59   return (m_pipe != NULL);
60 }
61
62 bool CPipeFile::Exists(const CURL& url)
63 {
64   CStdString name = url.Get();
65   return PipesManager::GetInstance().Exists(name);
66 }
67
68 int CPipeFile::Stat(const CURL& url, struct __stat64* buffer)
69 {
70   return -1;
71 }
72
73 int CPipeFile::Stat(struct __stat64* buffer)
74 {
75   memset(buffer,0,sizeof(struct __stat64));
76   buffer->st_size = m_length;
77   return 0;
78 }
79
80 unsigned int CPipeFile::Read(void* lpBuf, int64_t uiBufSize)
81 {
82   if (!m_pipe)
83     return -1;
84   
85   return m_pipe->Read((char *)lpBuf,(int)uiBufSize);
86 }
87
88 int CPipeFile::Write(const void* lpBuf, int64_t uiBufSize)
89 {
90   if (!m_pipe)
91     return -1;
92   
93   // m_pipe->Write return bool. either all was written or not.
94   return m_pipe->Write((const char *)lpBuf,(int)uiBufSize) ? (int)uiBufSize : 0;
95 }
96
97 void CPipeFile::SetEof()
98 {
99   if (!m_pipe)
100     return ;
101   m_pipe->SetEof();
102 }
103
104 bool CPipeFile::IsEof()
105 {
106   if (!m_pipe)
107     return true;
108   return m_pipe->IsEof();
109 }
110
111 bool CPipeFile::IsEmpty()
112 {
113   if (!m_pipe)
114     return true;
115   return m_pipe->IsEmpty();
116 }
117
118 int64_t CPipeFile::Seek(int64_t iFilePosition, int iWhence)
119 {
120   return -1;
121 }
122
123 void CPipeFile::Close()
124 {
125   if (m_pipe)
126   {
127     m_pipe->RemoveListener(this);
128     PipesManager::GetInstance().ClosePipe(m_pipe);    
129   }
130   m_pipe = NULL;
131 }
132
133 bool CPipeFile::IsClosed()
134 {
135   return (m_pipe == NULL);
136 }
137
138 void CPipeFile::Flush()
139 {
140   if (m_pipe)
141     m_pipe->Flush();
142 }
143
144 bool CPipeFile::OpenForWrite(const CURL& url, bool bOverWrite)
145 {
146   CStdString name = url.Get();
147
148   m_pipe = PipesManager::GetInstance().CreatePipe(name);
149   if (m_pipe)
150     m_pipe->AddListener(this);
151   return (m_pipe != NULL);
152 }
153
154 bool CPipeFile::Delete(const CURL& url)
155 {
156   return false;
157 }
158
159 bool CPipeFile::Rename(const CURL& url, const CURL& urlnew)
160 {
161   return false;
162 }
163
164 int CPipeFile::IoControl(int request, void* param)
165 {
166   return -1;
167 }
168
169 CStdString CPipeFile::GetName() const
170 {
171   if (!m_pipe)
172     return StringUtils::EmptyString;
173   return m_pipe->GetName();
174 }
175
176 void CPipeFile::OnPipeOverFlow()
177 {
178   CSingleLock lock(m_lock);
179   for (size_t l=0; l<m_listeners.size(); l++)
180     m_listeners[l]->OnPipeOverFlow();
181 }
182
183 int64_t CPipeFile::GetAvailableRead()
184 {
185   return m_pipe->GetAvailableRead();
186 }
187
188 void CPipeFile::OnPipeUnderFlow()
189 {
190   for (size_t l=0; l<m_listeners.size(); l++)
191     m_listeners[l]->OnPipeUnderFlow();
192 }
193
194 void CPipeFile::AddListener(IPipeListener *l)
195 {
196   CSingleLock lock(m_lock);
197   for (size_t i=0; i<m_listeners.size(); i++)
198   {
199     if (m_listeners[i] == l)
200       return;
201   }
202   m_listeners.push_back(l);
203 }
204
205 void CPipeFile::RemoveListener(IPipeListener *l)
206 {
207   CSingleLock lock(m_lock);
208   std::vector<XFILE::IPipeListener *>::iterator i = m_listeners.begin();
209   while(i != m_listeners.end())
210   {
211     if ( (*i) == l)
212       i = m_listeners.erase(i);
213     else
214       i++;
215   }
216 }
217
218 void CPipeFile::SetOpenThreashold(int threashold)
219 {
220   m_pipe->SetOpenThreashold(threashold);
221 }
222