Flutter Engine
The Flutter Engine
sync_socket.cc
Go to the documentation of this file.
1// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "bin/sync_socket.h"
6
7#include "bin/dartutils.h"
8#include "bin/io_buffer.h"
9#include "bin/isolate_data.h"
10#include "bin/lockers.h"
11#include "bin/thread.h"
12#include "bin/utils.h"
13
14#include "include/dart_api.h"
15
16#include "platform/globals.h"
17#include "platform/utils.h"
18
19#define DART_CHECK_ERROR_AND_CLEANUP(handle, ptr) \
20 do { \
21 if (Dart_IsError((handle))) { \
22 delete (ptr); \
23 Dart_SetReturnValue(args, (handle)); \
24 return; \
25 } \
26 } while (0)
27
28#define DART_CHECK_ERROR(handle) \
29 do { \
30 if (Dart_IsError((handle))) { \
31 Dart_SetReturnValue(args, (handle)); \
32 return; \
33 } \
34 } while (0)
35
36namespace dart {
37namespace bin {
38
39static constexpr int kSocketIdNativeField = 0;
40
44 args, DartUtils::NewDartArgumentError("Invalid argument count."));
45 return;
46 }
47
48 char* peer = nullptr;
49 Dart_Handle host_arg =
50 Dart_GetNativeStringArgument(args, 0, reinterpret_cast<void**>(&peer));
51 DART_CHECK_ERROR(host_arg);
52
53 char* host = nullptr;
54 host_arg = Dart_StringToCString(host_arg, const_cast<const char**>(&host));
55 DART_CHECK_ERROR(host_arg);
56
57 int64_t type = 0;
59 DART_CHECK_ERROR(port_error);
60
61 OSError* os_error = nullptr;
64 if (addresses == nullptr) {
66 return;
67 }
68
69 Dart_Handle array = Dart_NewList(addresses->count());
70 DART_CHECK_ERROR_AND_CLEANUP(array, addresses);
71
72 for (intptr_t i = 0; i < addresses->count(); i++) {
73 SocketAddress* addr = addresses->GetAt(i);
74 Dart_Handle entry = Dart_NewList(3);
75 DART_CHECK_ERROR_AND_CLEANUP(entry, addresses);
76
81
82 Dart_Handle as_string = Dart_NewStringFromCString(addr->as_string());
83 DART_CHECK_ERROR_AND_CLEANUP(as_string, addresses);
84 error = Dart_ListSetAt(entry, 1, as_string);
86
87 RawAddr raw = addr->addr();
90
91 error = Dart_ListSetAt(entry, 2, data);
93 error = Dart_ListSetAt(array, i, entry);
95 }
96 delete addresses;
98 return;
99}
100
106 DART_CHECK_ERROR(port_arg);
107 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535);
108 SocketAddress::SetAddrPort(&addr, static_cast<intptr_t>(port));
109 intptr_t socket = SynchronousSocket::CreateConnect(addr);
110 if (socket >= 0) {
115 } else {
117 }
118}
119
121 SynchronousSocket* socket = nullptr;
123 Dart_GetNativeArgument(args, 0), &socket);
125
126 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1);
127 if (!Dart_IsList(buffer_obj)) {
129 "First parameter must be a List<int>"));
130 return;
131 }
135 uint8_t* buffer = nullptr;
136 intptr_t len;
138 reinterpret_cast<void**>(&buffer), &len);
140 ASSERT((offset + length) <= len);
141 buffer += offset;
142 intptr_t bytes_written =
144 Dart_TypedDataReleaseData(buffer_obj);
145 if (bytes_written >= 0) {
146 Dart_SetIntegerReturnValue(args, bytes_written);
147 } else {
148 OSError os_error;
150 }
151}
152
154 SynchronousSocket* socket = nullptr;
156 Dart_GetNativeArgument(args, 0), &socket);
158
159 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1);
160 if (!Dart_IsList(buffer_obj)) {
162 "First parameter must be a List<int>"));
163 return;
164 }
167 intptr_t array_len = 0;
168
169 result = Dart_ListLength(buffer_obj, &array_len);
171
172 uint8_t* buffer = Dart_ScopeAllocate(bytes);
173 intptr_t bytes_read = SynchronousSocket::Read(socket->fd(), buffer, bytes);
174 if (bytes_read < 0) {
176 return;
177 }
178 if (bytes_read > 0) {
179 result = Dart_ListSetAsBytes(buffer_obj, offset, buffer, bytes_read);
181 }
182 Dart_SetIntegerReturnValue(args, bytes_read);
183}
184
186 SynchronousSocket* socket = nullptr;
188 Dart_GetNativeArgument(args, 0), &socket);
190
191 intptr_t available = SynchronousSocket::Available(socket->fd());
192 if (available >= 0) {
194 } else {
196 }
197}
198
200 SynchronousSocket* socket = nullptr;
202 Dart_GetNativeArgument(args, 0), &socket);
204
205 SynchronousSocket::Close(socket->fd());
206 socket->SetClosedFd();
207}
208
210 SynchronousSocket* socket = nullptr;
212 Dart_GetNativeArgument(args, 0), &socket);
214
215 int64_t length = 0;
217 (length < 0)) {
219 "First parameter must be an integer."));
220 return;
221 }
222 uint8_t* buffer = nullptr;
224 if (Dart_IsNull(result)) {
226 return;
227 }
228 ASSERT(buffer != nullptr);
229 intptr_t bytes_read = SynchronousSocket::Read(socket->fd(), buffer, length);
230 if (bytes_read == length) {
232 } else if (bytes_read > 0) {
233 uint8_t* new_buffer = nullptr;
234 Dart_Handle new_result = IOBuffer::Allocate(bytes_read, &new_buffer);
235 if (Dart_IsNull(new_result)) {
237 return;
238 }
239 ASSERT(new_buffer != nullptr);
240 memmove(new_buffer, buffer, bytes_read);
241 Dart_SetReturnValue(args, new_result);
242 } else if (bytes_read == -1) {
244 }
245}
246
248 SynchronousSocket* socket = nullptr;
250 Dart_GetNativeArgument(args, 0), &socket);
252
254}
255
257 SynchronousSocket* socket = nullptr;
259 Dart_GetNativeArgument(args, 0), &socket);
261
263}
264
266 SynchronousSocket* socket = nullptr;
268 Dart_GetNativeArgument(args, 0), &socket);
270
271 intptr_t port = SynchronousSocket::GetPort(socket->fd());
272 if (port > 0) {
274 } else {
276 }
277}
278
280 SynchronousSocket* socket = nullptr;
282 Dart_GetNativeArgument(args, 0), &socket);
284
285 intptr_t port = 0;
287 if (addr == nullptr) {
289 return;
290 }
291 Dart_Handle list = Dart_NewList(2);
293
294 Dart_Handle entry = Dart_NewList(3);
296
298 Dart_ListSetAt(entry, 0, Dart_NewInteger(addr->GetType()));
300 error =
301 Dart_ListSetAt(entry, 1, Dart_NewStringFromCString(addr->as_string()));
303
304 RawAddr raw = addr->addr();
307
308 error = Dart_ListSetAt(list, 0, entry);
313 delete addr;
314}
315
316static void SynchronousSocketFinalizer(void* isolate_data, void* data) {
317 SynchronousSocket* socket = reinterpret_cast<SynchronousSocket*>(data);
318 if (socket->fd() >= 0) {
319 SynchronousSocket::Close(socket->fd());
320 socket->SetClosedFd();
321 }
322 delete socket;
323}
324
326 Dart_Handle handle,
327 SynchronousSocket* socket) {
329 handle, kSocketIdNativeField, reinterpret_cast<intptr_t>(socket));
330 if (Dart_IsError(error)) {
331 delete socket;
332 return error;
333 }
334
335 Dart_NewFinalizableHandle(handle, reinterpret_cast<void*>(socket),
336 sizeof(SynchronousSocket),
338 return error;
339}
340
342 Dart_Handle socket_obj,
343 SynchronousSocket** socket) {
344 ASSERT(socket != nullptr);
345 intptr_t id;
348 if (Dart_IsError(result)) {
349 return result;
350 }
351 *socket = reinterpret_cast<SynchronousSocket*>(id);
352 if (*socket == nullptr) {
354 DartUtils::NewInternalError("No native peer")));
355 }
356 return result;
357}
358
359} // namespace bin
360} // namespace dart
GLenum type
#define FUNCTION_NAME(name)
Definition: builtin.h:19
intptr_t count() const
Definition: socket_base.h:140
T * GetAt(intptr_t i) const
Definition: socket_base.h:141
static int64_t GetInt64ValueCheckRange(Dart_Handle value_obj, int64_t lower, int64_t upper)
Definition: dartutils.cc:90
static Dart_Handle NewDartOSError()
Definition: dartutils.cc:702
static bool GetInt64Value(Dart_Handle value_obj, int64_t *value)
Definition: dartutils.cc:112
static Dart_Handle NewDartArgumentError(const char *message)
Definition: dartutils.cc:746
static Dart_Handle NewInternalError(const char *message)
Definition: dartutils.cc:781
static intptr_t GetIntptrValue(Dart_Handle value_obj)
Definition: dartutils.cc:100
static Dart_Handle Allocate(intptr_t size, uint8_t **buffer)
Definition: io_buffer.cc:12
static void GetSockAddr(Dart_Handle obj, RawAddr *addr)
Definition: socket_base.cc:105
static void SetAddrPort(RawAddr *addr, intptr_t port)
Definition: socket_base.cc:177
static Dart_Handle ToTypedData(const RawAddr &addr)
Definition: socket_base.cc:200
static AddressList< SocketAddress > * LookupAddress(const char *host, int type, OSError **os_error)
static SocketAddress * GetRemotePeer(intptr_t fd, intptr_t *port)
static intptr_t Available(intptr_t fd)
static Dart_Handle GetSocketIdNativeField(Dart_Handle socket_obj, SynchronousSocket **socket)
Definition: sync_socket.cc:341
static intptr_t CreateConnect(const RawAddr &addr)
static void ShutdownRead(intptr_t fd)
static void Close(intptr_t fd)
static intptr_t Read(intptr_t fd, void *buffer, intptr_t num_bytes)
static intptr_t Write(intptr_t fd, const void *buffer, intptr_t num_bytes)
static intptr_t GetPort(intptr_t fd)
static void ShutdownWrite(intptr_t fd)
static Dart_Handle SetSocketIdNativeField(Dart_Handle handle, SynchronousSocket *socket)
Definition: sync_socket.cc:325
struct _Dart_Handle * Dart_Handle
Definition: dart_api.h:258
struct _Dart_NativeArguments * Dart_NativeArguments
Definition: dart_api.h:3019
Dart_TypedData_Type
Definition: dart_api.h:2612
#define ASSERT(E)
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
const uint8_t uint32_t uint32_t GError ** error
GAsyncResult * result
size_t length
void FUNCTION_NAME() SynchronousSocket_Available(Dart_NativeArguments args)
Definition: sync_socket.cc:185
static constexpr int kSocketIdNativeField
Definition: socket.cc:26
void FUNCTION_NAME() SynchronousSocket_GetRemotePeer(Dart_NativeArguments args)
Definition: sync_socket.cc:279
static void SynchronousSocketFinalizer(void *isolate_data, void *data)
Definition: sync_socket.cc:316
void FUNCTION_NAME() SynchronousSocket_WriteList(Dart_NativeArguments args)
Definition: sync_socket.cc:120
void FUNCTION_NAME() SynchronousSocket_GetPort(Dart_NativeArguments args)
Definition: sync_socket.cc:265
void FUNCTION_NAME() SynchronousSocket_ShutdownRead(Dart_NativeArguments args)
Definition: sync_socket.cc:247
void FUNCTION_NAME() SynchronousSocket_Read(Dart_NativeArguments args)
Definition: sync_socket.cc:209
void FUNCTION_NAME() SynchronousSocket_LookupRequest(Dart_NativeArguments args)
Definition: sync_socket.cc:41
void FUNCTION_NAME() SynchronousSocket_ReadList(Dart_NativeArguments args)
Definition: sync_socket.cc:153
void FUNCTION_NAME() SynchronousSocket_CreateConnectSync(Dart_NativeArguments args)
Definition: sync_socket.cc:101
void FUNCTION_NAME() SynchronousSocket_ShutdownWrite(Dart_NativeArguments args)
Definition: sync_socket.cc:256
void FUNCTION_NAME() SynchronousSocket_CloseSync(Dart_NativeArguments args)
Definition: sync_socket.cc:199
Definition: dart_vm.cc:33
DART_EXPORT Dart_Handle Dart_GetNativeStringArgument(Dart_NativeArguments args, int arg_index, void **peer)
DART_EXPORT Dart_Handle Dart_GetNativeInstanceField(Dart_Handle obj, int index, intptr_t *value)
DART_EXPORT void Dart_SetBooleanReturnValue(Dart_NativeArguments args, bool retval)
DART_EXPORT Dart_Handle Dart_NewUnhandledExceptionError(Dart_Handle exception)
DART_EXPORT Dart_Handle Dart_NewInteger(int64_t value)
DART_EXPORT void Dart_PropagateError(Dart_Handle handle)
DART_EXPORT Dart_FinalizableHandle Dart_NewFinalizableHandle(Dart_Handle object, void *peer, intptr_t external_allocation_size, Dart_HandleFinalizer callback)
DART_EXPORT uint8_t * Dart_ScopeAllocate(intptr_t size)
DART_EXPORT void Dart_SetReturnValue(Dart_NativeArguments args, Dart_Handle retval)
DART_EXPORT Dart_Handle Dart_TypedDataAcquireData(Dart_Handle object, Dart_TypedData_Type *type, void **data, intptr_t *len)
DART_EXPORT Dart_Handle Dart_ListSetAt(Dart_Handle list, intptr_t index, Dart_Handle value)
DART_EXPORT bool Dart_IsError(Dart_Handle handle)
DART_EXPORT Dart_Handle Dart_SetNativeInstanceField(Dart_Handle obj, int index, intptr_t value)
DART_EXPORT void Dart_SetIntegerReturnValue(Dart_NativeArguments args, int64_t retval)
DART_EXPORT Dart_Handle Dart_ListSetAsBytes(Dart_Handle list, intptr_t offset, const uint8_t *native_array, intptr_t length)
DART_EXPORT Dart_Handle Dart_GetNativeArgument(Dart_NativeArguments args, int index)
DART_EXPORT Dart_Handle Dart_TypedDataReleaseData(Dart_Handle object)
DART_EXPORT Dart_Handle Dart_ListLength(Dart_Handle list, intptr_t *len)
DART_EXPORT Dart_Handle Dart_NewList(intptr_t length)
DART_EXPORT bool Dart_IsList(Dart_Handle object)
DART_EXPORT Dart_Handle Dart_GetNativeIntegerArgument(Dart_NativeArguments args, int index, int64_t *value)
DART_EXPORT int Dart_GetNativeArgumentCount(Dart_NativeArguments args)
DART_EXPORT bool Dart_IsNull(Dart_Handle object)
static int8_t data[kExtLength]
DART_EXPORT Dart_Handle Dart_StringToCString(Dart_Handle object, const char **cstr)
DART_EXPORT Dart_Handle Dart_NewStringFromCString(const char *str)
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service port
Definition: switches.h:87
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service host
Definition: switches.h:74
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
SeparatedVector2 offset
#define DART_CHECK_ERROR(handle)
Definition: sync_socket.cc:28
#define DART_CHECK_ERROR_AND_CLEANUP(handle, ptr)
Definition: sync_socket.cc:19
const uintptr_t id