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 "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
11 #include "net/socket/tcp_client_socket.h" | 11 #include "net/socket/tcp_client_socket.h" |
12 | 12 |
13 namespace net { | 13 namespace net { |
14 | 14 |
15 TCPServerSocket::TCPServerSocket(NetLog* net_log, const NetLog::Source& source) | 15 TCPServerSocket::TCPServerSocket(NetLog* net_log, const NetLog::Source& source) |
16 : socket_(net_log, source), | 16 : socket_(net_log, source), pending_accept_(false) { |
17 pending_accept_(false) { | |
18 } | 17 } |
19 | 18 |
20 TCPServerSocket::~TCPServerSocket() { | 19 TCPServerSocket::~TCPServerSocket() { |
21 } | 20 } |
22 | 21 |
23 int TCPServerSocket::Listen(const IPEndPoint& address, int backlog) { | 22 int TCPServerSocket::Listen(const IPEndPoint& address, int backlog) { |
24 int result = socket_.Open(address.GetFamily()); | 23 int result = socket_.Open(address.GetFamily()); |
25 if (result != OK) | 24 if (result != OK) |
26 return result; | 25 return result; |
27 | 26 |
(...skipping 28 matching lines...) Expand all Loading... |
56 DCHECK(!callback.is_null()); | 55 DCHECK(!callback.is_null()); |
57 | 56 |
58 if (pending_accept_) { | 57 if (pending_accept_) { |
59 NOTREACHED(); | 58 NOTREACHED(); |
60 return ERR_UNEXPECTED; | 59 return ERR_UNEXPECTED; |
61 } | 60 } |
62 | 61 |
63 // It is safe to use base::Unretained(this). |socket_| is owned by this class, | 62 // It is safe to use base::Unretained(this). |socket_| is owned by this class, |
64 // and the callback won't be run after |socket_| is destroyed. | 63 // and the callback won't be run after |socket_| is destroyed. |
65 CompletionCallback accept_callback = | 64 CompletionCallback accept_callback = |
66 base::Bind(&TCPServerSocket::OnAcceptCompleted, base::Unretained(this), | 65 base::Bind(&TCPServerSocket::OnAcceptCompleted, |
67 socket, callback); | 66 base::Unretained(this), |
68 int result = socket_.Accept(&accepted_socket_, &accepted_address_, | 67 socket, |
69 accept_callback); | 68 callback); |
| 69 int result = |
| 70 socket_.Accept(&accepted_socket_, &accepted_address_, accept_callback); |
70 if (result != ERR_IO_PENDING) { | 71 if (result != ERR_IO_PENDING) { |
71 // |accept_callback| won't be called so we need to run | 72 // |accept_callback| won't be called so we need to run |
72 // ConvertAcceptedSocket() ourselves in order to do the conversion from | 73 // ConvertAcceptedSocket() ourselves in order to do the conversion from |
73 // |accepted_socket_| to |socket|. | 74 // |accepted_socket_| to |socket|. |
74 result = ConvertAcceptedSocket(result, socket); | 75 result = ConvertAcceptedSocket(result, socket); |
75 } else { | 76 } else { |
76 pending_accept_ = true; | 77 pending_accept_ = true; |
77 } | 78 } |
78 | 79 |
79 return result; | 80 return result; |
80 } | 81 } |
81 | 82 |
82 int TCPServerSocket::ConvertAcceptedSocket( | 83 int TCPServerSocket::ConvertAcceptedSocket( |
83 int result, | 84 int result, |
84 scoped_ptr<StreamSocket>* output_accepted_socket) { | 85 scoped_ptr<StreamSocket>* output_accepted_socket) { |
85 // Make sure the TCPSocket object is destroyed in any case. | 86 // Make sure the TCPSocket object is destroyed in any case. |
86 scoped_ptr<TCPSocket> temp_accepted_socket(accepted_socket_.Pass()); | 87 scoped_ptr<TCPSocket> temp_accepted_socket(accepted_socket_.Pass()); |
87 if (result != OK) | 88 if (result != OK) |
88 return result; | 89 return result; |
89 | 90 |
90 output_accepted_socket->reset(new TCPClientSocket( | 91 output_accepted_socket->reset( |
91 temp_accepted_socket.Pass(), accepted_address_)); | 92 new TCPClientSocket(temp_accepted_socket.Pass(), accepted_address_)); |
92 | 93 |
93 return OK; | 94 return OK; |
94 } | 95 } |
95 | 96 |
96 void TCPServerSocket::OnAcceptCompleted( | 97 void TCPServerSocket::OnAcceptCompleted( |
97 scoped_ptr<StreamSocket>* output_accepted_socket, | 98 scoped_ptr<StreamSocket>* output_accepted_socket, |
98 const CompletionCallback& forward_callback, | 99 const CompletionCallback& forward_callback, |
99 int result) { | 100 int result) { |
100 result = ConvertAcceptedSocket(result, output_accepted_socket); | 101 result = ConvertAcceptedSocket(result, output_accepted_socket); |
101 pending_accept_ = false; | 102 pending_accept_ = false; |
102 forward_callback.Run(result); | 103 forward_callback.Run(result); |
103 } | 104 } |
104 | 105 |
105 } // namespace net | 106 } // namespace net |
OLD | NEW |