OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/shell/domain_socket/socket_libevent.h" | 5 #include "mojo/shell/domain_socket/socket_libevent.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <netinet/in.h> | 9 #include <netinet/in.h> |
10 #include <sys/socket.h> | 10 #include <sys/socket.h> |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 return net::ERR_CONNECTION_TIMED_OUT; | 56 return net::ERR_CONNECTION_TIMED_OUT; |
57 default: { | 57 default: { |
58 int net_error = net::MapSystemError(os_error); | 58 int net_error = net::MapSystemError(os_error); |
59 if (net_error == net::ERR_FAILED) | 59 if (net_error == net::ERR_FAILED) |
60 return net::ERR_CONNECTION_FAILED; // More specific than ERR_FAILED. | 60 return net::ERR_CONNECTION_FAILED; // More specific than ERR_FAILED. |
61 return net_error; | 61 return net_error; |
62 } | 62 } |
63 } | 63 } |
64 } | 64 } |
65 | 65 |
| 66 int SetNonBlocking(int fd) { |
| 67 int flags = fcntl(fd, F_GETFL, 0); |
| 68 if (-1 == flags) |
| 69 return flags; |
| 70 return fcntl(fd, F_SETFL, flags | O_NONBLOCK); |
| 71 } |
| 72 |
66 } // namespace | 73 } // namespace |
67 | 74 |
68 SocketLibevent::SocketLibevent() | 75 SocketLibevent::SocketLibevent() |
69 : socket_fd_(kInvalidSocket), waiting_connect_(false) { | 76 : socket_fd_(kInvalidSocket), waiting_connect_(false) { |
70 } | 77 } |
71 | 78 |
72 SocketLibevent::~SocketLibevent() { | 79 SocketLibevent::~SocketLibevent() { |
73 Close(); | 80 Close(); |
74 } | 81 } |
75 | 82 |
76 int SocketLibevent::Open(int address_family) { | 83 int SocketLibevent::Open(int address_family) { |
77 DCHECK(thread_checker_.CalledOnValidThread()); | 84 DCHECK(thread_checker_.CalledOnValidThread()); |
78 DCHECK_EQ(kInvalidSocket, socket_fd_); | 85 DCHECK_EQ(kInvalidSocket, socket_fd_); |
79 DCHECK(address_family == AF_INET || address_family == AF_INET6 || | 86 DCHECK(address_family == AF_INET || address_family == AF_INET6 || |
80 address_family == AF_UNIX); | 87 address_family == AF_UNIX); |
81 | 88 |
| 89 int socket_type = SOCK_STREAM; |
| 90 #ifdef SOCK_NONBLOCK |
| 91 socket_type |= SOCK_NONBLOCK; |
| 92 #endif |
82 socket_fd_ = ::socket(address_family, | 93 socket_fd_ = ::socket(address_family, |
83 SOCK_STREAM | SOCK_NONBLOCK, | 94 socket_type, |
84 address_family == AF_UNIX ? 0 : IPPROTO_TCP); | 95 address_family == AF_UNIX ? 0 : IPPROTO_TCP); |
| 96 #ifndef SOCK_NONBLOCK |
| 97 if (SetNonBlocking(socket_fd_) != 0) { |
| 98 PLOG(ERROR) << "SetNonBlocking() returned an error, errno=" << errno; |
| 99 return net::MapSystemError(errno); |
| 100 } |
| 101 #endif |
85 if (socket_fd_ < 0) { | 102 if (socket_fd_ < 0) { |
86 PLOG(ERROR) << "CreatePlatformSocket() returned an error, errno=" << errno; | 103 PLOG(ERROR) << "CreatePlatformSocket() returned an error, errno=" << errno; |
87 return net::MapSystemError(errno); | 104 return net::MapSystemError(errno); |
88 } | 105 } |
89 | 106 |
90 return net::OK; | 107 return net::OK; |
91 } | 108 } |
92 | 109 |
93 namespace { | |
94 int SetNonBlocking(int fd) { | |
95 int flags = fcntl(fd, F_GETFL, 0); | |
96 if (-1 == flags) | |
97 return flags; | |
98 return fcntl(fd, F_SETFL, flags | O_NONBLOCK); | |
99 } | |
100 } // namespace | |
101 | |
102 int SocketLibevent::AdoptConnectedSocket(SocketDescriptor socket, | 110 int SocketLibevent::AdoptConnectedSocket(SocketDescriptor socket, |
103 const SockaddrStorage& address) { | 111 const SockaddrStorage& address) { |
104 DCHECK(thread_checker_.CalledOnValidThread()); | 112 DCHECK(thread_checker_.CalledOnValidThread()); |
105 DCHECK_EQ(kInvalidSocket, socket_fd_); | 113 DCHECK_EQ(kInvalidSocket, socket_fd_); |
106 | 114 |
107 socket_fd_ = socket; | 115 socket_fd_ = socket; |
108 | 116 |
109 if (SetNonBlocking(socket_fd_)) { | 117 if (SetNonBlocking(socket_fd_)) { |
110 int rv = net::MapSystemError(errno); | 118 int rv = net::MapSystemError(errno); |
111 Close(); | 119 Close(); |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 accept_socket_ = NULL; | 378 accept_socket_ = NULL; |
371 accept_callback_.Reset(); | 379 accept_callback_.Reset(); |
372 } | 380 } |
373 | 381 |
374 waiting_connect_ = false; | 382 waiting_connect_ = false; |
375 peer_address_.reset(); | 383 peer_address_.reset(); |
376 } | 384 } |
377 | 385 |
378 } // namespace shell | 386 } // namespace shell |
379 } // namespace mojo | 387 } // namespace mojo |
OLD | NEW |