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 "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> |
11 #include <unistd.h> | 11 #include <unistd.h> |
12 | 12 |
13 #include "base/callback_helpers.h" | 13 #include "base/callback_helpers.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/posix/eintr_wrapper.h" | 15 #include "base/posix/eintr_wrapper.h" |
16 #include "mojo/shell/domain_socket/net_errors.h" | 16 #include "shell/domain_socket/net_errors.h" |
17 | 17 |
18 namespace mojo { | 18 namespace mojo { |
19 namespace shell { | 19 namespace shell { |
20 | 20 |
21 SockaddrStorage::SockaddrStorage(const SockaddrStorage& other) | 21 SockaddrStorage::SockaddrStorage(const SockaddrStorage& other) |
22 : addr_len(other.addr_len), | 22 : addr_len(other.addr_len), |
23 addr(reinterpret_cast<struct sockaddr*>(&addr_storage)) { | 23 addr(reinterpret_cast<struct sockaddr*>(&addr_storage)) { |
24 memcpy(addr, other.addr, addr_len); | 24 memcpy(addr, other.addr, addr_len); |
25 } | 25 } |
26 | 26 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 int SocketLibevent::Open(int address_family) { | 83 int SocketLibevent::Open(int address_family) { |
84 DCHECK(thread_checker_.CalledOnValidThread()); | 84 DCHECK(thread_checker_.CalledOnValidThread()); |
85 DCHECK_EQ(kInvalidSocket, socket_fd_); | 85 DCHECK_EQ(kInvalidSocket, socket_fd_); |
86 DCHECK(address_family == AF_INET || address_family == AF_INET6 || | 86 DCHECK(address_family == AF_INET || address_family == AF_INET6 || |
87 address_family == AF_UNIX); | 87 address_family == AF_UNIX); |
88 | 88 |
89 int socket_type = SOCK_STREAM; | 89 int socket_type = SOCK_STREAM; |
90 #ifdef SOCK_NONBLOCK | 90 #ifdef SOCK_NONBLOCK |
91 socket_type |= SOCK_NONBLOCK; | 91 socket_type |= SOCK_NONBLOCK; |
92 #endif | 92 #endif |
93 socket_fd_ = ::socket(address_family, | 93 socket_fd_ = ::socket(address_family, socket_type, |
94 socket_type, | |
95 address_family == AF_UNIX ? 0 : IPPROTO_TCP); | 94 address_family == AF_UNIX ? 0 : IPPROTO_TCP); |
96 #ifndef SOCK_NONBLOCK | 95 #ifndef SOCK_NONBLOCK |
97 if (SetNonBlocking(socket_fd_) != 0) { | 96 if (SetNonBlocking(socket_fd_) != 0) { |
98 PLOG(ERROR) << "SetNonBlocking() returned an error, errno=" << errno; | 97 PLOG(ERROR) << "SetNonBlocking() returned an error, errno=" << errno; |
99 return net::MapSystemError(errno); | 98 return net::MapSystemError(errno); |
100 } | 99 } |
101 #endif | 100 #endif |
102 if (socket_fd_ < 0) { | 101 if (socket_fd_ < 0) { |
103 PLOG(ERROR) << "CreatePlatformSocket() returned an error, errno=" << errno; | 102 PLOG(ERROR) << "CreatePlatformSocket() returned an error, errno=" << errno; |
104 return net::MapSystemError(errno); | 103 return net::MapSystemError(errno); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 DCHECK_NE(kInvalidSocket, socket_fd_); | 163 DCHECK_NE(kInvalidSocket, socket_fd_); |
165 DCHECK(accept_callback_.is_null()); | 164 DCHECK(accept_callback_.is_null()); |
166 DCHECK(socket); | 165 DCHECK(socket); |
167 DCHECK(!callback.is_null()); | 166 DCHECK(!callback.is_null()); |
168 | 167 |
169 int rv = DoAccept(socket); | 168 int rv = DoAccept(socket); |
170 if (rv != net::ERR_IO_PENDING) | 169 if (rv != net::ERR_IO_PENDING) |
171 return rv; | 170 return rv; |
172 | 171 |
173 if (!base::MessageLoopForIO::current()->WatchFileDescriptor( | 172 if (!base::MessageLoopForIO::current()->WatchFileDescriptor( |
174 socket_fd_, | 173 socket_fd_, true, base::MessageLoopForIO::WATCH_READ, |
175 true, | 174 &accept_socket_watcher_, this)) { |
176 base::MessageLoopForIO::WATCH_READ, | |
177 &accept_socket_watcher_, | |
178 this)) { | |
179 PLOG(ERROR) << "WatchFileDescriptor failed on accept, errno " << errno; | 175 PLOG(ERROR) << "WatchFileDescriptor failed on accept, errno " << errno; |
180 return net::MapSystemError(errno); | 176 return net::MapSystemError(errno); |
181 } | 177 } |
182 | 178 |
183 accept_socket_ = socket; | 179 accept_socket_ = socket; |
184 accept_callback_ = callback; | 180 accept_callback_ = callback; |
185 return net::ERR_IO_PENDING; | 181 return net::ERR_IO_PENDING; |
186 } | 182 } |
187 | 183 |
188 int SocketLibevent::Connect(const SockaddrStorage& address, | 184 int SocketLibevent::Connect(const SockaddrStorage& address, |
189 const CompletionCallback& callback) { | 185 const CompletionCallback& callback) { |
190 DCHECK(thread_checker_.CalledOnValidThread()); | 186 DCHECK(thread_checker_.CalledOnValidThread()); |
191 DCHECK_NE(kInvalidSocket, socket_fd_); | 187 DCHECK_NE(kInvalidSocket, socket_fd_); |
192 DCHECK(!waiting_connect_); | 188 DCHECK(!waiting_connect_); |
193 DCHECK(!callback.is_null()); | 189 DCHECK(!callback.is_null()); |
194 | 190 |
195 SetPeerAddress(address); | 191 SetPeerAddress(address); |
196 | 192 |
197 int rv = DoConnect(); | 193 int rv = DoConnect(); |
198 if (rv != net::ERR_IO_PENDING) | 194 if (rv != net::ERR_IO_PENDING) |
199 return rv; | 195 return rv; |
200 | 196 |
201 if (!base::MessageLoopForIO::current()->WatchFileDescriptor( | 197 if (!base::MessageLoopForIO::current()->WatchFileDescriptor( |
202 socket_fd_, | 198 socket_fd_, true, base::MessageLoopForIO::WATCH_WRITE, |
203 true, | 199 &write_socket_watcher_, this)) { |
204 base::MessageLoopForIO::WATCH_WRITE, | |
205 &write_socket_watcher_, | |
206 this)) { | |
207 PLOG(ERROR) << "WatchFileDescriptor failed on connect, errno " << errno; | 200 PLOG(ERROR) << "WatchFileDescriptor failed on connect, errno " << errno; |
208 return net::MapSystemError(errno); | 201 return net::MapSystemError(errno); |
209 } | 202 } |
210 | 203 |
211 write_callback_ = callback; | 204 write_callback_ = callback; |
212 waiting_connect_ = true; | 205 waiting_connect_ = true; |
213 return net::ERR_IO_PENDING; | 206 return net::ERR_IO_PENDING; |
214 } | 207 } |
215 | 208 |
216 bool SocketLibevent::IsConnected() const { | 209 bool SocketLibevent::IsConnected() const { |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 accept_socket_ = NULL; | 371 accept_socket_ = NULL; |
379 accept_callback_.Reset(); | 372 accept_callback_.Reset(); |
380 } | 373 } |
381 | 374 |
382 waiting_connect_ = false; | 375 waiting_connect_ = false; |
383 peer_address_.reset(); | 376 peer_address_.reset(); |
384 } | 377 } |
385 | 378 |
386 } // namespace shell | 379 } // namespace shell |
387 } // namespace mojo | 380 } // namespace mojo |
OLD | NEW |