| 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 } | 16 } |
| 17 | 17 |
| 18 TCPBoundSocketImpl::~TCPBoundSocketImpl() { | 18 TCPBoundSocketImpl::~TCPBoundSocketImpl() { |
| 19 } | 19 } |
| 20 | 20 |
| 21 int TCPBoundSocketImpl::Bind(NetAddressPtr local_address) { | 21 int TCPBoundSocketImpl::Bind(NetAddressPtr local_address) { |
| 22 // The local address might be null to match any port. | 22 net::IPEndPoint end_point = local_address.To<net::IPEndPoint>(); |
| 23 net::IPEndPoint end_point; | |
| 24 if (!local_address.is_null()) | |
| 25 end_point = local_address.To<net::IPEndPoint>(); | |
| 26 | 23 |
| 27 socket_.reset(new net::TCPSocket(NULL, net::NetLog::Source())); | 24 socket_.reset(new net::TCPSocket(NULL, net::NetLog::Source())); |
| 28 int result = socket_->Open(end_point.GetFamily()); | 25 int result = socket_->Open(end_point.GetFamily()); |
| 29 if (result != net::OK) | 26 if (result != net::OK) |
| 30 return result; | 27 return result; |
| 31 | 28 |
| 32 result = socket_->SetDefaultOptionsForServer(); | 29 result = socket_->SetDefaultOptionsForServer(); |
| 33 if (result != net::OK) | 30 if (result != net::OK) |
| 34 return result; | 31 return result; |
| 35 | 32 |
| 36 result = socket_->Bind(end_point); | 33 result = socket_->Bind(end_point); |
| 37 if (result != net::OK) | 34 if (result != net::OK) |
| 38 return result; | 35 return result; |
| 39 | 36 |
| 40 return net::OK; | 37 return net::OK; |
| 41 } | 38 } |
| 42 | 39 |
| 43 NetAddressPtr TCPBoundSocketImpl::GetLocalAddress() const { | 40 NetAddressPtr TCPBoundSocketImpl::GetLocalAddress() const { |
| 44 net::IPEndPoint resolved_local_address; | 41 net::IPEndPoint resolved_local_address; |
| 45 if (socket_->GetLocalAddress(&resolved_local_address) != net::OK) | 42 if (socket_->GetLocalAddress(&resolved_local_address) != net::OK) |
| 46 return NetAddressPtr(); | 43 return NetAddressPtr(); |
| 47 return NetAddress::From(resolved_local_address); | 44 return NetAddress::From(resolved_local_address); |
| 48 } | 45 } |
| 49 | 46 |
| 50 void TCPBoundSocketImpl::StartListening( | 47 void TCPBoundSocketImpl::StartListening( |
| 51 InterfaceRequest<TCPServerSocket> server, | 48 InterfaceRequest<TCPServerSocket> server, |
| 52 const Callback<void(NetworkErrorPtr)>& callback) { | 49 const Callback<void(NetworkErrorPtr)>& callback) { |
| 53 if (!socket_) { | 50 if (!socket_ || pending_connect_socket_.is_pending()) { |
| 54 // A bound socket will only be returned to the caller after binding | 51 // A bound socket will only be returned to the caller after binding |
| 55 // succeeds, so if the socket doesn't exist, that means ownership was | 52 // succeeds, so if the socket doesn't exist, that means ownership was |
| 56 // already passed to a server socket or client socket. | 53 // already passed to a server socket or client socket. |
| 57 callback.Run(MakeNetworkError(net::ERR_FAILED)); | 54 callback.Run(MakeNetworkError(net::ERR_FAILED)); |
| 58 return; | 55 return; |
| 59 } | 56 } |
| 60 | 57 |
| 61 // TODO(brettw) set the backlog properly. | 58 // TODO(brettw) set the backlog properly. |
| 62 int result = socket_->Listen(4); | 59 int result = socket_->Listen(4); |
| 63 if (result != net::OK) { | 60 if (result != net::OK) { |
| 64 callback.Run(MakeNetworkError(result)); | 61 callback.Run(MakeNetworkError(result)); |
| 65 return; | 62 return; |
| 66 } | 63 } |
| 67 | 64 |
| 68 // The server socket object takes ownership of the socket. | 65 // The server socket object takes ownership of the socket. |
| 69 BindToRequest(new TCPServerSocketImpl(socket_.Pass()), &server); | 66 BindToRequest(new TCPServerSocketImpl(socket_.Pass()), &server); |
| 70 callback.Run(MakeNetworkError(net::OK)); | 67 callback.Run(MakeNetworkError(net::OK)); |
| 71 } | 68 } |
| 72 | 69 |
| 73 void TCPBoundSocketImpl::Connect( | 70 void TCPBoundSocketImpl::Connect( |
| 74 NetAddressPtr remote_address, | 71 NetAddressPtr remote_address, |
| 75 ScopedDataPipeConsumerHandle send_stream, | 72 ScopedDataPipeConsumerHandle send_stream, |
| 76 ScopedDataPipeProducerHandle receive_stream, | 73 ScopedDataPipeProducerHandle receive_stream, |
| 77 InterfaceRequest<TCPConnectedSocket> client_socket, | 74 InterfaceRequest<TCPConnectedSocket> client_socket, |
| 78 const Callback<void(NetworkErrorPtr)>& callback) { | 75 const Callback<void(NetworkErrorPtr)>& callback) { |
| 79 // TODO(brettw) write this. | 76 if (!socket_ || pending_connect_socket_.is_pending()) { |
| 77 // A bound socket will only be returned to the caller after binding |
| 78 // succeeds, so if the socket doesn't exist, that means ownership was |
| 79 // already passed to a server socket or client socket. |
| 80 callback.Run(MakeNetworkError(net::ERR_FAILED)); |
| 81 return; |
| 82 } |
| 83 |
| 84 net::IPEndPoint end_point = remote_address.To<net::IPEndPoint>(); |
| 85 |
| 86 pending_connect_send_stream_ = send_stream.Pass(); |
| 87 pending_connect_receive_stream_ = receive_stream.Pass(); |
| 88 pending_connect_socket_ = client_socket.Pass(); |
| 89 pending_connect_callback_ = callback; |
| 90 int result = socket_->Connect( |
| 91 end_point, |
| 92 base::Bind(&TCPBoundSocketImpl::OnConnected, base::Unretained(this))); |
| 93 if (result == net::OK) { |
| 94 OnConnected(result); |
| 95 } else if (result != net::ERR_IO_PENDING) { |
| 96 // Error occurred. |
| 97 pending_connect_send_stream_.reset(); |
| 98 pending_connect_receive_stream_.reset(); |
| 99 pending_connect_socket_ = InterfaceRequest<TCPConnectedSocket>(); |
| 100 pending_connect_callback_ = Callback<void(NetworkErrorPtr)>(); |
| 101 callback.Run(MakeNetworkError(result)); |
| 102 } |
| 103 } |
| 104 |
| 105 void TCPBoundSocketImpl::OnConnected(int result) { |
| 106 if (result == net::OK) { |
| 107 BindToRequest(new TCPConnectedSocketImpl( |
| 108 socket_.Pass(), |
| 109 pending_connect_send_stream_.Pass(), |
| 110 pending_connect_receive_stream_.Pass()), |
| 111 &pending_connect_socket_); |
| 112 } else { |
| 113 pending_connect_send_stream_.reset(); |
| 114 pending_connect_receive_stream_.reset(); |
| 115 pending_connect_socket_ = InterfaceRequest<TCPConnectedSocket>(); |
| 116 } |
| 117 pending_connect_callback_.Run(MakeNetworkError(result)); |
| 118 pending_connect_callback_ = Callback<void(NetworkErrorPtr)>(); |
| 80 } | 119 } |
| 81 | 120 |
| 82 } // namespace mojo | 121 } // namespace mojo |
| OLD | NEW |