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 namespace dart { | |
| 22 namespace bin { | |
| 23 | |
| 24 static const int kSocketIdNativeField = 0; | |
| 25 | |
| 26 | |
| 27 void FUNCTION_NAME(SynchronousSocket_LookupRequest)(Dart_NativeArguments args) { | |
| 28 if (Dart_GetNativeArgumentCount(args) == 2) { | |
|
zra
2017/04/07 05:29:27
The logic can be rearranged a bit to avoid a lot o
bkonyi
2017/04/10 19:20:05
Done.
| |
| 29 const char* host = | |
| 30 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); | |
|
zra
2017/04/07 05:29:27
See Dart_GetNativeStringArgument instead
bkonyi
2017/04/10 19:20:04
Done.
| |
| 31 int type = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); | |
|
zra
2017/04/07 05:29:27
See Dart_GetNativeIntegerArgument instead
bkonyi
2017/04/10 19:20:04
Done.
| |
| 32 | |
| 33 Dart_Handle result = NULL; | |
| 34 OSError* os_error = NULL; | |
| 35 AddressList<SocketAddress>* addresses = | |
| 36 SocketBase::LookupAddress(host, type, &os_error); | |
| 37 if (addresses != NULL) { | |
| 38 Dart_Handle array = Dart_NewList(addresses->count() + 1); | |
|
zra
2017/04/07 05:29:27
If Dart API calls can return error handles, then w
bkonyi
2017/04/10 19:20:04
Done.
| |
| 39 Dart_ListSetAt(array, 0, Dart_NewInteger(0)); | |
|
zra
2017/04/07 05:29:27
Why is this here?
bkonyi
2017/04/10 19:20:04
There's a TODO in sync_socket_patch which referenc
| |
| 40 for (intptr_t i = 0; i < addresses->count(); i++) { | |
| 41 SocketAddress* addr = addresses->GetAt(i); | |
| 42 Dart_Handle entry = Dart_NewList(3); | |
| 43 | |
| 44 Dart_Handle type = Dart_NewInteger(addr->GetType()); | |
| 45 Dart_ListSetAt(entry, 0, type); | |
| 46 | |
| 47 Dart_Handle as_string = Dart_NewStringFromCString(addr->as_string()); | |
| 48 Dart_ListSetAt(entry, 1, as_string); | |
| 49 | |
| 50 RawAddr raw = addr->addr(); | |
| 51 Dart_Handle data = SocketAddress::ToTypedData(raw); | |
| 52 Dart_ListSetAt(entry, 2, data); | |
| 53 | |
| 54 Dart_ListSetAt(array, i + 1, entry); | |
| 55 } | |
| 56 result = array; | |
| 57 delete addresses; | |
| 58 } else { | |
| 59 result = DartUtils::NewDartOSError(os_error); | |
| 60 delete os_error; | |
| 61 } | |
| 62 Dart_SetReturnValue(args, result); | |
| 63 return; | |
| 64 } | |
| 65 Dart_SetReturnValue(args, DartUtils::NewDartArgumentError("")); | |
|
zra
2017/04/07 05:29:27
Can you give an error message here?
bkonyi
2017/04/10 19:20:04
Done.
| |
| 66 } | |
| 67 | |
| 68 | |
| 69 void FUNCTION_NAME(SynchronousSocket_CreateConnectSync)( | |
| 70 Dart_NativeArguments args) { | |
| 71 RawAddr addr; | |
| 72 SocketAddress::GetSockAddr(Dart_GetNativeArgument(args, 1), &addr); | |
| 73 Dart_Handle port_arg = Dart_GetNativeArgument(args, 2); | |
| 74 int64_t port = DartUtils::GetInt64ValueCheckRange(port_arg, 0, 65535); | |
| 75 SocketAddress::SetAddrPort(&addr, static_cast<intptr_t>(port)); | |
| 76 | |
| 77 intptr_t socket = SynchronousSocket::CreateConnect(addr); | |
| 78 OSError error; | |
|
zra
2017/04/07 05:29:27
This declaration should go inside the else case. t
bkonyi
2017/04/10 19:20:05
Done.
| |
| 79 if (socket >= 0) { | |
| 80 SynchronousSocket::SetSocketIdNativeField(Dart_GetNativeArgument(args, 0), | |
| 81 socket); | |
| 82 Dart_SetReturnValue(args, Dart_True()); | |
| 83 } else { | |
| 84 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 | |
| 89 void FUNCTION_NAME(SynchronousSocket_WriteList)(Dart_NativeArguments args) { | |
| 90 SynchronousSocket* socket = SynchronousSocket::GetSocketIdNativeField( | |
| 91 Dart_GetNativeArgument(args, 0)); | |
| 92 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | |
| 93 ASSERT(Dart_IsList(buffer_obj)); | |
|
zra
2017/04/07 05:29:27
This is probably an assert because the argument co
bkonyi
2017/04/10 19:20:05
Done.
| |
| 94 intptr_t offset = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 2)); | |
| 95 intptr_t length = DartUtils::GetIntptrValue(Dart_GetNativeArgument(args, 3)); | |
| 96 Dart_TypedData_Type type; | |
| 97 uint8_t* buffer = NULL; | |
| 98 intptr_t len; | |
| 99 Dart_Handle result = Dart_TypedDataAcquireData( | |
| 100 buffer_obj, &type, reinterpret_cast<void**>(&buffer), &len); | |
| 101 if (Dart_IsError(result)) { | |
| 102 Dart_PropagateError(result); | |
| 103 } | |
| 104 ASSERT((offset + length) <= len); | |
| 105 buffer += offset; | |
| 106 intptr_t bytes_written = SocketBase::Write(socket->fd(), buffer, length); | |
| 107 if (bytes_written >= 0) { | |
| 108 Dart_TypedDataReleaseData(buffer_obj); | |
| 109 Dart_SetReturnValue(args, Dart_NewInteger(bytes_written)); | |
| 110 } else { | |
| 111 // Extract OSError before we release data, as it may override the error. | |
| 112 OSError os_error; | |
| 113 Dart_TypedDataReleaseData(buffer_obj); | |
| 114 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 | |
| 119 void FUNCTION_NAME(SynchronousSocket_Available)(Dart_NativeArguments args) { | |
| 120 SynchronousSocket* socket = SynchronousSocket::GetSocketIdNativeField( | |
| 121 Dart_GetNativeArgument(args, 0)); | |
| 122 intptr_t available = SocketBase::Available(socket->fd()); | |
| 123 if (available >= 0) { | |
| 124 Dart_SetReturnValue(args, Dart_NewInteger(available)); | |
| 125 } else { | |
| 126 // Available failed. Mark socket as having data, to trigger a future read | |
|
zra
2017/04/07 05:29:27
This doesn't make any sense for a synchronous sock
bkonyi
2017/04/10 19:20:04
Done.
| |
| 127 // event where the actual error can be reported. | |
| 128 Dart_SetReturnValue(args, Dart_NewInteger(1)); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 | |
| 133 void FUNCTION_NAME(SynchronousSocket_CloseSync)(Dart_NativeArguments args) { | |
| 134 SynchronousSocket* socket = SynchronousSocket::GetSocketIdNativeField( | |
| 135 Dart_GetNativeArgument(args, 0)); | |
| 136 SocketBase::Close(socket->fd()); | |
| 137 socket->SetClosedFd(); | |
| 138 } | |
| 139 | |
| 140 | |
| 141 void FUNCTION_NAME(SynchronousSocket_Read)(Dart_NativeArguments args) { | |
| 142 SynchronousSocket* socket = SynchronousSocket::GetSocketIdNativeField( | |
| 143 Dart_GetNativeArgument(args, 0)); | |
| 144 int64_t length = 0; | |
| 145 if (DartUtils::GetInt64Value(Dart_GetNativeArgument(args, 1), &length)) { | |
| 146 uint8_t* buffer = NULL; | |
| 147 Dart_Handle result = IOBuffer::Allocate(length, &buffer); | |
| 148 if (Dart_IsError(result)) { | |
| 149 Dart_PropagateError(result); | |
| 150 } | |
| 151 ASSERT(buffer != NULL); | |
| 152 intptr_t bytes_read = SocketBase::Read(socket->fd(), buffer, length); | |
| 153 if (bytes_read == length) { | |
| 154 Dart_SetReturnValue(args, result); | |
| 155 } else if (bytes_read > 0) { | |
| 156 uint8_t* new_buffer = NULL; | |
| 157 Dart_Handle new_result = IOBuffer::Allocate(bytes_read, &new_buffer); | |
| 158 if (Dart_IsError(new_result)) { | |
| 159 Dart_PropagateError(new_result); | |
| 160 } | |
| 161 ASSERT(new_buffer != NULL); | |
| 162 memmove(new_buffer, buffer, bytes_read); | |
| 163 Dart_SetReturnValue(args, new_result); | |
| 164 } else if (bytes_read == 0) { | |
| 165 Dart_SetReturnValue(args, Dart_Null()); | |
| 166 } else { | |
| 167 ASSERT(bytes_read == -1); | |
| 168 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | |
| 169 } | |
| 170 } else { | |
| 171 OSError os_error(-1, "Invalid argument", OSError::kUnknown); | |
| 172 Dart_SetReturnValue(args, DartUtils::NewDartOSError(&os_error)); | |
|
zra
2017/04/07 16:29:40
Above, you return an ArgumentError on a bad argume
bkonyi
2017/04/10 19:20:05
Not sure... Probably something that migrated from
| |
| 173 } | |
| 174 } | |
| 175 | |
| 176 | |
| 177 void FUNCTION_NAME(SynchronousSocket_ShutdownRead)(Dart_NativeArguments args) { | |
| 178 SynchronousSocket* socket = SynchronousSocket::GetSocketIdNativeField( | |
| 179 Dart_GetNativeArgument(args, 0)); | |
| 180 SynchronousSocket::ShutdownRead(socket->fd()); | |
| 181 } | |
| 182 | |
| 183 | |
| 184 void FUNCTION_NAME(SynchronousSocket_ShutdownWrite)(Dart_NativeArguments args) { | |
| 185 SynchronousSocket* socket = SynchronousSocket::GetSocketIdNativeField( | |
| 186 Dart_GetNativeArgument(args, 0)); | |
| 187 SynchronousSocket::ShutdownWrite(socket->fd()); | |
| 188 } | |
| 189 | |
| 190 | |
| 191 void FUNCTION_NAME(SynchronousSocket_GetPort)(Dart_NativeArguments args) { | |
| 192 SynchronousSocket* socket = SynchronousSocket::GetSocketIdNativeField( | |
| 193 Dart_GetNativeArgument(args, 0)); | |
| 194 OSError os_error; | |
|
zra
2017/04/07 05:29:28
Not needed since you are using DartUtils::NewDartO
bkonyi
2017/04/10 19:20:04
Done.
| |
| 195 intptr_t port = SocketBase::GetPort(socket->fd()); | |
| 196 if (port > 0) { | |
| 197 Dart_SetReturnValue(args, Dart_NewInteger(port)); | |
| 198 } else { | |
| 199 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | |
| 200 } | |
| 201 } | |
| 202 | |
| 203 | |
| 204 void FUNCTION_NAME(SynchronousSocket_GetRemotePeer)(Dart_NativeArguments args) { | |
| 205 SynchronousSocket* socket = SynchronousSocket::GetSocketIdNativeField( | |
| 206 Dart_GetNativeArgument(args, 0)); | |
| 207 OSError os_error; | |
|
zra
2017/04/07 05:29:27
ditto
bkonyi
2017/04/10 19:20:05
Done.
| |
| 208 intptr_t port = 0; | |
| 209 SocketAddress* addr = SocketBase::GetRemotePeer(socket->fd(), &port); | |
| 210 if (addr != NULL) { | |
|
zra
2017/04/07 05:29:27
if (addr == NULL) {
// set error
return;
}
bkonyi
2017/04/10 19:20:04
Done.
| |
| 211 Dart_Handle list = Dart_NewList(2); | |
| 212 | |
| 213 Dart_Handle entry = Dart_NewList(3); | |
| 214 Dart_ListSetAt(entry, 0, Dart_NewInteger(addr->GetType())); | |
| 215 Dart_ListSetAt(entry, 1, Dart_NewStringFromCString(addr->as_string())); | |
| 216 | |
| 217 RawAddr raw = addr->addr(); | |
| 218 Dart_ListSetAt(entry, 2, SocketAddress::ToTypedData(raw)); | |
| 219 | |
| 220 Dart_ListSetAt(list, 0, entry); | |
| 221 Dart_ListSetAt(list, 1, Dart_NewInteger(port)); | |
| 222 Dart_SetReturnValue(args, list); | |
| 223 delete addr; | |
| 224 } else { | |
| 225 Dart_SetReturnValue(args, DartUtils::NewDartOSError()); | |
| 226 } | |
| 227 } | |
| 228 | |
| 229 | |
| 230 static void SynchronousSocketFinalizer(void* isolate_data, | |
| 231 Dart_WeakPersistentHandle handle, | |
| 232 void* data) { | |
| 233 SynchronousSocket* socket = reinterpret_cast<SynchronousSocket*>(data); | |
| 234 if (socket->fd() >= 0) { | |
| 235 SocketBase::Close(socket->fd()); | |
| 236 socket->SetClosedFd(); | |
| 237 } | |
|
zra
2017/04/07 05:29:28
delete socket;
bkonyi
2017/04/10 19:20:04
Done.
| |
| 238 } | |
| 239 | |
| 240 | |
| 241 void SynchronousSocket::ReuseSocketIdNativeField(Dart_Handle handle, | |
|
zra
2017/04/07 05:29:27
Not needed.
bkonyi
2017/04/10 19:20:04
Removed.
| |
| 242 SynchronousSocket* socket) { | |
| 243 Dart_Handle err = Dart_SetNativeInstanceField( | |
| 244 handle, kSocketIdNativeField, reinterpret_cast<intptr_t>(socket)); | |
| 245 if (Dart_IsError(err)) { | |
| 246 Dart_PropagateError(err); | |
| 247 } | |
| 248 Dart_NewWeakPersistentHandle(handle, reinterpret_cast<void*>(socket), | |
| 249 sizeof(SynchronousSocket), | |
| 250 SynchronousSocketFinalizer); | |
| 251 } | |
| 252 | |
| 253 | |
| 254 void SynchronousSocket::SetSocketIdNativeField(Dart_Handle handle, | |
| 255 intptr_t id) { | |
| 256 SynchronousSocket* socket = new SynchronousSocket(id); | |
| 257 ReuseSocketIdNativeField(handle, socket); | |
| 258 } | |
| 259 | |
| 260 | |
| 261 SynchronousSocket* SynchronousSocket::GetSocketIdNativeField( | |
| 262 Dart_Handle socket_obj) { | |
| 263 intptr_t id; | |
| 264 Dart_Handle err = | |
| 265 Dart_GetNativeInstanceField(socket_obj, kSocketIdNativeField, &id); | |
| 266 if (Dart_IsError(err)) { | |
| 267 Dart_PropagateError(err); | |
| 268 } | |
| 269 SynchronousSocket* socket = reinterpret_cast<SynchronousSocket*>(id); | |
| 270 return socket; | |
| 271 } | |
| 272 | |
| 273 } // namespace bin | |
| 274 } // namespace dart | |
| 275 | |
| 276 #endif // !defined(DART_IO_DISABLED) | |
| OLD | NEW |