blob: 401e03d92185d49e34e1c7cd6c20a52feb50aaf1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
/*
* Copyright (c) 2021, Dex♪ <dexes.ttp@gmail.com>
* Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/EventLoop.h>
#include <LibCore/Socket.h>
#include <LibWebSocket/Impl/WebSocketImplSerenity.h>
namespace WebSocket {
WebSocketImplSerenity::~WebSocketImplSerenity() = default;
WebSocketImplSerenity::WebSocketImplSerenity() = default;
bool WebSocketImplSerenity::can_read_line()
{
return MUST(m_socket->can_read_line());
}
bool WebSocketImplSerenity::send(ReadonlyBytes bytes)
{
return !m_socket->write_entire_buffer(bytes).is_error();
}
bool WebSocketImplSerenity::eof()
{
return m_socket->is_eof();
}
void WebSocketImplSerenity::discard_connection()
{
m_socket = nullptr;
}
void WebSocketImplSerenity::connect(ConnectionInfo const& connection_info)
{
VERIFY(!m_socket);
VERIFY(on_connected);
VERIFY(on_connection_error);
VERIFY(on_ready_to_read);
auto socket_result = [&]() -> ErrorOr<NonnullOwnPtr<Core::BufferedSocketBase>> {
if (connection_info.is_secure()) {
TLS::Options options;
options.set_alert_handler([this](auto) {
on_connection_error();
});
return TRY(Core::BufferedSocket<TLS::TLSv12>::create(
TRY(TLS::TLSv12::connect(connection_info.url().host(), connection_info.url().port_or_default(), move(options)))));
}
return TRY(Core::BufferedTCPSocket::create(
TRY(Core::TCPSocket::connect(connection_info.url().host(), connection_info.url().port_or_default()))));
}();
if (socket_result.is_error()) {
Core::deferred_invoke([this] {
on_connection_error();
});
return;
}
m_socket = socket_result.release_value();
m_socket->on_ready_to_read = [this] {
on_ready_to_read();
};
Core::deferred_invoke([this] {
on_connected();
});
}
ErrorOr<ByteBuffer> WebSocketImplSerenity::read(int max_size)
{
auto buffer = TRY(ByteBuffer::create_uninitialized(max_size));
auto read_bytes = TRY(m_socket->read(buffer));
return buffer.slice(0, read_bytes.size());
}
ErrorOr<DeprecatedString> WebSocketImplSerenity::read_line(size_t size)
{
auto buffer = TRY(ByteBuffer::create_uninitialized(size));
auto line = TRY(m_socket->read_line(buffer));
return line.to_deprecated_string();
}
}
|