Chromium Code Reviews| OLD | NEW |
|---|---|
| (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* host = NULL; | |
|
zra
2017/04/11 16:19:40
nit: Maybe declare this closer to where it's used.
bkonyi
2017/04/11 18:11:17
Done.
| |
| 51 char* peer = NULL; | |
| 52 int64_t type = 0; | |
|
zra
2017/04/11 16:19:41
ditto
bkonyi
2017/04/11 18:11:18
Done.
| |
| 53 | |
| 54 Dart_Handle host_arg = | |
| 55 Dart_GetNativeStringArgument(args, 0, reinterpret_cast<void**>(&peer)); | |
| 56 if (Dart_IsError(host_arg)) { | |
|
zra
2017/04/11 16:19:40
You can use your macro here.
bkonyi
2017/04/11 18:11:18
Done.
| |
| 57 Dart_SetReturnValue(args, host_arg); | |
| 58 return; | |
| 59 } | |
| 60 | |
| 61 host_arg = Dart_StringToCString(host_arg, const_cast<const char**>(&host)); | |
| 62 if (Dart_IsError(host_arg)) { | |
|
zra
2017/04/11 16:19:40
ditto
bkonyi
2017/04/11 18:11:18
Done.
| |
| 63 Dart_SetReturnValue(args, host_arg); | |
| 64 return; | |
| 65 } | |
| 66 | |
| 67 Dart_Handle port_error = Dart_GetNativeIntegerArgument(args, 1, &type); | |
| 68 if (Dart_IsError(port_error)) { | |
|
zra
2017/04/11 16:19:40
ditto
bkonyi
2017/04/11 18:11:18
Done.
| |
| 69 Dart_SetReturnValue(args, port_error); | |
| 70 return; | |
| 71 } | |
| 72 | |
| 73 OSError* os_error = NULL; | |
| 74 AddressList<SocketAddress>* addresses = | |
| 75 SocketBase::LookupAddress(host, type, &os_error); | |
| 76 | |
|
zra
2017/04/11 16:19:40
nit: This whitespace is allowed by the style guide
bkonyi
2017/04/11 18:11:18
Makes sense. I've removed the whitespace.
| |
| 77 if (addresses == NULL) { | |
| 78 Dart_SetReturnValue(args, DartUtils::NewDartOSError(os_error)); | |
| 79 return; | |
| 80 } | |
| 81 | |
| 82 Dart_Handle array = Dart_NewList(addresses->count()); | |
| 83 Dart_Handle error; | |
|
zra
2017/04/11 16:19:40
nit: It's a little confusing to declare this betwe
bkonyi
2017/04/11 18:11:18
I agree. I've moved the declaration to the first t
| |
| 84 DART_CHECK_ERROR_AND_CLEANUP(array, addresses); | |
| 85 | |
| 86 for (intptr_t i = 0; i < addresses->count(); i++) { | |
| 87 SocketAddress* addr = addresses->GetAt(i); | |
| 88 Dart_Handle entry = Dart_NewList(3); | |
| 89 DART_CHECK_ERROR_AND_CLEANUP(entry, addresses); | |
| 90 | |
| 91 Dart_Handle type = Dart_NewInteger(addr->GetType()); | |
| 92 DART_CHECK_ERROR_AND_CLEANUP(type, addresses); | |
| 93 error = Dart_ListSetAt(entry, 0, type); | |
| 94 DART_CHECK_ERROR_AND_CLEANUP(error, addresses); | |
| 95 | |
| 96 Dart_Handle as_string = Dart_NewStringFromCString(addr->as_string()); | |
| 97 DART_CHECK_ERROR_AND_CLEANUP(as_string, addresses); | |
| 98 error = Dart_ListSetAt(entry, 1, as_string); | |
| 99 DART_CHECK_ERROR_AND_CLEANUP(error, addresses); | |
| 100 | |
| 101 RawAddr raw = addr->addr(); | |
| 102 Dart_Handle data = SocketAddress::ToTypedData(raw); | |
| 103 DART_CHECK_ERROR_AND_CLEANUP(data, addresses); | |
| 104 | |
| 105 error = Dart_ListSetAt(entry, 2, data); | |
| 106 DART_CHECK_ERROR_AND_CLEANUP(error, addresses); | |
| 107 error = Dart_ListSetAt(array, i, entry); | |
| 108 DART_CHECK_ERROR_AND_CLEANUP(error, addresses); | |
| 109 } | |
| 110 delete addresses; | |
| 111 Dart_SetReturnValue(args, array); | |
| 112 return; | |
| 113 } | |
| 114 | |
| 115 | |
| 116 void FUNCTION_NAME(SynchronousSocket_CreateConnectSync)( | |
| 117 Dart_NativeArguments args) { | |
| 118 RawAddr addr; | |
| 119 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | |
| 120 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); | |
| 121 DART_CHECK_ERROR(port_arg); | |
| 122 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); | |
| 123 SocketAddress::SetAddrPort(&addr, static_cast<intptr_t>(port)); | |
| 124 intptr_t socket = SynchronousSocket::CreateConnect(addr); | |
| 125 if (socket >= 0) { | |
| 126 SynchronousSocket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), | |
| 127 socket); | |
| 128 Dart_SetBooleanReturnValue(args, true); | |
| 129 } else { | |
| 130 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 | |
| 135 void FUNCTION_NAME(SynchronousSocket_WriteList)(Dart_NativeArguments args) { | |
| 136 SynchronousSocket* socket = SynchronousSocket::GetSocketIdNativeField( | |
| 137 Dart_GetNativeArgument(args, 0)); | |
| 138 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | |
| 139 if (!Dart_IsList(buffer_obj)) { | |
| 140 Dart_SetReturnValue(args, DartUtils::NewDartArgumentError( | |
| 141 "First parameter must be a List<int>")); | |
| 142 return; | |
| 143 } | |
| 144 intptr_t offset = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); | |
| 145 intptr_t length = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); | |
| 146 Dart_TypedData_Type type; | |
| 147 uint8_t* buffer = NULL; | |
| 148 intptr_t len; | |
| 149 Dart_Handle result = Dart_TypedDataAcquireData( | |
| 150 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); | |
| 151 DART_CHECK_ERROR(result); | |
| 152 ASSERT((offset + length) <= len); | |
| 153 buffer += offset; | |
| 154 intptr_t bytes_written = SocketBase::Write(socket->fd(), buffer, length); | |
| 155 if (bytes_written >= 0) { | |
| 156 Dart_TypedDataReleaseData(buffer_obj); | |
| 157 Dart_SetIntegerReturnValue(args, bytes_written); | |
| 158 } else { | |
| 159 // Extract OSError before we release data, as it may override the error. | |
| 160 OSError os_error; | |
| 161 Dart_TypedDataReleaseData(buffer_obj); | |
| 162 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 | |
| 167 void FUNCTION_NAME(SynchronousSocket_ReadList)(Dart_NativeArguments args) { | |
| 168 SynchronousSocket* socket = SynchronousSocket::GetSocketIdNativeField( | |
| 169 Dart_GetNativeArgument(args, 0)); | |
| 170 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | |
| 171 if (!Dart_IsList(buffer_obj)) { | |
| 172 Dart_SetReturnValue(args, DartUtils::NewDartArgumentError( | |
| 173 "First parameter must be a List<int>")); | |
| 174 return; | |
| 175 } | |
| 176 intptr_t offset = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); | |
| 177 intptr_t bytes = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); | |
| 178 intptr_t array_len = 0; | |
| 179 | |
| 180 Dart_Handle result = Dart_ListLength(buffer_obj, &array_len); | |
| 181 DART_CHECK_ERROR(result); | |
| 182 | |
| 183 uint8_t* buffer = Dart_ScopeAllocate(bytes); | |
| 184 intptr_t bytes_read = SocketBase::Read(socket->fd(), buffer, bytes); | |
| 185 if (bytes_read >= 0) { | |
| 186 result = Dart_ListSetAsBytes(buffer_obj, offset, buffer, bytes_read); | |
| 187 DART_CHECK_ERROR(result); | |
| 188 Dart_SetIntegerReturnValue(args, bytes_read); | |
| 189 } else { | |
| 190 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | |
| 191 } | |
| 192 } | |
| 193 | |
| 194 | |
| 195 void FUNCTION_NAME(SynchronousSocket_Available)(Dart_NativeArguments args) { | |
| 196 SynchronousSocket* socket = SynchronousSocket::GetSocketIdNativeField( | |
| 197 Dart_GetNativeArgument(args, 0)); | |
| 198 intptr_t available = SocketBase::Available(socket->fd()); | |
| 199 if (available >= 0) { | |
| 200 Dart_SetIntegerReturnValue(args, available); | |
| 201 } else { | |
| 202 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 | |
| 207 void FUNCTION_NAME(SynchronousSocket_CloseSync)(Dart_NativeArguments args) { | |
| 208 SynchronousSocket* socket = SynchronousSocket::GetSocketIdNativeField( | |
| 209 Dart_GetNativeArgument(args, 0)); | |
| 210 SocketBase::Close(socket->fd()); | |
| 211 socket->SetClosedFd(); | |
| 212 } | |
| 213 | |
| 214 | |
| 215 void FUNCTION_NAME(SynchronousSocket_Read)(Dart_NativeArguments args) { | |
| 216 SynchronousSocket* socket = SynchronousSocket::GetSocketIdNativeField( | |
| 217 Dart_GetNativeArgument(args, 0)); | |
| 218 int64_t length = 0; | |
| 219 if (!DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &length)) { | |
| 220 Dart_SetReturnValue(args, DartUtils::NewDartArgumentError( | |
| 221 "First parameter must be an integer.")); | |
| 222 return; | |
| 223 } | |
| 224 uint8_t* buffer = NULL; | |
| 225 Dart_Handle result = IOBuffer::Allocate(length, &buffer); | |
| 226 if (Dart_IsError(result)) { | |
|
zra
2017/04/11 16:19:40
IOBuffer::Allocate can't return an error handle. (
bkonyi
2017/04/11 18:11:17
Acknowledged.
| |
| 227 if (buffer != NULL) { | |
| 228 IOBuffer::Free(buffer); | |
| 229 } | |
| 230 Dart_SetReturnValue(args, result); | |
| 231 return; | |
| 232 } | |
| 233 ASSERT(buffer != NULL); | |
| 234 intptr_t bytes_read = SocketBase::Read(socket->fd(), buffer, length); | |
| 235 if (bytes_read == length) { | |
| 236 Dart_SetReturnValue(args, result); | |
| 237 } else if (bytes_read > 0) { | |
| 238 uint8_t* new_buffer = NULL; | |
| 239 Dart_Handle new_result = IOBuffer::Allocate(bytes_read, &new_buffer); | |
| 240 if (Dart_IsError(new_result)) { | |
|
zra
2017/04/11 16:19:41
IOBuffer::Allocate can't return an error handle.
bkonyi
2017/04/11 18:11:18
Acknowledged.
| |
| 241 IOBuffer::Free(buffer); | |
| 242 if (new_buffer != NULL) { | |
| 243 IOBuffer::Free(new_buffer); | |
| 244 } | |
| 245 return; | |
|
zra
2017/04/11 16:19:40
Return the error that's in new_result
bkonyi
2017/04/11 18:11:18
Done.
| |
| 246 } | |
| 247 ASSERT(new_buffer != NULL); | |
| 248 memmove(new_buffer, buffer, bytes_read); | |
| 249 IOBuffer::Free(buffer); | |
|
zra
2017/04/11 16:19:40
IOBuffer attaches 'buffer' to a WeakPersistentHand
bkonyi
2017/04/11 18:11:18
Acknowledged.
| |
| 250 Dart_SetReturnValue(args, new_result); | |
| 251 } else if (bytes_read == -1) { | |
| 252 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | |
| 253 } | |
| 254 } | |
| 255 | |
| 256 | |
| 257 void FUNCTION_NAME(SynchronousSocket_ShutdownRead)(Dart_NativeArguments args) { | |
| 258 SynchronousSocket* socket = SynchronousSocket::GetSocketIdNativeField( | |
| 259 Dart_GetNativeArgument(args, 0)); | |
| 260 SynchronousSocket::ShutdownRead(socket->fd()); | |
| 261 } | |
| 262 | |
| 263 | |
| 264 void FUNCTION_NAME(SynchronousSocket_ShutdownWrite)(Dart_NativeArguments args) { | |
| 265 SynchronousSocket* socket = SynchronousSocket::GetSocketIdNativeField( | |
| 266 Dart_GetNativeArgument(args, 0)); | |
| 267 SynchronousSocket::ShutdownWrite(socket->fd()); | |
| 268 } | |
| 269 | |
| 270 | |
| 271 void FUNCTION_NAME(SynchronousSocket_GetPort)(Dart_NativeArguments args) { | |
| 272 SynchronousSocket* socket = SynchronousSocket::GetSocketIdNativeField( | |
| 273 Dart_GetNativeArgument(args, 0)); | |
| 274 intptr_t port = SocketBase::GetPort(socket->fd()); | |
| 275 if (port > 0) { | |
| 276 Dart_SetReturnValue(args, Dart_NewInteger(port)); | |
| 277 } else { | |
| 278 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | |
| 279 } | |
| 280 } | |
| 281 | |
| 282 | |
| 283 void FUNCTION_NAME(SynchronousSocket_GetRemotePeer)(Dart_NativeArguments args) { | |
| 284 SynchronousSocket* socket = SynchronousSocket::GetSocketIdNativeField( | |
| 285 Dart_GetNativeArgument(args, 0)); | |
| 286 intptr_t port = 0; | |
| 287 SocketAddress* addr = SocketBase::GetRemotePeer(socket->fd(), &port); | |
| 288 if (addr == NULL) { | |
| 289 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | |
| 290 return; | |
| 291 } | |
| 292 Dart_Handle list = Dart_NewList(2); | |
| 293 DART_CHECK_ERROR_AND_CLEANUP(list, addr); | |
| 294 | |
| 295 Dart_Handle entry = Dart_NewList(3); | |
| 296 DART_CHECK_ERROR_AND_CLEANUP(entry, addr); | |
| 297 | |
| 298 Dart_Handle error = | |
| 299 Dart_ListSetAt(entry, 0, Dart_NewInteger(addr->GetType())); | |
| 300 DART_CHECK_ERROR_AND_CLEANUP(error, addr); | |
| 301 error = | |
| 302 Dart_ListSetAt(entry, 1, Dart_NewStringFromCString(addr->as_string())); | |
| 303 DART_CHECK_ERROR_AND_CLEANUP(error, addr); | |
| 304 | |
| 305 RawAddr raw = addr->addr(); | |
| 306 error = Dart_ListSetAt(entry, 2, SocketAddress::ToTypedData(raw)); | |
| 307 DART_CHECK_ERROR_AND_CLEANUP(error, addr); | |
| 308 | |
| 309 error = Dart_ListSetAt(list, 0, entry); | |
| 310 DART_CHECK_ERROR_AND_CLEANUP(error, addr); | |
| 311 error = Dart_ListSetAt(list, 1, Dart_NewInteger(port)); | |
| 312 DART_CHECK_ERROR_AND_CLEANUP(error, addr); | |
| 313 Dart_SetReturnValue(args, list); | |
| 314 delete addr; | |
| 315 } | |
| 316 | |
| 317 #undef DART_CHECK_ERROR_AND_CLEANUP | |
|
zra
2017/04/11 16:19:40
This is probably unnecessary.
bkonyi
2017/04/11 18:11:17
Yes, this was left over from some rough stuff I wa
| |
| 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 void SynchronousSocket::SetSocketIdNativeField(Dart_Handle handle, | |
| 332 intptr_t id) { | |
| 333 SynchronousSocket* socket = new SynchronousSocket(id); | |
| 334 Dart_SetNativeInstanceField(handle, kSocketIdNativeField, | |
| 335 reinterpret_cast<intptr_t>(socket)); | |
| 336 Dart_NewWeakPersistentHandle(handle, reinterpret_cast<void*>(socket), | |
| 337 sizeof(SynchronousSocket), | |
| 338 SynchronousSocketFinalizer); | |
| 339 } | |
| 340 | |
| 341 | |
| 342 SynchronousSocket* SynchronousSocket::GetSocketIdNativeField( | |
| 343 Dart_Handle socket_obj) { | |
| 344 intptr_t id; | |
| 345 Dart_GetNativeInstanceField(socket_obj, kSocketIdNativeField, &id); | |
| 346 SynchronousSocket* socket = reinterpret_cast<SynchronousSocket*>(id); | |
| 347 return socket; | |
| 348 } | |
| 349 | |
| 350 } // namespace bin | |
| 351 } // namespace dart | |
| 352 | |
| 353 #endif // !defined(DART_IO_DISABLED) | |
| OLD | NEW |