Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/socket/tcp_server_socket.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "build/build_config.h" | |
| 11 #include "net/base/net_errors.h" | |
| 12 #include "net/socket/tcp_client_socket.h" | |
| 13 | |
| 14 namespace net { | |
| 15 | |
| 16 TCPServerSocket::TCPServerSocket(NetLog* net_log, const NetLog::Source& source) | |
| 17 : socket_(net_log, source), | |
| 18 pending_accept_(false) { | |
| 19 } | |
| 20 | |
| 21 TCPServerSocket::~TCPServerSocket() { | |
| 22 } | |
| 23 | |
| 24 int TCPServerSocket::Listen(const IPEndPoint& address, int backlog) { | |
| 25 int result = socket_.Create(address.GetFamily()); | |
| 26 if (result != OK) | |
| 27 return result; | |
| 28 | |
| 29 result = socket_.SetDefaultOptionsForServer(); | |
| 30 if (result != OK) { | |
| 31 socket_.Close(); | |
| 32 return result; | |
| 33 } | |
| 34 | |
| 35 result = socket_.Bind(address); | |
| 36 if (result != OK) { | |
| 37 socket_.Close(); | |
| 38 return result; | |
| 39 } | |
| 40 | |
| 41 result = socket_.Listen(backlog); | |
| 42 if (result != OK) { | |
| 43 socket_.Close(); | |
| 44 return result; | |
| 45 } | |
| 46 | |
| 47 return OK; | |
| 48 } | |
| 49 | |
| 50 int TCPServerSocket::GetLocalAddress(IPEndPoint* address) const { | |
| 51 return socket_.GetLocalAddress(address); | |
| 52 } | |
| 53 | |
| 54 int TCPServerSocket::Accept(scoped_ptr<StreamSocket>* socket, | |
| 55 const CompletionCallback& callback) { | |
| 56 DCHECK(socket); | |
| 57 DCHECK(!callback.is_null()); | |
| 58 DCHECK(!pending_accept_); | |
|
akalin
2013/08/29 22:08:33
would prefer:
if (pending_accept_) {
NOTREACHED
yzshen1
2013/08/29 23:04:18
I thought this is more consistent with the existin
akalin
2013/08/29 23:33:26
You know that NOTREACHED() is equivalent to DCHECK
| |
| 59 | |
| 60 | |
| 61 // It is safe to use base::Unretained(this). |socket_| is owned by this class, | |
| 62 // and the callback won't be run after |socket_| is destroyed. | |
| 63 CompletionCallback accept_callback = | |
| 64 base::Bind(&TCPServerSocket::OnAcceptCompleted, base::Unretained(this), | |
| 65 socket, callback); | |
| 66 int result = socket_.Accept(&accepted_socket_, &accepted_address_, | |
| 67 accept_callback); | |
| 68 if (result != ERR_IO_PENDING) { | |
| 69 // |accept_callback| won't be called so we need to run | |
| 70 // ConvertAcceptedSocket() ourselves in order to do the conversion from | |
| 71 // |accepted_socket_| to |socket|. | |
| 72 result = ConvertAcceptedSocket(result, socket); | |
| 73 } else { | |
| 74 pending_accept_ = true; | |
| 75 } | |
| 76 | |
| 77 return result; | |
| 78 } | |
| 79 | |
| 80 int TCPServerSocket::ConvertAcceptedSocket( | |
| 81 int result, | |
| 82 scoped_ptr<StreamSocket>* output_accepted_socket) { | |
| 83 // Make sure the TCPSocket object is destructed in any case. | |
|
akalin
2013/08/29 22:08:33
destructed -> destroyed
yzshen1
2013/08/29 23:04:18
Done.
| |
| 84 scoped_ptr<TCPSocket> temp_accepted_socket(accepted_socket_.Pass()); | |
| 85 if (result != OK) | |
| 86 return result; | |
| 87 | |
| 88 scoped_ptr<TCPClientSocket> client_socket(new TCPClientSocket( | |
| 89 AddressList(accepted_address_), | |
| 90 temp_accepted_socket->net_log().net_log(), | |
| 91 temp_accepted_socket->net_log().source())); | |
| 92 // TODO(yzshen): Once we switch TCPClientSocket::AdoptSocket() to take a | |
| 93 // TCPSocket object, we don't need to do platform-specific handling. | |
| 94 #if defined(OS_WIN) | |
| 95 SOCKET raw_socket = temp_accepted_socket->Release(); | |
| 96 #elif defined(OS_POSIX) | |
| 97 int raw_socket = temp_accepted_socket->Release(); | |
| 98 #endif | |
| 99 result = client_socket->AdoptSocket(raw_socket); | |
| 100 if (result != OK) { | |
| 101 // |client_socket| won't take ownership of |raw_socket| on failure. | |
| 102 // Therefore, we put it back into |temp_accepted_socket| to close it. | |
| 103 temp_accepted_socket->Adopt(raw_socket); | |
| 104 return result; | |
| 105 } | |
| 106 | |
| 107 *output_accepted_socket = client_socket.Pass(); | |
| 108 return OK; | |
| 109 } | |
| 110 | |
| 111 void TCPServerSocket::OnAcceptCompleted( | |
| 112 scoped_ptr<StreamSocket>* output_accepted_socket, | |
| 113 const CompletionCallback& forward_callback, | |
| 114 int result) { | |
| 115 result = ConvertAcceptedSocket(result, output_accepted_socket); | |
| 116 pending_accept_ = false; | |
| 117 forward_callback.Run(result); | |
| 118 } | |
| 119 | |
| 120 } // namespace net | |
| OLD | NEW |