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 "mojo/services/network/net_adapters.h" | 7 #include "mojo/services/network/net_adapters.h" |
8 #include "mojo/services/network/net_address_type_converters.h" | 8 #include "mojo/services/network/net_address_type_converters.h" |
9 #include "mojo/services/network/tcp_connected_socket_impl.h" | 9 #include "mojo/services/network/tcp_connected_socket_impl.h" |
10 #include "mojo/services/network/tcp_server_socket_impl.h" | 10 #include "mojo/services/network/tcp_server_socket_impl.h" |
11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
12 | 12 |
13 namespace mojo { | 13 namespace mojo { |
14 | 14 |
15 TCPBoundSocketImpl::TCPBoundSocketImpl() { | 15 TCPBoundSocketImpl::TCPBoundSocketImpl( |
| 16 scoped_ptr<mojo::AppRefCount> app_refcount) |
| 17 : app_refcount_(app_refcount.Pass()) { |
16 } | 18 } |
17 | 19 |
18 TCPBoundSocketImpl::~TCPBoundSocketImpl() { | 20 TCPBoundSocketImpl::~TCPBoundSocketImpl() { |
19 } | 21 } |
20 | 22 |
21 int TCPBoundSocketImpl::Bind(NetAddressPtr local_address) { | 23 int TCPBoundSocketImpl::Bind(NetAddressPtr local_address) { |
22 net::IPEndPoint end_point = local_address.To<net::IPEndPoint>(); | 24 net::IPEndPoint end_point = local_address.To<net::IPEndPoint>(); |
23 | 25 |
24 socket_.reset(new net::TCPSocket(NULL, net::NetLog::Source())); | 26 socket_.reset(new net::TCPSocket(NULL, net::NetLog::Source())); |
25 int result = socket_->Open(end_point.GetFamily()); | 27 int result = socket_->Open(end_point.GetFamily()); |
(...skipping 30 matching lines...) Expand all Loading... |
56 } | 58 } |
57 | 59 |
58 // TODO(brettw) set the backlog properly. | 60 // TODO(brettw) set the backlog properly. |
59 int result = socket_->Listen(4); | 61 int result = socket_->Listen(4); |
60 if (result != net::OK) { | 62 if (result != net::OK) { |
61 callback.Run(MakeNetworkError(result)); | 63 callback.Run(MakeNetworkError(result)); |
62 return; | 64 return; |
63 } | 65 } |
64 | 66 |
65 // The server socket object takes ownership of the socket. | 67 // The server socket object takes ownership of the socket. |
66 BindToRequest(new TCPServerSocketImpl(socket_.Pass()), &server); | 68 BindToRequest( |
| 69 new TCPServerSocketImpl(socket_.Pass(), app_refcount_->Clone()), |
| 70 &server); |
67 callback.Run(MakeNetworkError(net::OK)); | 71 callback.Run(MakeNetworkError(net::OK)); |
68 } | 72 } |
69 | 73 |
70 void TCPBoundSocketImpl::Connect( | 74 void TCPBoundSocketImpl::Connect( |
71 NetAddressPtr remote_address, | 75 NetAddressPtr remote_address, |
72 ScopedDataPipeConsumerHandle send_stream, | 76 ScopedDataPipeConsumerHandle send_stream, |
73 ScopedDataPipeProducerHandle receive_stream, | 77 ScopedDataPipeProducerHandle receive_stream, |
74 InterfaceRequest<TCPConnectedSocket> client_socket, | 78 InterfaceRequest<TCPConnectedSocket> client_socket, |
75 const Callback<void(NetworkErrorPtr)>& callback) { | 79 const Callback<void(NetworkErrorPtr)>& callback) { |
76 if (!socket_ || pending_connect_socket_.is_pending()) { | 80 if (!socket_ || pending_connect_socket_.is_pending()) { |
(...skipping 22 matching lines...) Expand all Loading... |
99 pending_connect_socket_ = InterfaceRequest<TCPConnectedSocket>(); | 103 pending_connect_socket_ = InterfaceRequest<TCPConnectedSocket>(); |
100 pending_connect_callback_ = Callback<void(NetworkErrorPtr)>(); | 104 pending_connect_callback_ = Callback<void(NetworkErrorPtr)>(); |
101 callback.Run(MakeNetworkError(result)); | 105 callback.Run(MakeNetworkError(result)); |
102 } | 106 } |
103 } | 107 } |
104 | 108 |
105 void TCPBoundSocketImpl::OnConnected(int result) { | 109 void TCPBoundSocketImpl::OnConnected(int result) { |
106 if (result == net::OK) { | 110 if (result == net::OK) { |
107 new TCPConnectedSocketImpl( | 111 new TCPConnectedSocketImpl( |
108 socket_.Pass(), pending_connect_send_stream_.Pass(), | 112 socket_.Pass(), pending_connect_send_stream_.Pass(), |
109 pending_connect_receive_stream_.Pass(), pending_connect_socket_.Pass()); | 113 pending_connect_receive_stream_.Pass(), pending_connect_socket_.Pass(), |
| 114 app_refcount_->Clone()); |
110 } else { | 115 } else { |
111 pending_connect_send_stream_.reset(); | 116 pending_connect_send_stream_.reset(); |
112 pending_connect_receive_stream_.reset(); | 117 pending_connect_receive_stream_.reset(); |
113 pending_connect_socket_ = InterfaceRequest<TCPConnectedSocket>(); | 118 pending_connect_socket_ = InterfaceRequest<TCPConnectedSocket>(); |
114 } | 119 } |
115 pending_connect_callback_.Run(MakeNetworkError(result)); | 120 pending_connect_callback_.Run(MakeNetworkError(result)); |
116 pending_connect_callback_ = Callback<void(NetworkErrorPtr)>(); | 121 pending_connect_callback_ = Callback<void(NetworkErrorPtr)>(); |
117 } | 122 } |
118 | 123 |
119 } // namespace mojo | 124 } // namespace mojo |
OLD | NEW |