Index: net/socket/tcp_server_socket.cc |
diff --git a/net/socket/tcp_server_socket.cc b/net/socket/tcp_server_socket.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..fe05cf94370666d2aa049118adce635e884a24fd |
--- /dev/null |
+++ b/net/socket/tcp_server_socket.cc |
@@ -0,0 +1,107 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "net/socket/tcp_server_socket.h" |
+ |
+#include "base/bind.h" |
+#include "base/bind_helpers.h" |
+#include "base/logging.h" |
+#include "build/build_config.h" |
+#include "net/base/ip_endpoint.h" |
+#include "net/base/net_errors.h" |
+#include "net/socket/tcp_client_socket.h" |
+ |
+namespace net { |
+ |
+TCPServerSocket::TCPServerSocket(NetLog* net_log, const NetLog::Source& source) |
+ : socket_(net_log, source) { |
+} |
+ |
+TCPServerSocket::~TCPServerSocket() { |
+} |
+ |
+int TCPServerSocket::Listen(const IPEndPoint& address, int backlog) { |
+ int result = socket_.Create(address.GetFamily()); |
+ if (result != OK) |
+ return result; |
+ |
+#if defined(WIN) |
+ result = socket_.SetExclusiveAddrUse(); |
+#elif defined(POSIX) |
+ result = socket_.SetAddressReuse(true); |
+#endif |
+ if (result != OK || |
+ (result = socket_.Bind(address)) != OK || |
+ (result = socket_.Listen(backlog)) != OK) { |
+ socket_.Close(); |
+ return result; |
+ } |
+ |
+ return OK; |
+} |
+ |
+int TCPServerSocket::GetLocalAddress(IPEndPoint* address) const { |
+ return socket_.GetLocalAddress(address); |
+} |
+ |
+int TCPServerSocket::Accept(scoped_ptr<StreamSocket>* socket, |
+ const CompletionCallback& callback) { |
+ DCHECK(socket); |
+ DCHECK(!callback.is_null()); |
+ |
+ scoped_ptr<TCPSocket>* tcp_socket = new scoped_ptr<TCPSocket>(); |
+ IPEndPoint* address = new IPEndPoint(); |
+ // It is safe to use base::Unretained(this). |socket_| is owned by this class, |
+ // and the callback won't be run after |socket_| is destroyed. |
+ CompletionCallback accept_callback = base::Bind( |
akalin
2013/08/26 18:53:21
this call is really awkward. :/ Would it help if t
yzshen1
2013/08/26 23:09:57
I changed them to members. Thanks!
On 2013/08/26
|
+ base::Bind(&TCPServerSocket::OnAcceptCompleted, base::Unretained(this), |
+ base::Owned(tcp_socket), base::Owned(address), socket, callback)); |
+ int result = socket_.Accept(tcp_socket, address, accept_callback); |
+ if (result != ERR_IO_PENDING) { |
+ // |accept_callback| won't be called so we need to run OnAcceptCompleted() |
+ // ourselves in order to do the conversion from |tcp_socket| to |socket|. We |
+ // don't want to run |callback| in this case, so we use a null |
+ // CompletionCallback. |
+ // |
+ // |tcp_socket| and |address| are owned by |accept_callback|. They must |
+ // still be alive at this point. |
+ OnAcceptCompleted(tcp_socket, address, socket, CompletionCallback(), |
+ result); |
+ } |
+ |
+ return result; |
+} |
+ |
+void TCPServerSocket::OnAcceptCompleted( |
+ scoped_ptr<TCPSocket>* tcp_socket, |
+ IPEndPoint* address, |
+ scoped_ptr<StreamSocket>* socket, |
+ const CompletionCallback& forward_callback, |
+ int result) { |
+ do { |
+ if (result != OK) |
+ break; |
+ |
+ scoped_ptr<TCPClientSocket> client_socket(new TCPClientSocket( |
+ AddressList(*address), (*tcp_socket)->net_log().net_log(), |
+ (*tcp_socket)->net_log().source())); |
+ // TODO(yzshen): Once we switch TCPClientSocket::AdoptSocket() to take a |
+ // TCPSocket object, we don't need to do platform-specific handling. |
+#if defined(OS_WIN) |
+ SOCKET raw_socket = (*tcp_socket)->Release(); |
+#elif defined(OS_POSIX) |
+ int raw_socket = (*tcp_socket)->Release(); |
+#endif |
+ result = client_socket->AdoptSocket(raw_socket); |
+ if (result != OK) |
+ break; |
+ |
+ socket->reset(client_socket.release()); |
akalin
2013/08/26 18:53:21
*socket = client_socket.Pass();
You might have to
yzshen1
2013/08/26 23:09:57
Done.
|
+ } while (false); |
+ |
+ if (!forward_callback.is_null()) |
+ forward_callback.Run(result); |
+} |
+ |
+} // namespace net |