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

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

Issue 9699017: Start better error reporting for sockets (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Added tests Created 8 years, 9 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "bin/socket.h" 5 #include "bin/socket.h"
6 #include "bin/dartutils.h" 6 #include "bin/dartutils.h"
7 7 #include "bin/thread.h"
8 #include "bin/utils.h"
9 #include "platform/thread.h"
8 #include "platform/utils.h" 10 #include "platform/utils.h"
9 11
10 #include "include/dart_api.h" 12 #include "include/dart_api.h"
11 13
14 dart::Mutex Socket::mutex_;
15 int Socket::service_ports_size_ = 0;
16 Dart_Port* Socket::service_ports_ = NULL;
17 int Socket::service_ports_index_ = 0;
12 18
13 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) { 19 void FUNCTION_NAME(Socket_CreateConnect)(Dart_NativeArguments args) {
14 Dart_EnterScope(); 20 Dart_EnterScope();
15 Dart_Handle socketobj = Dart_GetNativeArgument(args, 0); 21 Dart_Handle socketobj = Dart_GetNativeArgument(args, 0);
16 const char* host = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); 22 const char* host = DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1));
17 int64_t port = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); 23 int64_t port = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2));
18 intptr_t socket = Socket::CreateConnect(host, port); 24 intptr_t socket = Socket::CreateConnect(host, port);
19 DartUtils::SetIntegerField(socketobj, DartUtils::kIdFieldName, socket); 25 DartUtils::SetIntegerField(socketobj, DartUtils::kIdFieldName, socket);
20 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0)); 26 Dart_SetReturnValue(args, Dart_NewBoolean(socket >= 0));
21 Dart_ExitScope(); 27 Dart_ExitScope();
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 DartUtils::kIdFieldName); 174 DartUtils::kIdFieldName);
169 Dart_Handle socketobj = Dart_GetNativeArgument(args, 1); 175 Dart_Handle socketobj = Dart_GetNativeArgument(args, 1);
170 intptr_t newSocket = ServerSocket::Accept(socket); 176 intptr_t newSocket = ServerSocket::Accept(socket);
171 if (newSocket >= 0) { 177 if (newSocket >= 0) {
172 DartUtils::SetIntegerField( 178 DartUtils::SetIntegerField(
173 socketobj, DartUtils::kIdFieldName, newSocket); 179 socketobj, DartUtils::kIdFieldName, newSocket);
174 } 180 }
175 Dart_SetReturnValue(args, Dart_NewBoolean(newSocket >= 0)); 181 Dart_SetReturnValue(args, Dart_NewBoolean(newSocket >= 0));
176 Dart_ExitScope(); 182 Dart_ExitScope();
177 } 183 }
184
185
186 static CObject* LookupRequest(const CObjectArray& request) {
187 if (request.Length() == 2 && request[1]->IsString()) {
188 CObjectString host(request[1]);
189 CObject* result = NULL;
190 OSError* os_error = NULL;
191 const char* ip_address =
192 Socket::LookupIPv4Address(host.CString(), &os_error);
193 if (ip_address != NULL) {
194 result = new CObjectString(CObject::NewString(ip_address));
195 free(const_cast<char*>(ip_address));
196 } else {
197 result = CObject::NewOSError(os_error);
198 delete os_error;
199 }
200 return result;
201 }
202 return CObject::IllegalArgumentError();
203 }
204
205
206 void SocketService(Dart_Port dest_port_id,
207 Dart_Port reply_port_id,
208 Dart_CObject* message) {
209 CObject* response = CObject::False();
210 CObjectArray request(message);
211 if (message->type == Dart_CObject::kArray) {
212 if (request.Length() > 1 && request[0]->IsInt32()) {
213 CObjectInt32 requestType(request[0]);
214 switch (requestType.Value()) {
215 case Socket::kLookupRequest:
216 response = LookupRequest(request);
217 break;
218 default:
219 UNREACHABLE();
220 }
221 }
222 }
223
224 Dart_PostCObject(reply_port_id, response->AsApiCObject());
225 }
226
227
228 Dart_Port Socket::GetServicePort() {
229 MutexLocker lock(&mutex_);
230 if (service_ports_size_ == 0) {
231 ASSERT(service_ports_ == NULL);
232 service_ports_size_ = 16;
233 service_ports_ = new Dart_Port[service_ports_size_];
234 service_ports_index_ = 0;
235 for (int i = 0; i < service_ports_size_; i++) {
236 service_ports_[i] = kIllegalPort;
237 }
238 }
239
240 Dart_Port result = service_ports_[service_ports_index_];
241 if (result == kIllegalPort) {
242 result = Dart_NewNativePort("SocketService",
243 SocketService,
244 true);
245 ASSERT(result != kIllegalPort);
246 service_ports_[service_ports_index_] = result;
247 }
248 service_ports_index_ = (service_ports_index_ + 1) % service_ports_size_;
249 return result;
250 }
251
252
253 void FUNCTION_NAME(Socket_NewServicePort)(Dart_NativeArguments args) {
254 Dart_EnterScope();
255 Dart_SetReturnValue(args, Dart_Null());
256 Dart_Port service_port = Socket::GetServicePort();
257 if (service_port != kIllegalPort) {
258 // Return a send port for the service port.
259 Dart_Handle send_port = Dart_NewSendPort(service_port);
260 Dart_SetReturnValue(args, send_port);
261 }
262 Dart_ExitScope();
263 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698