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

Side by Side Diff: runtime/bin/socket_macos.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 Mac OS. 17 // Nothing to do on Mac OS.
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 len = sizeof(errno); 127 int len = sizeof(errno);
127 getsockopt(fd, 128 getsockopt(fd,
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
176 intptr_t fd; 177 intptr_t fd;
177 struct sockaddr_in server_address; 178 struct sockaddr_in server_address;
178 179
179 in_addr_t s_addr = inet_addr(host); 180 in_addr_t s_addr = inet_addr(host);
180 if (s_addr == INADDR_NONE) { 181 if (s_addr == INADDR_NONE) {
181 return -5; 182 return -5;
182 } 183 }
183 184
184 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0)); 185 fd = TEMP_FAILURE_RETRY(socket(AF_INET, SOCK_STREAM, 0));
185 if (fd < 0) { 186 if (fd < 0) {
186 fprintf(stderr, "Error CreateBind: %s\n", strerror(errno)); 187 Log::PrintErr("Error CreateBind: %s\n", strerror(errno));
187 return -1; 188 return -1;
188 } 189 }
189 190
190 int optval = 1; 191 int optval = 1;
191 TEMP_FAILURE_RETRY( 192 TEMP_FAILURE_RETRY(
192 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))); 193 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval)));
193 194
194 server_address.sin_family = AF_INET; 195 server_address.sin_family = AF_INET;
195 server_address.sin_port = htons(port); 196 server_address.sin_port = htons(port);
196 server_address.sin_addr.s_addr = s_addr; 197 server_address.sin_addr.s_addr = s_addr;
197 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero)); 198 memset(&server_address.sin_zero, 0, sizeof(server_address.sin_zero));
198 199
199 if (TEMP_FAILURE_RETRY( 200 if (TEMP_FAILURE_RETRY(
200 bind(fd, 201 bind(fd,
201 reinterpret_cast<struct sockaddr *>(&server_address), 202 reinterpret_cast<struct sockaddr *>(&server_address),
202 sizeof(server_address))) < 0) { 203 sizeof(server_address))) < 0) {
203 TEMP_FAILURE_RETRY(close(fd)); 204 TEMP_FAILURE_RETRY(close(fd));
204 fprintf(stderr, "Error Bind: %s\n", strerror(errno)); 205 Log::PrintErr("Error Bind: %s\n", strerror(errno));
205 return -1; 206 return -1;
206 } 207 }
207 208
208 if (TEMP_FAILURE_RETRY(listen(fd, backlog)) != 0) { 209 if (TEMP_FAILURE_RETRY(listen(fd, backlog)) != 0) {
209 fprintf(stderr, "Error Listen: %s\n", strerror(errno)); 210 Log::PrintErr("Error Listen: %s\n", strerror(errno));
210 return -1; 211 return -1;
211 } 212 }
212 213
213 FDUtils::SetNonBlocking(fd); 214 FDUtils::SetNonBlocking(fd);
214 return fd; 215 return fd;
215 } 216 }
216 217
217 218
218 intptr_t ServerSocket::Accept(intptr_t fd) { 219 intptr_t ServerSocket::Accept(intptr_t fd) {
219 intptr_t socket; 220 intptr_t socket;
220 struct sockaddr clientaddr; 221 struct sockaddr clientaddr;
221 socklen_t addrlen = sizeof(clientaddr); 222 socklen_t addrlen = sizeof(clientaddr);
222 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen)); 223 socket = TEMP_FAILURE_RETRY(accept(fd, &clientaddr, &addrlen));
223 if (socket == -1) { 224 if (socket == -1) {
224 if (errno == EAGAIN) { 225 if (errno == EAGAIN) {
225 // We need to signal to the caller that this is actually not an 226 // We need to signal to the caller that this is actually not an
226 // error. We got woken up from the poll on the listening socket, 227 // error. We got woken up from the poll on the listening socket,
227 // but there is no connection ready to be accepted. 228 // but there is no connection ready to be accepted.
228 ASSERT(kTemporaryFailure != -1); 229 ASSERT(kTemporaryFailure != -1);
229 socket = kTemporaryFailure; 230 socket = kTemporaryFailure;
230 } 231 }
231 } else { 232 } else {
232 FDUtils::SetNonBlocking(socket); 233 FDUtils::SetNonBlocking(socket);
233 } 234 }
234 return socket; 235 return socket;
235 } 236 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698