OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/tcp_server_socket.h" | 5 #include "net/socket/tcp_server_socket.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
12 #include "net/socket/tcp_client_socket.h" | 12 #include "net/socket/tcp_client_socket.h" |
13 | 13 |
14 namespace net { | 14 namespace net { |
15 | 15 |
16 TCPServerSocket::TCPServerSocket(NetLog* net_log, const NetLog::Source& source) | 16 TCPServerSocket::TCPServerSocket(NetLog* net_log, const NetLog::Source& source) |
17 : socket_(net_log, source), | 17 : socket_(net_log, source), |
18 pending_accept_(false) { | 18 pending_accept_(false) { |
19 } | 19 } |
20 | 20 |
21 TCPServerSocket::~TCPServerSocket() { | 21 TCPServerSocket::~TCPServerSocket() { |
22 } | 22 } |
23 | 23 |
24 int TCPServerSocket::Listen(const IPEndPoint& address, int backlog) { | 24 int TCPServerSocket::Listen(const IPEndPoint& address, int backlog) { |
25 int result = socket_.Create(address.GetFamily()); | 25 int result = socket_.Open(address.GetFamily()); |
26 if (result != OK) | 26 if (result != OK) |
27 return result; | 27 return result; |
28 | 28 |
29 result = socket_.SetDefaultOptionsForServer(); | 29 result = socket_.SetDefaultOptionsForServer(); |
30 if (result != OK) { | 30 if (result != OK) { |
31 socket_.Close(); | 31 socket_.Close(); |
32 return result; | 32 return result; |
33 } | 33 } |
34 | 34 |
35 result = socket_.Bind(address); | 35 result = socket_.Bind(address); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 } | 81 } |
82 | 82 |
83 int TCPServerSocket::ConvertAcceptedSocket( | 83 int TCPServerSocket::ConvertAcceptedSocket( |
84 int result, | 84 int result, |
85 scoped_ptr<StreamSocket>* output_accepted_socket) { | 85 scoped_ptr<StreamSocket>* output_accepted_socket) { |
86 // Make sure the TCPSocket object is destroyed in any case. | 86 // Make sure the TCPSocket object is destroyed in any case. |
87 scoped_ptr<TCPSocket> temp_accepted_socket(accepted_socket_.Pass()); | 87 scoped_ptr<TCPSocket> temp_accepted_socket(accepted_socket_.Pass()); |
88 if (result != OK) | 88 if (result != OK) |
89 return result; | 89 return result; |
90 | 90 |
| 91 // TODO(yzshen): Once we switch TCPClientSocketLibevent to take a connected |
| 92 // TCPSocket object, we don't need to do platform-specific handling. |
| 93 #if defined(OS_WIN) |
| 94 scoped_ptr<TCPClientSocket> client_socket(new TCPClientSocket( |
| 95 temp_accepted_socket.Pass(), accepted_address_)); |
| 96 #elif defined(OS_POSIX) |
91 scoped_ptr<TCPClientSocket> client_socket(new TCPClientSocket( | 97 scoped_ptr<TCPClientSocket> client_socket(new TCPClientSocket( |
92 AddressList(accepted_address_), | 98 AddressList(accepted_address_), |
93 temp_accepted_socket->net_log().net_log(), | 99 temp_accepted_socket->net_log().net_log(), |
94 temp_accepted_socket->net_log().source())); | 100 temp_accepted_socket->net_log().source())); |
95 // TODO(yzshen): Once we switch TCPClientSocket::AdoptSocket() to take a | |
96 // TCPSocket object, we don't need to do platform-specific handling. | |
97 #if defined(OS_WIN) | |
98 SOCKET raw_socket = temp_accepted_socket->Release(); | |
99 #elif defined(OS_POSIX) | |
100 int raw_socket = temp_accepted_socket->Release(); | 101 int raw_socket = temp_accepted_socket->Release(); |
101 #endif | |
102 result = client_socket->AdoptSocket(raw_socket); | 102 result = client_socket->AdoptSocket(raw_socket); |
103 if (result != OK) { | 103 if (result != OK) { |
104 // |client_socket| won't take ownership of |raw_socket| on failure. | 104 // |client_socket| won't take ownership of |raw_socket| on failure. |
105 // Therefore, we put it back into |temp_accepted_socket| to close it. | 105 // Therefore, we put it back into |temp_accepted_socket| to close it. |
106 temp_accepted_socket->Adopt(raw_socket); | 106 temp_accepted_socket->Adopt(raw_socket); |
107 return result; | 107 return result; |
108 } | 108 } |
| 109 #endif |
109 | 110 |
110 *output_accepted_socket = client_socket.Pass(); | 111 *output_accepted_socket = client_socket.Pass(); |
111 return OK; | 112 return OK; |
112 } | 113 } |
113 | 114 |
114 void TCPServerSocket::OnAcceptCompleted( | 115 void TCPServerSocket::OnAcceptCompleted( |
115 scoped_ptr<StreamSocket>* output_accepted_socket, | 116 scoped_ptr<StreamSocket>* output_accepted_socket, |
116 const CompletionCallback& forward_callback, | 117 const CompletionCallback& forward_callback, |
117 int result) { | 118 int result) { |
118 result = ConvertAcceptedSocket(result, output_accepted_socket); | 119 result = ConvertAcceptedSocket(result, output_accepted_socket); |
119 pending_accept_ = false; | 120 pending_accept_ = false; |
120 forward_callback.Run(result); | 121 forward_callback.Run(result); |
121 } | 122 } |
122 | 123 |
123 } // namespace net | 124 } // namespace net |
OLD | NEW |