OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "mojo/services/network/tcp_bound_socket_impl.h" | 5 #include "mojo/services/network/tcp_bound_socket_impl.h" |
6 | 6 |
| 7 #include <utility> |
| 8 |
7 #include "mojo/services/network/net_adapters.h" | 9 #include "mojo/services/network/net_adapters.h" |
8 #include "mojo/services/network/net_address_type_converters.h" | 10 #include "mojo/services/network/net_address_type_converters.h" |
9 #include "mojo/services/network/tcp_connected_socket_impl.h" | 11 #include "mojo/services/network/tcp_connected_socket_impl.h" |
10 #include "mojo/services/network/tcp_server_socket_impl.h" | 12 #include "mojo/services/network/tcp_server_socket_impl.h" |
11 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
12 | 14 |
13 namespace mojo { | 15 namespace mojo { |
14 | 16 |
15 TCPBoundSocketImpl::TCPBoundSocketImpl( | 17 TCPBoundSocketImpl::TCPBoundSocketImpl( |
16 scoped_ptr<mojo::AppRefCount> app_refcount, | 18 scoped_ptr<mojo::AppRefCount> app_refcount, |
17 InterfaceRequest<TCPBoundSocket> request) | 19 InterfaceRequest<TCPBoundSocket> request) |
18 : app_refcount_(app_refcount.Pass()), binding_(this, request.Pass()) { | 20 : app_refcount_(std::move(app_refcount)), |
19 } | 21 binding_(this, std::move(request)) {} |
20 | 22 |
21 TCPBoundSocketImpl::~TCPBoundSocketImpl() { | 23 TCPBoundSocketImpl::~TCPBoundSocketImpl() { |
22 } | 24 } |
23 | 25 |
24 int TCPBoundSocketImpl::Bind(NetAddressPtr local_address) { | 26 int TCPBoundSocketImpl::Bind(NetAddressPtr local_address) { |
25 net::IPEndPoint end_point = local_address.To<net::IPEndPoint>(); | 27 net::IPEndPoint end_point = local_address.To<net::IPEndPoint>(); |
26 | 28 |
27 socket_.reset(new net::TCPSocket(NULL, net::NetLog::Source())); | 29 socket_.reset(new net::TCPSocket(NULL, net::NetLog::Source())); |
28 int result = socket_->Open(end_point.GetFamily()); | 30 int result = socket_->Open(end_point.GetFamily()); |
29 if (result != net::OK) | 31 if (result != net::OK) |
(...skipping 29 matching lines...) Expand all Loading... |
59 } | 61 } |
60 | 62 |
61 // TODO(brettw) set the backlog properly. | 63 // TODO(brettw) set the backlog properly. |
62 int result = socket_->Listen(4); | 64 int result = socket_->Listen(4); |
63 if (result != net::OK) { | 65 if (result != net::OK) { |
64 callback.Run(MakeNetworkError(result)); | 66 callback.Run(MakeNetworkError(result)); |
65 return; | 67 return; |
66 } | 68 } |
67 | 69 |
68 // The server socket object takes ownership of the socket. | 70 // The server socket object takes ownership of the socket. |
69 new TCPServerSocketImpl(socket_.Pass(), app_refcount_->Clone(), | 71 new TCPServerSocketImpl(std::move(socket_), app_refcount_->Clone(), |
70 server.Pass()); | 72 std::move(server)); |
71 callback.Run(MakeNetworkError(net::OK)); | 73 callback.Run(MakeNetworkError(net::OK)); |
72 } | 74 } |
73 | 75 |
74 void TCPBoundSocketImpl::Connect( | 76 void TCPBoundSocketImpl::Connect( |
75 NetAddressPtr remote_address, | 77 NetAddressPtr remote_address, |
76 ScopedDataPipeConsumerHandle send_stream, | 78 ScopedDataPipeConsumerHandle send_stream, |
77 ScopedDataPipeProducerHandle receive_stream, | 79 ScopedDataPipeProducerHandle receive_stream, |
78 InterfaceRequest<TCPConnectedSocket> client_socket, | 80 InterfaceRequest<TCPConnectedSocket> client_socket, |
79 const Callback<void(NetworkErrorPtr)>& callback) { | 81 const Callback<void(NetworkErrorPtr)>& callback) { |
80 if (!socket_ || pending_connect_socket_.is_pending()) { | 82 if (!socket_ || pending_connect_socket_.is_pending()) { |
81 // A bound socket will only be returned to the caller after binding | 83 // A bound socket will only be returned to the caller after binding |
82 // succeeds, so if the socket doesn't exist, that means ownership was | 84 // succeeds, so if the socket doesn't exist, that means ownership was |
83 // already passed to a server socket or client socket. | 85 // already passed to a server socket or client socket. |
84 callback.Run(MakeNetworkError(net::ERR_FAILED)); | 86 callback.Run(MakeNetworkError(net::ERR_FAILED)); |
85 return; | 87 return; |
86 } | 88 } |
87 | 89 |
88 net::IPEndPoint end_point = remote_address.To<net::IPEndPoint>(); | 90 net::IPEndPoint end_point = remote_address.To<net::IPEndPoint>(); |
89 | 91 |
90 pending_connect_send_stream_ = send_stream.Pass(); | 92 pending_connect_send_stream_ = std::move(send_stream); |
91 pending_connect_receive_stream_ = receive_stream.Pass(); | 93 pending_connect_receive_stream_ = std::move(receive_stream); |
92 pending_connect_socket_ = client_socket.Pass(); | 94 pending_connect_socket_ = std::move(client_socket); |
93 pending_connect_callback_ = callback; | 95 pending_connect_callback_ = callback; |
94 int result = socket_->Connect( | 96 int result = socket_->Connect( |
95 end_point, | 97 end_point, |
96 base::Bind(&TCPBoundSocketImpl::OnConnected, base::Unretained(this))); | 98 base::Bind(&TCPBoundSocketImpl::OnConnected, base::Unretained(this))); |
97 if (result == net::OK) { | 99 if (result == net::OK) { |
98 OnConnected(result); | 100 OnConnected(result); |
99 } else if (result != net::ERR_IO_PENDING) { | 101 } else if (result != net::ERR_IO_PENDING) { |
100 // Error occurred. | 102 // Error occurred. |
101 pending_connect_send_stream_.reset(); | 103 pending_connect_send_stream_.reset(); |
102 pending_connect_receive_stream_.reset(); | 104 pending_connect_receive_stream_.reset(); |
103 pending_connect_socket_ = InterfaceRequest<TCPConnectedSocket>(); | 105 pending_connect_socket_ = InterfaceRequest<TCPConnectedSocket>(); |
104 pending_connect_callback_ = Callback<void(NetworkErrorPtr)>(); | 106 pending_connect_callback_ = Callback<void(NetworkErrorPtr)>(); |
105 callback.Run(MakeNetworkError(result)); | 107 callback.Run(MakeNetworkError(result)); |
106 } | 108 } |
107 } | 109 } |
108 | 110 |
109 void TCPBoundSocketImpl::OnConnected(int result) { | 111 void TCPBoundSocketImpl::OnConnected(int result) { |
110 if (result == net::OK) { | 112 if (result == net::OK) { |
111 new TCPConnectedSocketImpl( | 113 new TCPConnectedSocketImpl( |
112 socket_.Pass(), pending_connect_send_stream_.Pass(), | 114 std::move(socket_), std::move(pending_connect_send_stream_), |
113 pending_connect_receive_stream_.Pass(), pending_connect_socket_.Pass(), | 115 std::move(pending_connect_receive_stream_), |
114 app_refcount_->Clone()); | 116 std::move(pending_connect_socket_), app_refcount_->Clone()); |
115 } else { | 117 } else { |
116 pending_connect_send_stream_.reset(); | 118 pending_connect_send_stream_.reset(); |
117 pending_connect_receive_stream_.reset(); | 119 pending_connect_receive_stream_.reset(); |
118 pending_connect_socket_ = InterfaceRequest<TCPConnectedSocket>(); | 120 pending_connect_socket_ = InterfaceRequest<TCPConnectedSocket>(); |
119 } | 121 } |
120 pending_connect_callback_.Run(MakeNetworkError(result)); | 122 pending_connect_callback_.Run(MakeNetworkError(result)); |
121 pending_connect_callback_ = Callback<void(NetworkErrorPtr)>(); | 123 pending_connect_callback_ = Callback<void(NetworkErrorPtr)>(); |
122 } | 124 } |
123 | 125 |
124 } // namespace mojo | 126 } // namespace mojo |
OLD | NEW |