WebSocketImpl.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. /*
  2. * Copyright (c) 2021, Dex♪ <dexes.ttp@gmail.com>
  3. * Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #pragma once
  9. #include <AK/ByteBuffer.h>
  10. #include <AK/Span.h>
  11. #include <AK/String.h>
  12. #include <LibCore/Object.h>
  13. #include <LibWebSocket/ConnectionInfo.h>
  14. namespace WebSocket {
  15. class WebSocketImpl : public Core::Object {
  16. C_OBJECT(WebSocketImpl);
  17. public:
  18. virtual ~WebSocketImpl() override = default;
  19. explicit WebSocketImpl(Core::Object* parent = nullptr);
  20. void connect(ConnectionInfo const&);
  21. bool can_read_line() { return MUST(m_socket->can_read_line()); }
  22. ErrorOr<String> read_line(size_t size);
  23. bool can_read() { return MUST(m_socket->can_read_without_blocking()); }
  24. ErrorOr<ByteBuffer> read(int max_size);
  25. bool send(ReadonlyBytes bytes) { return m_socket->write_or_error(bytes); }
  26. bool eof() { return m_socket->is_eof(); }
  27. void discard_connection()
  28. {
  29. m_socket.clear();
  30. }
  31. Function<void()> on_connected;
  32. Function<void()> on_connection_error;
  33. Function<void()> on_ready_to_read;
  34. private:
  35. OwnPtr<Core::Stream::BufferedSocketBase> m_socket;
  36. };
  37. }