Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(342)

Side by Side Diff: runtime/bin/sync_socket.cc

Issue 2812153002: Revert "Added synchronous socket implementation to dart:io." (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/bin/sync_socket.h ('k') | runtime/bin/sync_socket_android.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 #if !defined(DART_IO_DISABLED)
6
7 #include "bin/sync_socket.h"
8
9 #include "bin/dartutils.h"
10 #include "bin/io_buffer.h"
11 #include "bin/isolate_data.h"
12 #include "bin/lockers.h"
13 #include "bin/thread.h"
14 #include "bin/utils.h"
15
16 #include "include/dart_api.h"
17
18 #include "platform/globals.h"
19 #include "platform/utils.h"
20
21 #define DART_CHECK_ERROR_AND_CLEANUP(handle, ptr) \
22 do { \
23 if (Dart_IsError((handle))) { \
24 delete (ptr); \
25 Dart_SetReturnValue(args, (handle)); \
26 return; \
27 } \
28 } while (0)
29
30 #define DART_CHECK_ERROR(handle) \
31 do { \
32 if (Dart_IsError((handle))) { \
33 Dart_SetReturnValue(args, (handle)); \
34 return; \
35 } \
36 } while (0)
37
38 namespace dart {
39 namespace bin {
40
41 static const int kSocketIdNativeField = 0;
42
43 void FUNCTION_NAME(SynchronousSocket_LookupRequest)(Dart_NativeArguments args) {
44 if (Dart_GetNativeArgumentCount(args) != 2) {
45 Dart_SetReturnValue(
46 args, DartUtils::NewDartArgumentError("Invalid argument count."));
47 return;
48 }
49
50 char* peer = NULL;
51 Dart_Handle host_arg =
52 Dart_GetNativeStringArgument(args, 0, reinterpret_cast<void**>(&peer));
53 DART_CHECK_ERROR(host_arg);
54
55 char* host = NULL;
56 host_arg = Dart_StringToCString(host_arg, const_cast<const char**>(&host));
57 DART_CHECK_ERROR(host_arg);
58
59 int64_t type = 0;
60 Dart_Handle port_error = Dart_GetNativeIntegerArgument(args, 1, &type);
61 DART_CHECK_ERROR(port_error);
62
63 OSError* os_error = NULL;
64 AddressList<SocketAddress>* addresses =
65 SocketBase::LookupAddress(host, type, &os_error);
66 if (addresses == NULL) {
67 Dart_SetReturnValue(args, DartUtils::NewDartOSError(os_error));
68 return;
69 }
70
71 Dart_Handle array = Dart_NewList(addresses->count());
72 DART_CHECK_ERROR_AND_CLEANUP(array, addresses);
73
74 for (intptr_t i = 0; i < addresses->count(); i++) {
75 SocketAddress* addr = addresses->GetAt(i);
76 Dart_Handle entry = Dart_NewList(3);
77 DART_CHECK_ERROR_AND_CLEANUP(entry, addresses);
78
79 Dart_Handle type = Dart_NewInteger(addr->GetType());
80 DART_CHECK_ERROR_AND_CLEANUP(type, addresses);
81 Dart_Handle error = Dart_ListSetAt(entry, 0, type);
82 DART_CHECK_ERROR_AND_CLEANUP(error, addresses);
83
84 Dart_Handle as_string = Dart_NewStringFromCString(addr->as_string());
85 DART_CHECK_ERROR_AND_CLEANUP(as_string, addresses);
86 error = Dart_ListSetAt(entry, 1, as_string);
87 DART_CHECK_ERROR_AND_CLEANUP(error, addresses);
88
89 RawAddr raw = addr->addr();
90 Dart_Handle data = SocketAddress::ToTypedData(raw);
91 DART_CHECK_ERROR_AND_CLEANUP(data, addresses);
92
93 error = Dart_ListSetAt(entry, 2, data);
94 DART_CHECK_ERROR_AND_CLEANUP(error, addresses);
95 error = Dart_ListSetAt(array, i, entry);
96 DART_CHECK_ERROR_AND_CLEANUP(error, addresses);
97 }
98 delete addresses;
99 Dart_SetReturnValue(args, array);
100 return;
101 }
102
103
104 void FUNCTION_NAME(SynchronousSocket_CreateConnectSync)(
105 Dart_NativeArguments args) {
106 RawAddr addr;
107 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr);
108 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2);
109 DART_CHECK_ERROR(port_arg);
110 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535);
111 SocketAddress::SetAddrPort(&addr, static_cast<intptr_t>(port));
112 intptr_t socket = SynchronousSocket::CreateConnect(addr);
113 if (socket >= 0) {
114 Dart_Handle error = SynchronousSocket::SetSocketIdNativeField(
115 Dart_GetNativeArgument(args, 0), socket);
116 DART_CHECK_ERROR(error);
117 Dart_SetBooleanReturnValue(args, true);
118 } else {
119 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
120 }
121 }
122
123
124 void FUNCTION_NAME(SynchronousSocket_WriteList)(Dart_NativeArguments args) {
125 SynchronousSocket* socket = NULL;
126 Dart_Handle result = SynchronousSocket::GetSocketIdNativeField(
127 Dart_GetNativeArgument(args, 0), &socket);
128 DART_CHECK_ERROR(result);
129
130 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1);
131 if (!Dart_IsList(buffer_obj)) {
132 Dart_SetReturnValue(args, DartUtils::NewDartArgumentError(
133 "First parameter must be a List<int>"));
134 return;
135 }
136 intptr_t offset = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2));
137 intptr_t length = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3));
138 Dart_TypedData_Type type;
139 uint8_t* buffer = NULL;
140 intptr_t len;
141 result = Dart_TypedDataAcquireData(buffer_obj, &type,
142 reinterpret_cast<void**>(&buffer), &len);
143 DART_CHECK_ERROR(result);
144 ASSERT((offset + length) <= len);
145 buffer += offset;
146 intptr_t bytes_written = SocketBase::Write(socket->fd(), buffer, length);
147 if (bytes_written >= 0) {
148 Dart_TypedDataReleaseData(buffer_obj);
149 Dart_SetIntegerReturnValue(args, bytes_written);
150 } else {
151 // Extract OSError before we release data, as it may override the error.
152 OSError os_error;
153 Dart_TypedDataReleaseData(buffer_obj);
154 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error));
155 }
156 }
157
158
159 void FUNCTION_NAME(SynchronousSocket_ReadList)(Dart_NativeArguments args) {
160 SynchronousSocket* socket = NULL;
161 Dart_Handle result = SynchronousSocket::GetSocketIdNativeField(
162 Dart_GetNativeArgument(args, 0), &socket);
163 DART_CHECK_ERROR(result);
164
165 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1);
166 if (!Dart_IsList(buffer_obj)) {
167 Dart_SetReturnValue(args, DartUtils::NewDartArgumentError(
168 "First parameter must be a List<int>"));
169 return;
170 }
171 intptr_t offset = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2));
172 intptr_t bytes = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3));
173 intptr_t array_len = 0;
174
175 result = Dart_ListLength(buffer_obj, &array_len);
176 DART_CHECK_ERROR(result);
177
178 uint8_t* buffer = Dart_ScopeAllocate(bytes);
179 intptr_t bytes_read = SocketBase::Read(socket->fd(), buffer, bytes);
180 if (bytes_read >= 0) {
181 result = Dart_ListSetAsBytes(buffer_obj, offset, buffer, bytes_read);
182 DART_CHECK_ERROR(result);
183 Dart_SetIntegerReturnValue(args, bytes_read);
184 } else {
185 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
186 }
187 }
188
189
190 void FUNCTION_NAME(SynchronousSocket_Available)(Dart_NativeArguments args) {
191 SynchronousSocket* socket = NULL;
192 Dart_Handle result = SynchronousSocket::GetSocketIdNativeField(
193 Dart_GetNativeArgument(args, 0), &socket);
194 DART_CHECK_ERROR(result);
195
196 intptr_t available = SocketBase::Available(socket->fd());
197 if (available >= 0) {
198 Dart_SetIntegerReturnValue(args, available);
199 } else {
200 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
201 }
202 }
203
204
205 void FUNCTION_NAME(SynchronousSocket_CloseSync)(Dart_NativeArguments args) {
206 SynchronousSocket* socket = NULL;
207 Dart_Handle result = SynchronousSocket::GetSocketIdNativeField(
208 Dart_GetNativeArgument(args, 0), &socket);
209 DART_CHECK_ERROR(result);
210
211 SocketBase::Close(socket->fd());
212 socket->SetClosedFd();
213 }
214
215
216 void FUNCTION_NAME(SynchronousSocket_Read)(Dart_NativeArguments args) {
217 SynchronousSocket* socket = NULL;
218 Dart_Handle result = SynchronousSocket::GetSocketIdNativeField(
219 Dart_GetNativeArgument(args, 0), &socket);
220 DART_CHECK_ERROR(result);
221
222 int64_t length = 0;
223 if (!DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &length)) {
224 Dart_SetReturnValue(args, DartUtils::NewDartArgumentError(
225 "First parameter must be an integer."));
226 return;
227 }
228 uint8_t* buffer = NULL;
229 result = IOBuffer::Allocate(length, &buffer);
230 ASSERT(buffer != NULL);
231 intptr_t bytes_read = SocketBase::Read(socket->fd(), buffer, length);
232 if (bytes_read == length) {
233 Dart_SetReturnValue(args, result);
234 } else if (bytes_read > 0) {
235 uint8_t* new_buffer = NULL;
236 Dart_Handle new_result = IOBuffer::Allocate(bytes_read, &new_buffer);
237 ASSERT(new_buffer != NULL);
238 memmove(new_buffer, buffer, bytes_read);
239 Dart_SetReturnValue(args, new_result);
240 } else if (bytes_read == -1) {
241 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
242 }
243 }
244
245
246 void FUNCTION_NAME(SynchronousSocket_ShutdownRead)(Dart_NativeArguments args) {
247 SynchronousSocket* socket = NULL;
248 Dart_Handle result = SynchronousSocket::GetSocketIdNativeField(
249 Dart_GetNativeArgument(args, 0), &socket);
250 DART_CHECK_ERROR(result);
251
252 SynchronousSocket::ShutdownRead(socket->fd());
253 }
254
255
256 void FUNCTION_NAME(SynchronousSocket_ShutdownWrite)(Dart_NativeArguments args) {
257 SynchronousSocket* socket = NULL;
258 Dart_Handle result = SynchronousSocket::GetSocketIdNativeField(
259 Dart_GetNativeArgument(args, 0), &socket);
260 DART_CHECK_ERROR(result);
261
262 SynchronousSocket::ShutdownWrite(socket->fd());
263 }
264
265
266 void FUNCTION_NAME(SynchronousSocket_GetPort)(Dart_NativeArguments args) {
267 SynchronousSocket* socket = NULL;
268 Dart_Handle result = SynchronousSocket::GetSocketIdNativeField(
269 Dart_GetNativeArgument(args, 0), &socket);
270 DART_CHECK_ERROR(result);
271
272 intptr_t port = SocketBase::GetPort(socket->fd());
273 if (port > 0) {
274 Dart_SetReturnValue(args, Dart_NewInteger(port));
275 } else {
276 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
277 }
278 }
279
280
281 void FUNCTION_NAME(SynchronousSocket_GetRemotePeer)(Dart_NativeArguments args) {
282 SynchronousSocket* socket = NULL;
283 Dart_Handle result = SynchronousSocket::GetSocketIdNativeField(
284 Dart_GetNativeArgument(args, 0), &socket);
285 DART_CHECK_ERROR(result);
286
287 intptr_t port = 0;
288 SocketAddress* addr = SocketBase::GetRemotePeer(socket->fd(), &port);
289 if (addr == NULL) {
290 Dart_SetReturnValue(args, DartUtils::NewDartOSError());
291 return;
292 }
293 Dart_Handle list = Dart_NewList(2);
294 DART_CHECK_ERROR_AND_CLEANUP(list, addr);
295
296 Dart_Handle entry = Dart_NewList(3);
297 DART_CHECK_ERROR_AND_CLEANUP(entry, addr);
298
299 Dart_Handle error =
300 Dart_ListSetAt(entry, 0, Dart_NewInteger(addr->GetType()));
301 DART_CHECK_ERROR_AND_CLEANUP(error, addr);
302 error =
303 Dart_ListSetAt(entry, 1, Dart_NewStringFromCString(addr->as_string()));
304 DART_CHECK_ERROR_AND_CLEANUP(error, addr);
305
306 RawAddr raw = addr->addr();
307 error = Dart_ListSetAt(entry, 2, SocketAddress::ToTypedData(raw));
308 DART_CHECK_ERROR_AND_CLEANUP(error, addr);
309
310 error = Dart_ListSetAt(list, 0, entry);
311 DART_CHECK_ERROR_AND_CLEANUP(error, addr);
312 error = Dart_ListSetAt(list, 1, Dart_NewInteger(port));
313 DART_CHECK_ERROR_AND_CLEANUP(error, addr);
314 Dart_SetReturnValue(args, list);
315 delete addr;
316 }
317
318
319 static void SynchronousSocketFinalizer(void* isolate_data,
320 Dart_WeakPersistentHandle handle,
321 void* data) {
322 SynchronousSocket* socket = reinterpret_cast<SynchronousSocket*>(data);
323 if (socket->fd() >= 0) {
324 SocketBase::Close(socket->fd());
325 socket->SetClosedFd();
326 }
327 delete socket;
328 }
329
330
331 Dart_Handle SynchronousSocket::SetSocketIdNativeField(Dart_Handle handle,
332 intptr_t id) {
333 SynchronousSocket* socket = new SynchronousSocket(id);
334 Dart_Handle error = Dart_SetNativeInstanceField(
335 handle, kSocketIdNativeField, reinterpret_cast<intptr_t>(socket));
336 if (Dart_IsError(error)) {
337 delete socket;
338 return error;
339 }
340
341 Dart_NewWeakPersistentHandle(handle, reinterpret_cast<void*>(socket),
342 sizeof(SynchronousSocket),
343 SynchronousSocketFinalizer);
344 return error;
345 }
346
347
348 Dart_Handle SynchronousSocket::GetSocketIdNativeField(
349 Dart_Handle socket_obj,
350 SynchronousSocket** socket) {
351 ASSERT(socket != NULL);
352 intptr_t id;
353 Dart_Handle result =
354 Dart_GetNativeInstanceField(socket_obj, kSocketIdNativeField, &id);
355 if (Dart_IsError(result)) {
356 return result;
357 }
358 *socket = reinterpret_cast<SynchronousSocket*>(id);
359 return result;
360 }
361
362 } // namespace bin
363 } // namespace dart
364
365 #endif // !defined(DART_IO_DISABLED)
OLDNEW
« no previous file with comments | « runtime/bin/sync_socket.h ('k') | runtime/bin/sync_socket_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698