2 * Copyright (C) 2011-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
11 #include "PipesManager.h"
16 using namespace XFILE
;
18 CPipeFile::CPipeFile() : m_pipe(NULL
)
22 CPipeFile::~CPipeFile()
27 int64_t CPipeFile::GetPosition()
32 int64_t CPipeFile::GetLength()
37 void CPipeFile::SetLength(int64_t len
)
42 bool CPipeFile::Open(const CURL
& url
)
44 std::string name
= url
.Get();
45 m_pipe
= PipesManager::GetInstance().OpenPipe(name
);
47 m_pipe
->AddListener(this);
48 return (m_pipe
!= NULL
);
51 bool CPipeFile::Exists(const CURL
& url
)
53 std::string name
= url
.Get();
54 return PipesManager::GetInstance().Exists(name
);
57 int CPipeFile::Stat(const CURL
& url
, struct __stat64
* buffer
)
62 int CPipeFile::Stat(struct __stat64
* buffer
)
68 buffer
->st_size
= m_length
;
72 ssize_t
CPipeFile::Read(void* lpBuf
, size_t uiBufSize
)
77 if (uiBufSize
> SSIZE_MAX
)
78 uiBufSize
= SSIZE_MAX
;
80 return m_pipe
->Read((char *)lpBuf
,(int)uiBufSize
);
83 ssize_t
CPipeFile::Write(const void* lpBuf
, size_t uiBufSize
)
88 // m_pipe->Write return bool. either all was written or not.
89 return m_pipe
->Write((const char *)lpBuf
,uiBufSize
) ? uiBufSize
: -1;
92 void CPipeFile::SetEof()
99 bool CPipeFile::IsEof()
103 return m_pipe
->IsEof();
106 bool CPipeFile::IsEmpty()
110 return m_pipe
->IsEmpty();
113 int64_t CPipeFile::Seek(int64_t iFilePosition
, int iWhence
)
118 void CPipeFile::Close()
122 m_pipe
->RemoveListener(this);
123 PipesManager::GetInstance().ClosePipe(m_pipe
);
128 bool CPipeFile::IsClosed()
130 return (m_pipe
== NULL
);
133 void CPipeFile::Flush()
139 bool CPipeFile::OpenForWrite(const CURL
& url
, bool bOverWrite
)
141 std::string name
= url
.Get();
143 m_pipe
= PipesManager::GetInstance().CreatePipe(name
);
145 m_pipe
->AddListener(this);
146 return (m_pipe
!= NULL
);
149 bool CPipeFile::Delete(const CURL
& url
)
154 bool CPipeFile::Rename(const CURL
& url
, const CURL
& urlnew
)
159 int CPipeFile::IoControl(EIoControl
, void* param
)
164 std::string
CPipeFile::GetName() const
168 return m_pipe
->GetName();
171 void CPipeFile::OnPipeOverFlow()
173 std::unique_lock
<CCriticalSection
> lock(m_lock
);
174 for (size_t l
=0; l
<m_listeners
.size(); l
++)
175 m_listeners
[l
]->OnPipeOverFlow();
178 int64_t CPipeFile::GetAvailableRead()
180 return m_pipe
->GetAvailableRead();
183 void CPipeFile::OnPipeUnderFlow()
185 for (size_t l
=0; l
<m_listeners
.size(); l
++)
186 m_listeners
[l
]->OnPipeUnderFlow();
189 void CPipeFile::AddListener(IPipeListener
*l
)
191 std::unique_lock
<CCriticalSection
> lock(m_lock
);
192 for (size_t i
=0; i
<m_listeners
.size(); i
++)
194 if (m_listeners
[i
] == l
)
197 m_listeners
.push_back(l
);
200 void CPipeFile::RemoveListener(IPipeListener
*l
)
202 std::unique_lock
<CCriticalSection
> lock(m_lock
);
203 std::vector
<XFILE::IPipeListener
*>::iterator i
= m_listeners
.begin();
204 while(i
!= m_listeners
.end())
207 i
= m_listeners
.erase(i
);
213 void CPipeFile::SetOpenThreshold(int threshold
)
215 m_pipe
->SetOpenThreshold(threshold
);