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

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

Issue 11312242: Revised CL for customisable logging (replacing printfs). (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 1 month 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) 2012, 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 <errno.h> 5 #include <errno.h>
6 #include <stdio.h> 6 #include <stdio.h>
7 #include <stdlib.h> 7 #include <stdlib.h>
8 #include <string.h> 8 #include <string.h>
9 #include <unistd.h> 9 #include <unistd.h>
10 10
11 #include "bin/fdutils.h" 11 #include "bin/fdutils.h"
12 #include "bin/log.h"
12 #include "bin/socket.h" 13 #include "bin/socket.h"
13 14
14 15
15 bool Socket::Initialize() { 16 bool Socket::Initialize() {
16 // Nothing to do on Android. 17 // Nothing to do on Android.
17 return true; 18 return true;
18 } 19 }
19 20
20 21
21 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) { 22 intptr_t Socket::CreateConnect(const char* host, const intptr_t port) {
22 intptr_t fd; 23 intptr_t fd;
23 struct hostent* server; 24 struct hostent* server;
24 struct sockaddr_in server_address; 25 struct sockaddr_in server_address;
25 26
26 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); 27 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0));
27 if (fd < 0) { 28 if (fd < 0) {
28 fprintf(stderr, "Error CreateConnect: %s\n", strerror(errno)); 29 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno));
29 return -1; 30 return -1;
30 } 31 }
31 32
32 FDUtils::SetNonBlocking(fd); 33 FDUtils::SetNonBlocking(fd);
33 34
34 server = gethostbyname(host); 35 server = gethostbyname(host);
35 if (server == NULL) { 36 if (server == NULL) {
36 TEMP_FAILURE_RETRY(close(fd)); 37 TEMP_FAILURE_RETRY(close(fd));
37 fprintf(stderr, "Error CreateConnect: %s\n", strerror(errno)); 38 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno));
38 return -1; 39 return -1;
39 } 40 }
40 41
41 server_address.sin_family = AF_INET; 42 server_address.sin_family = AF_INET;
42 server_address.sin_port = htons(port); 43 server_address.sin_port = htons(port);
43 bcopy(server->h_addr, &server_address.sin_addr.s_addr, server->h_length); 44 bcopy(server->h_addr, &server_address.sin_addr.s_addr, server->h_length);
44 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); 45 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero));
45 intptr_t result = TEMP_FAILURE_RETRY( 46 intptr_t result = TEMP_FAILURE_RETRY(
46 connect(fd, 47 connect(fd,
47 reinterpret_cast<struct sockaddr *>(&server_address), 48 reinterpret_cast<struct sockaddr *>(&server_address),
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 86
86 87
87 intptr_t Socket::GetPort(intptr_t fd) { 88 intptr_t Socket::GetPort(intptr_t fd) {
88 ASSERT(fd >= 0); 89 ASSERT(fd >= 0);
89 struct sockaddr_in socket_address; 90 struct sockaddr_in socket_address;
90 socklen_t size = sizeof(socket_address); 91 socklen_t size = sizeof(socket_address);
91 if (TEMP_FAILURE_RETRY( 92 if (TEMP_FAILURE_RETRY(
92 getsockname(fd, 93 getsockname(fd,
93 reinterpret_cast<struct sockaddr *>(&socket_address), 94 reinterpret_cast<struct sockaddr *>(&socket_address),
94 &size))) { 95 &size))) {
95 fprintf(stderr, "Error getsockname: %s\n", strerror(errno)); 96 Log::PrintErr("Error getsockname: %s\n", strerror(errno));
96 return 0; 97 return 0;
97 } 98 }
98 return ntohs(socket_address.sin_port); 99 return ntohs(socket_address.sin_port);
99 } 100 }
100 101
101 102
102 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { 103 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) {
103 ASSERT(fd >= 0); 104 ASSERT(fd >= 0);
104 struct sockaddr_in socket_address; 105 struct sockaddr_in socket_address;
105 socklen_t size = sizeof(socket_address); 106 socklen_t size = sizeof(socket_address);
106 if (TEMP_FAILURE_RETRY( 107 if (TEMP_FAILURE_RETRY(
107 getpeername(fd, 108 getpeername(fd,
108 reinterpret_cast<struct sockaddr *>(&socket_address), 109 reinterpret_cast<struct sockaddr *>(&socket_address),
109 &size))) { 110 &size))) {
110 fprintf(stderr, "Error getpeername: %s\n", strerror(errno)); 111 Log::PrintErr("Error getpeername: %s\n", strerror(errno));
111 return false; 112 return false;
112 } 113 }
113 if (inet_ntop(socket_address.sin_family, 114 if (inet_ntop(socket_address.sin_family,
114 reinterpret_cast<const void *>(&socket_address.sin_addr), 115 reinterpret_cast<const void *>(&socket_address.sin_addr),
115 host, 116 host,
116 INET_ADDRSTRLEN) == NULL) { 117 INET_ADDRSTRLEN) == NULL) {
117 fprintf(stderr, "Error inet_ntop: %s\n", strerror(errno)); 118 Log::PrintErr("Error inet_ntop: %s\n", strerror(errno));
118 return false; 119 return false;
119 } 120 }
120 *port = ntohs(socket_address.sin_port); 121 *port = ntohs(socket_address.sin_port);
121 return true; 122 return true;
122 } 123 }
123 124
124 125
125 void Socket::GetError(intptr_t fd, OSError* os_error) { 126 void Socket::GetError(intptr_t fd, OSError* os_error) {
126 int errorNumber; 127 int errorNumber;
127 socklen_t len = sizeof(errorNumber); 128 socklen_t len = sizeof(errorNumber);
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 intptr_t fd; 178 intptr_t fd;
178 struct sockaddr_in server_address; 179 struct sockaddr_in server_address;
179 180
180 in_addr_t s_addr = inet_addr(host); 181 in_addr_t s_addr = inet_addr(host);
181 if (s_addr == INADDR_NONE) { 182 if (s_addr == INADDR_NONE) {
182 return -5; 183 return -5;
183 } 184 }
184 185
185 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); 186 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0));
186 if (fd < 0) { 187 if (fd < 0) {
187 fprintf(stderr, "Error CreateBind: %s\n", strerror(errno)); 188 Log::PrintErr("Error CreateBind: %s\n", strerror(errno));
188 return -1; 189 return -1;
189 } 190 }
190 191
191 int optval = 1; 192 int optval = 1;
192 TEMP_FAILURE_RETRY( 193 TEMP_FAILURE_RETRY(
193 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); 194 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)));
194 195
195 server_address.sin_family = AF_INET; 196 server_address.sin_family = AF_INET;
196 server_address.sin_port = htons(port); 197 server_address.sin_port = htons(port);
197 server_address.sin_addr.s_addr = s_addr; 198 server_address.sin_addr.s_addr = s_addr;
198 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); 199 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero));
199 200
200 if (TEMP_FAILURE_RETRY( 201 if (TEMP_FAILURE_RETRY(
201 bind(fd, 202 bind(fd,
202 reinterpret_cast<struct sockaddr *>(&server_address), 203 reinterpret_cast<struct sockaddr *>(&server_address),
203 sizeof(server_address))) < 0) { 204 sizeof(server_address))) < 0) {
204 TEMP_FAILURE_RETRY(close(fd)); 205 TEMP_FAILURE_RETRY(close(fd));
205 fprintf(stderr, "Error Bind: %s\n", strerror(errno)); 206 Log::PrintErr("Error Bind: %s\n", strerror(errno));
206 return -1; 207 return -1;
207 } 208 }
208 209
209 if (TEMP_FAILURE_RETRY(listen(fd, backlog)) != 0) { 210 if (TEMP_FAILURE_RETRY(listen(fd, backlog)) != 0) {
210 fprintf(stderr, "Error Listen: %s\n", strerror(errno)); 211 Log::PrintErr("Error Listen: %s\n", strerror(errno));
211 return -1; 212 return -1;
212 } 213 }
213 214
214 FDUtils::SetNonBlocking(fd); 215 FDUtils::SetNonBlocking(fd);
215 return fd; 216 return fd;
216 } 217 }
217 218
218 219
219 static bool IsTemporaryAcceptError(int error) { 220 static bool IsTemporaryAcceptError(int error) {
220 // On Android a number of protocol errors should be treated as EAGAIN. 221 // On Android a number of protocol errors should be treated as EAGAIN.
(...skipping 16 matching lines...) Expand all
237 // error. We got woken up from the poll on the listening socket, 238 // error. We got woken up from the poll on the listening socket,
238 // but there is no connection ready to be accepted. 239 // but there is no connection ready to be accepted.
239 ASSERT(kTemporaryFailure != -1); 240 ASSERT(kTemporaryFailure != -1);
240 socket = kTemporaryFailure; 241 socket = kTemporaryFailure;
241 } 242 }
242 } else { 243 } else {
243 FDUtils::SetNonBlocking(socket); 244 FDUtils::SetNonBlocking(socket);
244 } 245 }
245 return socket; 246 return socket;
246 } 247 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698