OLD | NEW |
| (Empty) |
1 // Copyright 2014 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 "mojo/services/network/tcp_server_socket_impl.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "mojo/services/network/net_adapters.h" | |
10 #include "mojo/services/network/net_address_type_converters.h" | |
11 #include "mojo/services/network/tcp_connected_socket_impl.h" | |
12 #include "net/base/net_errors.h" | |
13 | |
14 namespace mojo { | |
15 | |
16 TCPServerSocketImpl::TCPServerSocketImpl( | |
17 scoped_ptr<net::TCPSocket> socket, | |
18 scoped_ptr<mojo::MessageLoopRef> app_refcount, | |
19 InterfaceRequest<TCPServerSocket> request) | |
20 : socket_(std::move(socket)), | |
21 app_refcount_(std::move(app_refcount)), | |
22 binding_(this, std::move(request)) {} | |
23 | |
24 TCPServerSocketImpl::~TCPServerSocketImpl() { | |
25 } | |
26 | |
27 void TCPServerSocketImpl::Accept( | |
28 ScopedDataPipeConsumerHandle send_stream, | |
29 ScopedDataPipeProducerHandle receive_stream, | |
30 InterfaceRequest<TCPConnectedSocket> client_socket, | |
31 const AcceptCallback& callback) { | |
32 // One possible future enhancement would be to enqueue multiple Accept calls | |
33 // on this object. This would allow the client to accept some number of | |
34 // incoming connections rapidly without doing an IPC round-trip. | |
35 if (!pending_callback_.is_null()) { | |
36 // Already have a pending accept on this socket. | |
37 callback.Run(MakeNetworkError(net::ERR_UNEXPECTED), NetAddressPtr()); | |
38 return; | |
39 } | |
40 | |
41 int result = socket_->Accept( | |
42 &accepted_socket_, &accepted_address_, | |
43 base::Bind(&TCPServerSocketImpl::OnAcceptCompleted, | |
44 base::Unretained(this))); | |
45 if (result == net::OK || result == net::ERR_IO_PENDING) { | |
46 pending_callback_ = callback; | |
47 pending_send_stream_ = std::move(send_stream); | |
48 pending_receive_stream_ = std::move(receive_stream); | |
49 pending_client_socket_ = std::move(client_socket); | |
50 if (result == net::OK) | |
51 OnAcceptCompleted(net::OK); | |
52 } else { | |
53 callback.Run(MakeNetworkError(result), NetAddressPtr()); | |
54 } | |
55 } | |
56 | |
57 void TCPServerSocketImpl::OnAcceptCompleted(int result) { | |
58 if (result != net::OK) { | |
59 pending_callback_.Run(MakeNetworkError(result), NetAddressPtr()); | |
60 pending_send_stream_.reset(); | |
61 pending_receive_stream_.reset(); | |
62 pending_client_socket_ = InterfaceRequest<TCPConnectedSocket>(); | |
63 } else { | |
64 new TCPConnectedSocketImpl( | |
65 std::move(accepted_socket_), std::move(pending_send_stream_), | |
66 std::move(pending_receive_stream_), std::move(pending_client_socket_), | |
67 app_refcount_->Clone()); | |
68 pending_callback_.Run(MakeNetworkError(net::OK), | |
69 NetAddress::From(accepted_address_)); | |
70 } | |
71 | |
72 pending_callback_ = AcceptCallback(); | |
73 } | |
74 | |
75 } // namespace mojo | |
OLD | NEW |