LeechCraft  0.6.70-16373-g319c272718
Modular cross-platform feature rich live environment.
channeldevice.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #include "channeldevice.h"
10 #include <cstring>
11 
12 namespace LC::Util
13 {
15  {
16  return true;
17  }
18 
19  bool ChannelDevice::atEnd () const
20  {
21  if (!Finished_)
22  return false;
23 
24  const std::lock_guard g { Mutex_ };
25  return Chunks_.empty ();
26  }
27 
29  {
30  Finished_ = true;
31  }
32 
33  qint64 ChannelDevice::readData (char *data, qint64 maxSize)
34  {
35  QVector<QByteArray> consuming;
36 
37  {
38  const std::lock_guard g { Mutex_ };
39  consuming.reserve (Chunks_.size ());
40 
41  while (!Chunks_.empty ())
42  {
43  auto& chunk = Chunks_.front ();
44  if (chunk.size () <= maxSize)
45  {
46  maxSize -= chunk.size ();
47  consuming.push_back (std::move (chunk));
48  Chunks_.pop_front ();
49  }
50  else
51  {
52  consuming.push_back (chunk.left (maxSize));
53  chunk.remove (0, maxSize);
54  break;
55  }
56  }
57  }
58 
59  qint64 read = 0;
60  for (const auto& chunk : consuming)
61  {
62  std::memcpy (data + read, chunk.constData (), chunk.size ());
63  read += chunk.size ();
64  }
65 
66  return read;
67  }
68 
69  qint64 ChannelDevice::writeData (const char *data, qint64 maxSize)
70  {
71  {
72  const std::lock_guard g { Mutex_ };
73  Chunks_.emplace_back (data, maxSize);
74  }
75 
76  emit readyRead ();
77 
78  return maxSize;
79  }
80 }
qint64 readData(char *, qint64) override
qint64 writeData(const char *, qint64) override
bool isSequential() const override
bool atEnd() const override