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

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
« no previous file with comments | « runtime/bin/process_win.cc ('k') | runtime/bin/socket_linux.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/socket.h"
11 #include "bin/fdutils.h" 12 #include "bin/fdutils.h"
12 #include "bin/socket.h" 13 #include "bin/log.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::SetCloseOnExec(fd); 33 FDUtils::SetCloseOnExec(fd);
33 FDUtils::SetNonBlocking(fd); 34 FDUtils::SetNonBlocking(fd);
34 35
35 server = gethostbyname(host); 36 server = gethostbyname(host);
36 if (server == NULL) { 37 if (server == NULL) {
37 TEMP_FAILURE_RETRY(close(fd)); 38 TEMP_FAILURE_RETRY(close(fd));
38 fprintf(stderr, "Error CreateConnect: %s\n", strerror(errno)); 39 Log::PrintErr("Error CreateConnect: %s\n", strerror(errno));
39 return -1; 40 return -1;
40 } 41 }
41 42
42 server_address.sin_family = AF_INET; 43 server_address.sin_family = AF_INET;
43 server_address.sin_port = htons(port); 44 server_address.sin_port = htons(port);
44 bcopy(server->h_addr, &server_address.sin_addr.s_addr, server->h_length); 45 bcopy(server->h_addr, &server_address.sin_addr.s_addr, server->h_length);
45 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); 46 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero));
46 intptr_t result = TEMP_FAILURE_RETRY( 47 intptr_t result = TEMP_FAILURE_RETRY(
47 connect(fd, 48 connect(fd,
48 reinterpret_cast<struct sockaddr *>(&server_address), 49 reinterpret_cast<struct sockaddr *>(&server_address),
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 87
87 88
88 intptr_t Socket::GetPort(intptr_t fd) { 89 intptr_t Socket::GetPort(intptr_t fd) {
89 ASSERT(fd >= 0); 90 ASSERT(fd >= 0);
90 struct sockaddr_in socket_address; 91 struct sockaddr_in socket_address;
91 socklen_t size = sizeof(socket_address); 92 socklen_t size = sizeof(socket_address);
92 if (TEMP_FAILURE_RETRY( 93 if (TEMP_FAILURE_RETRY(
93 getsockname(fd, 94 getsockname(fd,
94 reinterpret_cast<struct sockaddr *>(&socket_address), 95 reinterpret_cast<struct sockaddr *>(&socket_address),
95 &size))) { 96 &size))) {
96 fprintf(stderr, "Error getsockname: %s\n", strerror(errno)); 97 Log::PrintErr("Error getsockname: %s\n", strerror(errno));
97 return 0; 98 return 0;
98 } 99 }
99 return ntohs(socket_address.sin_port); 100 return ntohs(socket_address.sin_port);
100 } 101 }
101 102
102 103
103 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) { 104 bool Socket::GetRemotePeer(intptr_t fd, char *host, intptr_t *port) {
104 ASSERT(fd >= 0); 105 ASSERT(fd >= 0);
105 struct sockaddr_in socket_address; 106 struct sockaddr_in socket_address;
106 socklen_t size = sizeof(socket_address); 107 socklen_t size = sizeof(socket_address);
107 if (TEMP_FAILURE_RETRY( 108 if (TEMP_FAILURE_RETRY(
108 getpeername(fd, 109 getpeername(fd,
109 reinterpret_cast<struct sockaddr *>(&socket_address), 110 reinterpret_cast<struct sockaddr *>(&socket_address),
110 &size))) { 111 &size))) {
111 fprintf(stderr, "Error getpeername: %s\n", strerror(errno)); 112 Log::PrintErr("Error getpeername: %s\n", strerror(errno));
112 return false; 113 return false;
113 } 114 }
114 if (inet_ntop(socket_address.sin_family, 115 if (inet_ntop(socket_address.sin_family,
115 reinterpret_cast<const void *>(&socket_address.sin_addr), 116 reinterpret_cast<const void *>(&socket_address.sin_addr),
116 host, 117 host,
117 INET_ADDRSTRLEN) == NULL) { 118 INET_ADDRSTRLEN) == NULL) {
118 fprintf(stderr, "Error inet_ntop: %s\n", strerror(errno)); 119 Log::PrintErr("Error inet_ntop: %s\n", strerror(errno));
119 return false; 120 return false;
120 } 121 }
121 *port = ntohs(socket_address.sin_port); 122 *port = ntohs(socket_address.sin_port);
122 return true; 123 return true;
123 } 124 }
124 125
125 126
126 void Socket::GetError(intptr_t fd, OSError* os_error) { 127 void Socket::GetError(intptr_t fd, OSError* os_error) {
127 int errorNumber; 128 int errorNumber;
128 socklen_t len = sizeof(errorNumber); 129 socklen_t len = sizeof(errorNumber);
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 intptr_t fd; 179 intptr_t fd;
179 struct sockaddr_in server_address; 180 struct sockaddr_in server_address;
180 181
181 in_addr_t s_addr = inet_addr(host); 182 in_addr_t s_addr = inet_addr(host);
182 if (s_addr == INADDR_NONE) { 183 if (s_addr == INADDR_NONE) {
183 return -5; 184 return -5;
184 } 185 }
185 186
186 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); 187 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0));
187 if (fd < 0) { 188 if (fd < 0) {
188 fprintf(stderr, "Error CreateBind: %s\n", strerror(errno)); 189 Log::PrintErr("Error CreateBind: %s\n", strerror(errno));
189 return -1; 190 return -1;
190 } 191 }
191 192
192 FDUtils::SetCloseOnExec(fd); 193 FDUtils::SetCloseOnExec(fd);
193 194
194 int optval = 1; 195 int optval = 1;
195 TEMP_FAILURE_RETRY( 196 TEMP_FAILURE_RETRY(
196 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); 197 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)));
197 198
198 server_address.sin_family = AF_INET; 199 server_address.sin_family = AF_INET;
199 server_address.sin_port = htons(port); 200 server_address.sin_port = htons(port);
200 server_address.sin_addr.s_addr = s_addr; 201 server_address.sin_addr.s_addr = s_addr;
201 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); 202 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero));
202 203
203 if (TEMP_FAILURE_RETRY( 204 if (TEMP_FAILURE_RETRY(
204 bind(fd, 205 bind(fd,
205 reinterpret_cast<struct sockaddr *>(&server_address), 206 reinterpret_cast<struct sockaddr *>(&server_address),
206 sizeof(server_address))) < 0) { 207 sizeof(server_address))) < 0) {
207 TEMP_FAILURE_RETRY(close(fd)); 208 TEMP_FAILURE_RETRY(close(fd));
208 fprintf(stderr, "Error Bind: %s\n", strerror(errno)); 209 Log::PrintErr("Error Bind: %s\n", strerror(errno));
209 return -1; 210 return -1;
210 } 211 }
211 212
212 if (TEMP_FAILURE_RETRY(listen(fd, backlog)) != 0) { 213 if (TEMP_FAILURE_RETRY(listen(fd, backlog)) != 0) {
213 fprintf(stderr, "Error Listen: %s\n", strerror(errno)); 214 Log::PrintErr("Error Listen: %s\n", strerror(errno));
214 return -1; 215 return -1;
215 } 216 }
216 217
217 FDUtils::SetNonBlocking(fd); 218 FDUtils::SetNonBlocking(fd);
218 return fd; 219 return fd;
219 } 220 }
220 221
221 222
222 static bool IsTemporaryAcceptError(int error) { 223 static bool IsTemporaryAcceptError(int error) {
223 // On Android a number of protocol errors should be treated as EAGAIN. 224 // On Android a number of protocol errors should be treated as EAGAIN.
(...skipping 16 matching lines...) Expand all
240 // error. We got woken up from the poll on the listening socket, 241 // error. We got woken up from the poll on the listening socket,
241 // but there is no connection ready to be accepted. 242 // but there is no connection ready to be accepted.
242 ASSERT(kTemporaryFailure != -1); 243 ASSERT(kTemporaryFailure != -1);
243 socket = kTemporaryFailure; 244 socket = kTemporaryFailure;
244 } 245 }
245 } else { 246 } else {
246 FDUtils::SetNonBlocking(socket); 247 FDUtils::SetNonBlocking(socket);
247 } 248 }
248 return socket; 249 return socket;
249 } 250 }
OLDNEW
« no previous file with comments | « runtime/bin/process_win.cc ('k') | runtime/bin/socket_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698