feat(plugins): add c2.WebSocket (#6076)
This commit is contained in:
+15
-17
@@ -9,32 +9,31 @@ namespace chatterino {
|
||||
// Taken from
|
||||
// https://stackoverflow.com/questions/21646467/how-to-execute-a-functor-or-a-lambda-in-a-given-thread-in-qt-gcd-style
|
||||
// Qt 5/4 - preferred, has least allocations
|
||||
template <typename F>
|
||||
static void postToThread(F &&fun, QObject *obj = QCoreApplication::instance())
|
||||
static void postToThread(auto &&f, QObject *obj = QCoreApplication::instance())
|
||||
{
|
||||
struct Event : public QEvent {
|
||||
using Fun = typename std::decay<F>::type;
|
||||
using Fun = typename std::decay_t<decltype(f)>;
|
||||
Fun fun;
|
||||
Event(Fun &&fun)
|
||||
|
||||
Event(decltype(fun) f)
|
||||
: QEvent(QEvent::None)
|
||||
, fun(std::move(fun))
|
||||
{
|
||||
}
|
||||
Event(const Fun &fun)
|
||||
: QEvent(QEvent::None)
|
||||
, fun(fun)
|
||||
, fun(std::forward<decltype(fun)>(f))
|
||||
{
|
||||
}
|
||||
Event(const Event &) = delete;
|
||||
Event(Event &&) = delete;
|
||||
Event &operator=(const Event &) = delete;
|
||||
Event &operator=(Event &&) = delete;
|
||||
|
||||
~Event() override
|
||||
{
|
||||
fun();
|
||||
}
|
||||
};
|
||||
QCoreApplication::postEvent(obj, new Event(std::forward<F>(fun)));
|
||||
QCoreApplication::postEvent(obj, new Event(std::forward<decltype(f)>(f)));
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
static void runInGuiThread(F &&fun)
|
||||
static void runInGuiThread(auto &&fun)
|
||||
{
|
||||
if (isGuiThread())
|
||||
{
|
||||
@@ -42,17 +41,16 @@ static void runInGuiThread(F &&fun)
|
||||
}
|
||||
else
|
||||
{
|
||||
postToThread(fun);
|
||||
postToThread(std::forward<decltype(fun)>(fun));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename F>
|
||||
inline void postToGuiThread(F &&fun)
|
||||
inline void postToGuiThread(auto &&fun)
|
||||
{
|
||||
assert(!isGuiThread() &&
|
||||
"postToGuiThread must be called from a non-GUI thread");
|
||||
|
||||
postToThread(std::forward<F>(fun));
|
||||
postToThread(std::forward<decltype(fun)>(fun));
|
||||
}
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
#pragma once
|
||||
|
||||
#include <boost/beast/core/buffer_traits.hpp>
|
||||
#include <QByteArray>
|
||||
|
||||
namespace chatterino {
|
||||
|
||||
/// A ConstBufferSequence over a single QByteArray.
|
||||
/// https://www.boost.org/doc/libs/1_87_0/doc/html/boost_asio/reference/ConstBufferSequence.html
|
||||
struct QByteArrayBuffer {
|
||||
struct QByteArrayHolder {
|
||||
QByteArray data;
|
||||
|
||||
operator boost::asio::const_buffer() const
|
||||
{
|
||||
return {
|
||||
this->data.constData(),
|
||||
static_cast<size_t>(this->data.size()),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
struct ConstIterator : public std::bidirectional_iterator_tag {
|
||||
// using iterator_category = std::bidirectional_iterator_tag;
|
||||
using value_type = QByteArrayHolder;
|
||||
using difference_type = ptrdiff_t;
|
||||
using pointer = const QByteArrayHolder *;
|
||||
using reference = const QByteArrayHolder &;
|
||||
|
||||
constexpr ConstIterator() noexcept = default;
|
||||
|
||||
constexpr explicit ConstIterator(pointer ptr) noexcept
|
||||
: ptr(ptr)
|
||||
{
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr reference operator*() const noexcept
|
||||
{
|
||||
return *ptr;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr pointer operator->() const noexcept
|
||||
{
|
||||
return ptr;
|
||||
}
|
||||
|
||||
constexpr ConstIterator &operator++() noexcept
|
||||
{
|
||||
++ptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr ConstIterator operator++(int) noexcept
|
||||
{
|
||||
ConstIterator tmp = *this;
|
||||
++ptr;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
constexpr ConstIterator &operator--() noexcept
|
||||
{
|
||||
--ptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
constexpr ConstIterator operator--(int) noexcept
|
||||
{
|
||||
ConstIterator tmp = *this;
|
||||
--ptr;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
[[nodiscard]] constexpr auto operator==(
|
||||
const ConstIterator &rhs) const noexcept
|
||||
{
|
||||
return this->ptr == rhs.ptr;
|
||||
}
|
||||
|
||||
const QByteArrayHolder *ptr = nullptr;
|
||||
};
|
||||
|
||||
using value_type = QByteArrayHolder;
|
||||
using size_type = size_t;
|
||||
using difference_type = ptrdiff_t;
|
||||
using pointer = QByteArrayHolder *;
|
||||
using const_pointer = const QByteArrayHolder *;
|
||||
using reference = QByteArrayHolder &;
|
||||
using const_reference = const QByteArrayHolder &;
|
||||
using iterator = ConstIterator;
|
||||
using const_iterator = ConstIterator;
|
||||
|
||||
QByteArrayBuffer(QByteArray ba)
|
||||
: holder({std::move(ba)})
|
||||
{
|
||||
}
|
||||
|
||||
const_iterator begin() const
|
||||
{
|
||||
return ConstIterator(&this->holder);
|
||||
}
|
||||
|
||||
const_iterator end() const
|
||||
{
|
||||
return ConstIterator((&this->holder) + 1);
|
||||
}
|
||||
|
||||
QByteArray data() const
|
||||
{
|
||||
return this->holder.data;
|
||||
}
|
||||
|
||||
QByteArrayHolder holder;
|
||||
};
|
||||
|
||||
static_assert(sizeof(QByteArrayBuffer) == sizeof(QByteArray));
|
||||
|
||||
} // namespace chatterino
|
||||
|
||||
static_assert(boost::beast::is_const_buffer_sequence<
|
||||
chatterino::QByteArrayBuffer>::value);
|
||||
Reference in New Issue
Block a user