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 "net/socket/socket_libevent.h" | 5 #include "net/socket/socket_libevent.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <netinet/in.h> | 8 #include <netinet/in.h> |
9 #include <sys/socket.h> | 9 #include <sys/socket.h> |
10 | 10 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 if (SetNonBlocking(socket_fd_)) { | 99 if (SetNonBlocking(socket_fd_)) { |
100 int rv = MapSystemError(errno); | 100 int rv = MapSystemError(errno); |
101 Close(); | 101 Close(); |
102 return rv; | 102 return rv; |
103 } | 103 } |
104 | 104 |
105 SetPeerAddress(address); | 105 SetPeerAddress(address); |
106 return OK; | 106 return OK; |
107 } | 107 } |
108 | 108 |
| 109 SocketDescriptor SocketLibevent::ReleaseConnectedSocket() { |
| 110 StopWatchingAndCleanUp(); |
| 111 SocketDescriptor socket_fd = socket_fd_; |
| 112 socket_fd_ = kInvalidSocket; |
| 113 return socket_fd; |
| 114 } |
| 115 |
109 int SocketLibevent::Bind(const SockaddrStorage& address) { | 116 int SocketLibevent::Bind(const SockaddrStorage& address) { |
110 DCHECK(thread_checker_.CalledOnValidThread()); | 117 DCHECK(thread_checker_.CalledOnValidThread()); |
111 DCHECK_NE(kInvalidSocket, socket_fd_); | 118 DCHECK_NE(kInvalidSocket, socket_fd_); |
112 | 119 |
113 int rv = bind(socket_fd_, address.addr, address.addr_len); | 120 int rv = bind(socket_fd_, address.addr, address.addr_len); |
114 if (rv < 0) { | 121 if (rv < 0) { |
115 PLOG(ERROR) << "bind() returned an error, errno=" << errno; | 122 PLOG(ERROR) << "bind() returned an error, errno=" << errno; |
116 return MapSystemError(errno); | 123 return MapSystemError(errno); |
117 } | 124 } |
118 | 125 |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
319 } | 326 } |
320 | 327 |
321 bool SocketLibevent::HasPeerAddress() const { | 328 bool SocketLibevent::HasPeerAddress() const { |
322 DCHECK(thread_checker_.CalledOnValidThread()); | 329 DCHECK(thread_checker_.CalledOnValidThread()); |
323 return peer_address_ != NULL; | 330 return peer_address_ != NULL; |
324 } | 331 } |
325 | 332 |
326 void SocketLibevent::Close() { | 333 void SocketLibevent::Close() { |
327 DCHECK(thread_checker_.CalledOnValidThread()); | 334 DCHECK(thread_checker_.CalledOnValidThread()); |
328 | 335 |
329 bool ok = accept_socket_watcher_.StopWatchingFileDescriptor(); | 336 StopWatchingAndCleanUp(); |
330 DCHECK(ok); | |
331 ok = read_socket_watcher_.StopWatchingFileDescriptor(); | |
332 DCHECK(ok); | |
333 ok = write_socket_watcher_.StopWatchingFileDescriptor(); | |
334 DCHECK(ok); | |
335 | 337 |
336 if (socket_fd_ != kInvalidSocket) { | 338 if (socket_fd_ != kInvalidSocket) { |
337 if (IGNORE_EINTR(close(socket_fd_)) < 0) | 339 if (IGNORE_EINTR(close(socket_fd_)) < 0) |
338 PLOG(ERROR) << "close() returned an error, errno=" << errno; | 340 PLOG(ERROR) << "close() returned an error, errno=" << errno; |
339 socket_fd_ = kInvalidSocket; | 341 socket_fd_ = kInvalidSocket; |
340 } | 342 } |
341 | |
342 if (!accept_callback_.is_null()) { | |
343 accept_socket_ = NULL; | |
344 accept_callback_.Reset(); | |
345 } | |
346 | |
347 if (!read_callback_.is_null()) { | |
348 read_buf_ = NULL; | |
349 read_buf_len_ = 0; | |
350 read_callback_.Reset(); | |
351 } | |
352 | |
353 if (!write_callback_.is_null()) { | |
354 write_buf_ = NULL; | |
355 write_buf_len_ = 0; | |
356 write_callback_.Reset(); | |
357 } | |
358 | |
359 waiting_connect_ = false; | |
360 peer_address_.reset(); | |
361 } | 343 } |
362 | 344 |
363 void SocketLibevent::OnFileCanReadWithoutBlocking(int fd) { | 345 void SocketLibevent::OnFileCanReadWithoutBlocking(int fd) { |
364 DCHECK(!accept_callback_.is_null() || !read_callback_.is_null()); | 346 DCHECK(!accept_callback_.is_null() || !read_callback_.is_null()); |
365 if (!accept_callback_.is_null()) { | 347 if (!accept_callback_.is_null()) { |
366 AcceptCompleted(); | 348 AcceptCompleted(); |
367 } else { // !read_callback_.is_null() | 349 } else { // !read_callback_.is_null() |
368 ReadCompleted(); | 350 ReadCompleted(); |
369 } | 351 } |
370 } | 352 } |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 if (rv == ERR_IO_PENDING) | 443 if (rv == ERR_IO_PENDING) |
462 return; | 444 return; |
463 | 445 |
464 bool ok = write_socket_watcher_.StopWatchingFileDescriptor(); | 446 bool ok = write_socket_watcher_.StopWatchingFileDescriptor(); |
465 DCHECK(ok); | 447 DCHECK(ok); |
466 write_buf_ = NULL; | 448 write_buf_ = NULL; |
467 write_buf_len_ = 0; | 449 write_buf_len_ = 0; |
468 base::ResetAndReturn(&write_callback_).Run(rv); | 450 base::ResetAndReturn(&write_callback_).Run(rv); |
469 } | 451 } |
470 | 452 |
| 453 void SocketLibevent::StopWatchingAndCleanUp() { |
| 454 bool ok = accept_socket_watcher_.StopWatchingFileDescriptor(); |
| 455 DCHECK(ok); |
| 456 ok = read_socket_watcher_.StopWatchingFileDescriptor(); |
| 457 DCHECK(ok); |
| 458 ok = write_socket_watcher_.StopWatchingFileDescriptor(); |
| 459 DCHECK(ok); |
| 460 |
| 461 if (!accept_callback_.is_null()) { |
| 462 accept_socket_ = NULL; |
| 463 accept_callback_.Reset(); |
| 464 } |
| 465 |
| 466 if (!read_callback_.is_null()) { |
| 467 read_buf_ = NULL; |
| 468 read_buf_len_ = 0; |
| 469 read_callback_.Reset(); |
| 470 } |
| 471 |
| 472 if (!write_callback_.is_null()) { |
| 473 write_buf_ = NULL; |
| 474 write_buf_len_ = 0; |
| 475 write_callback_.Reset(); |
| 476 } |
| 477 |
| 478 waiting_connect_ = false; |
| 479 peer_address_.reset(); |
| 480 } |
| 481 |
471 } // namespace net | 482 } // namespace net |
OLD | NEW |