forked from WebKit/WebKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SocketConnection.cpp
277 lines (238 loc) · 9.76 KB
/
SocketConnection.cpp
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Copyright (C) 2019 Igalia, S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "SocketConnection.h"
#include <cstring>
#include <gio/gio.h>
#include <wtf/ByteOrder.h>
#include <wtf/CheckedArithmetic.h>
#include <wtf/FastMalloc.h>
#include <wtf/RunLoop.h>
namespace WTF {
static const unsigned defaultBufferSize = 4096;
SocketConnection::SocketConnection(GRefPtr<GSocketConnection>&& connection, const MessageHandlers& messageHandlers, gpointer userData)
: m_connection(WTFMove(connection))
, m_messageHandlers(messageHandlers)
, m_userData(userData)
{
relaxAdoptionRequirement();
m_readBuffer.reserveInitialCapacity(defaultBufferSize);
m_writeBuffer.reserveInitialCapacity(defaultBufferSize);
auto* socket = g_socket_connection_get_socket(m_connection.get());
g_socket_set_blocking(socket, FALSE);
m_readMonitor.start(socket, G_IO_IN, RunLoop::current(), [this, protectedThis = Ref { *this }](GIOCondition condition) -> gboolean {
if (isClosed())
return G_SOURCE_REMOVE;
if (condition & G_IO_HUP || condition & G_IO_ERR || condition & G_IO_NVAL) {
didClose();
return G_SOURCE_REMOVE;
}
ASSERT(condition & G_IO_IN);
return read();
});
}
SocketConnection::~SocketConnection() = default;
bool SocketConnection::read()
{
while (true) {
size_t previousBufferSize = m_readBuffer.size();
if (m_readBuffer.capacity() - previousBufferSize <= 0)
m_readBuffer.reserveCapacity(m_readBuffer.capacity() + defaultBufferSize);
m_readBuffer.grow(m_readBuffer.capacity());
GUniqueOutPtr<GError> error;
auto bytesRead = g_socket_receive(g_socket_connection_get_socket(m_connection.get()), m_readBuffer.data() + previousBufferSize, m_readBuffer.size() - previousBufferSize, nullptr, &error.outPtr());
if (bytesRead == -1) {
if (g_error_matches(error.get(), G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) {
m_readBuffer.shrink(previousBufferSize);
break;
}
g_warning("Error reading from socket connection: %s\n", error->message);
didClose();
return G_SOURCE_REMOVE;
}
if (!bytesRead) {
didClose();
return G_SOURCE_REMOVE;
}
m_readBuffer.shrink(previousBufferSize + bytesRead);
while (readMessage()) { }
if (isClosed())
return G_SOURCE_REMOVE;
}
return G_SOURCE_CONTINUE;
}
enum {
ByteOrderLittleEndian = 1 << 0
};
typedef uint8_t MessageFlags;
static inline bool messageIsByteSwapped(MessageFlags flags)
{
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
return !(flags & ByteOrderLittleEndian);
#else
return (flags & ByteOrderLittleEndian);
#endif
}
bool SocketConnection::readMessage()
{
if (m_readBuffer.size() < sizeof(uint32_t))
return false;
auto* messageData = m_readBuffer.data();
uint32_t bodySizeHeader;
memcpy(&bodySizeHeader, messageData, sizeof(uint32_t));
messageData += sizeof(uint32_t);
bodySizeHeader = ntohl(bodySizeHeader);
Checked<size_t> bodySize = bodySizeHeader;
MessageFlags flags;
memcpy(&flags, messageData, sizeof(MessageFlags));
messageData += sizeof(MessageFlags);
auto messageSize = sizeof(uint32_t) + sizeof(MessageFlags) + bodySize;
if (m_readBuffer.size() < messageSize)
return false;
Checked<size_t> messageNameLength = strlen(messageData);
messageNameLength++;
if (m_readBuffer.size() < messageNameLength) {
ASSERT_NOT_REACHED();
return false;
}
const auto it = m_messageHandlers.find(messageData);
if (it != m_messageHandlers.end()) {
messageData += messageNameLength.value();
GRefPtr<GVariant> parameters;
if (!it->value.first.isNull()) {
GUniquePtr<GVariantType> variantType(g_variant_type_new(it->value.first.data()));
size_t parametersSize = bodySize.value() - messageNameLength.value();
// g_variant_new_from_data() requires the memory to be properly aligned for the type being loaded,
// but it's not possible to know the alignment because g_variant_type_info_query() is not public API.
// Since GLib 2.60 g_variant_new_from_data() already checks the alignment and reallocates the buffer
// in aligned memory only if needed. For older versions we can simply ensure the memory is 8 aligned.
#if GLIB_CHECK_VERSION(2, 60, 0)
parameters = g_variant_new_from_data(variantType.get(), messageData, parametersSize, FALSE, nullptr, nullptr);
#else
auto* alignedMemory = fastAlignedMalloc(8, parametersSize);
memcpy(alignedMemory, messageData, parametersSize);
GRefPtr<GBytes> bytes = g_bytes_new_with_free_func(alignedMemory, parametersSize, [](gpointer data) {
fastAlignedFree(data);
}, alignedMemory);
parameters = g_variant_new_from_bytes(variantType.get(), bytes.get(), FALSE);
#endif
if (messageIsByteSwapped(flags))
parameters = adoptGRef(g_variant_byteswap(parameters.get()));
}
it->value.second(*this, parameters.get(), m_userData);
if (isClosed())
return false;
}
if (m_readBuffer.size() > messageSize) {
std::memmove(m_readBuffer.data(), m_readBuffer.data() + messageSize.value(), m_readBuffer.size() - messageSize.value());
m_readBuffer.shrink(m_readBuffer.size() - messageSize.value());
} else
m_readBuffer.shrink(0);
if (m_readBuffer.size() < defaultBufferSize)
m_readBuffer.shrinkCapacity(defaultBufferSize);
return true;
}
void SocketConnection::sendMessage(const char* messageName, GVariant* parameters)
{
GRefPtr<GVariant> adoptedParameters = parameters;
size_t parametersSize = parameters ? g_variant_get_size(parameters) : 0;
CheckedSize messageNameLength = strlen(messageName);
messageNameLength++;
if (UNLIKELY(messageNameLength.hasOverflowed())) {
g_warning("Trying to send message with invalid too long name");
return;
}
CheckedUint32 bodySize = messageNameLength + parametersSize;
if (UNLIKELY(bodySize.hasOverflowed())) {
g_warning("Trying to send message '%s' with invalid too long body", messageName);
return;
}
size_t previousBufferSize = m_writeBuffer.size();
m_writeBuffer.grow(previousBufferSize + sizeof(uint32_t) + sizeof(MessageFlags) + bodySize.value());
auto* messageData = m_writeBuffer.data() + previousBufferSize;
uint32_t bodySizeHeader = htonl(bodySize.value());
memcpy(messageData, &bodySizeHeader, sizeof(uint32_t));
messageData += sizeof(uint32_t);
MessageFlags flags = 0;
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
flags |= ByteOrderLittleEndian;
#endif
memcpy(messageData, &flags, sizeof(MessageFlags));
messageData += sizeof(MessageFlags);
memcpy(messageData, messageName, messageNameLength);
messageData += messageNameLength.value();
if (parameters)
memcpy(messageData, g_variant_get_data(parameters), parametersSize);
write();
}
void SocketConnection::write()
{
if (isClosed())
return;
GUniqueOutPtr<GError> error;
auto bytesWritten = g_socket_send(g_socket_connection_get_socket(m_connection.get()), m_writeBuffer.data(), m_writeBuffer.size(), nullptr, &error.outPtr());
if (bytesWritten == -1) {
if (g_error_matches(error.get(), G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK)) {
waitForSocketWritability();
return;
}
g_warning("Error sending message on socket connection: %s\n", error->message);
didClose();
return;
}
if (m_writeBuffer.size() > static_cast<size_t>(bytesWritten)) {
std::memmove(m_writeBuffer.data(), m_writeBuffer.data() + bytesWritten, m_writeBuffer.size() - bytesWritten);
m_writeBuffer.shrink(m_writeBuffer.size() - bytesWritten);
} else
m_writeBuffer.shrink(0);
if (m_writeBuffer.size() < defaultBufferSize)
m_writeBuffer.shrinkCapacity(defaultBufferSize);
if (!m_writeBuffer.isEmpty())
waitForSocketWritability();
}
void SocketConnection::waitForSocketWritability()
{
if (m_writeMonitor.isActive())
return;
m_writeMonitor.start(g_socket_connection_get_socket(m_connection.get()), G_IO_OUT, RunLoop::current(), [this, protectedThis = Ref { *this }] (GIOCondition condition) -> gboolean {
if (condition & G_IO_OUT) {
// We can't stop the monitor from this lambda, because stop destroys the lambda.
RunLoop::current().dispatch([this, protectedThis] {
m_writeMonitor.stop();
write();
});
}
return G_SOURCE_REMOVE;
});
}
void SocketConnection::close()
{
m_readMonitor.stop();
m_writeMonitor.stop();
m_connection = nullptr;
}
void SocketConnection::didClose()
{
if (isClosed())
return;
close();
ASSERT(m_messageHandlers.contains("DidClose"));
m_messageHandlers.get("DidClose").second(*this, nullptr, m_userData);
}
} // namespace WTF