Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(356)

Side by Side Diff: net/socket/tcp_server_socket.cc

Issue 22861033: Move server socket functionality from TCPServerSocket into TCPSocket. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/ip_endpoint.h"
12 #include "net/base/net_errors.h"
13 #include "net/socket/tcp_client_socket.h"
14
15 namespace net {
16
17 TCPServerSocket::TCPServerSocket(NetLog* net_log, const NetLog::Source& source)
18 : socket_(net_log, source) {
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 #if defined(WIN)
30 result = socket_.SetExclusiveAddrUse();
31 #elif defined(POSIX)
32 result = socket_.SetAddressReuse(true);
33 #endif
34 if (result != OK ||
35 (result = socket_.Bind(address)) != OK ||
36 (result = socket_.Listen(backlog)) != OK) {
37 socket_.Close();
38 return result;
39 }
40
41 return OK;
42 }
43
44 int TCPServerSocket::GetLocalAddress(IPEndPoint* address) const {
45 return socket_.GetLocalAddress(address);
46 }
47
48 int TCPServerSocket::Accept(scoped_ptr<StreamSocket>* socket,
49 const CompletionCallback& callback) {
50 DCHECK(socket);
51 DCHECK(!callback.is_null());
52
53 scoped_ptr<TCPSocket>* tcp_socket = new scoped_ptr<TCPSocket>();
54 IPEndPoint* address = new IPEndPoint();
55 // It is safe to use base::Unretained(this). |socket_| is owned by this class,
56 // and the callback won't be run after |socket_| is destroyed.
57 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
58 base::Bind(&TCPServerSocket::OnAcceptCompleted, base::Unretained(this),
59 base::Owned(tcp_socket), base::Owned(address), socket, callback));
60 int result = socket_.Accept(tcp_socket, address, accept_callback);
61 if (result != ERR_IO_PENDING) {
62 // |accept_callback| won't be called so we need to run OnAcceptCompleted()
63 // ourselves in order to do the conversion from |tcp_socket| to |socket|. We
64 // don't want to run |callback| in this case, so we use a null
65 // CompletionCallback.
66 //
67 // |tcp_socket| and |address| are owned by |accept_callback|. They must
68 // still be alive at this point.
69 OnAcceptCompleted(tcp_socket, address, socket, CompletionCallback(),
70 result);
71 }
72
73 return result;
74 }
75
76 void TCPServerSocket::OnAcceptCompleted(
77 scoped_ptr<TCPSocket>* tcp_socket,
78 IPEndPoint* address,
79 scoped_ptr<StreamSocket>* socket,
80 const CompletionCallback& forward_callback,
81 int result) {
82 do {
83 if (result != OK)
84 break;
85
86 scoped_ptr<TCPClientSocket> client_socket(new TCPClientSocket(
87 AddressList(*address), (*tcp_socket)->net_log().net_log(),
88 (*tcp_socket)->net_log().source()));
89 // TODO(yzshen): Once we switch TCPClientSocket::AdoptSocket() to take a
90 // TCPSocket object, we don't need to do platform-specific handling.
91 #if defined(OS_WIN)
92 SOCKET raw_socket = (*tcp_socket)->Release();
93 #elif defined(OS_POSIX)
94 int raw_socket = (*tcp_socket)->Release();
95 #endif
96 result = client_socket->AdoptSocket(raw_socket);
97 if (result != OK)
98 break;
99
100 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.
101 } while (false);
102
103 if (!forward_callback.is_null())
104 forward_callback.Run(result);
105 }
106
107 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698