Chromium Code Reviews| 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_bound_socket_impl.h" | |
| 6 | |
| 7 #include "mojo/services/network/net_adapters.h" | |
| 8 #include "mojo/services/network/net_address_type_converters.h" | |
| 9 #include "mojo/services/network/tcp_connected_socket_impl.h" | |
| 10 #include "mojo/services/network/tcp_server_socket_impl.h" | |
| 11 #include "net/base/net_errors.h" | |
| 12 | |
| 13 namespace mojo { | |
| 14 | |
| 15 TCPBoundSocketImpl::TCPBoundSocketImpl() { | |
| 16 } | |
| 17 | |
| 18 TCPBoundSocketImpl::~TCPBoundSocketImpl() { | |
| 19 } | |
| 20 | |
| 21 int TCPBoundSocketImpl::Bind(NetAddressPtr local_address) { | |
| 22 // The local address might be null to match any port. | |
| 23 net::IPEndPoint end_point; | |
| 24 if (!local_address.is_null()) | |
| 25 end_point = local_address.To<net::IPEndPoint>(); | |
| 26 | |
| 27 socket_.reset(new net::TCPSocket(NULL, net::NetLog::Source())); | |
| 28 int result = socket_->Open(end_point.GetFamily()); | |
| 29 if (result != net::OK) | |
| 30 return result; | |
| 31 | |
| 32 // TODO(brettw) can we set the options after we start listening or connecting | |
|
yzshen1
2014/10/07 20:14:34
I don't think so. :/ This call sets "address reuse
| |
| 33 // so we knoe whether to do default options for client or server? | |
|
yzshen1
2014/10/07 20:14:34
knoe->know
| |
| 34 result = socket_->SetDefaultOptionsForServer(); | |
| 35 if (result != net::OK) | |
| 36 return result; | |
| 37 | |
| 38 result = socket_->Bind(end_point); | |
| 39 if (result != net::OK) | |
| 40 return result; | |
| 41 | |
| 42 return net::OK; | |
| 43 } | |
| 44 | |
| 45 NetAddressPtr TCPBoundSocketImpl::GetLocalAddress() const { | |
| 46 net::IPEndPoint resolved_local_address; | |
| 47 if (socket_->GetLocalAddress(&resolved_local_address) != net::OK) | |
| 48 return NetAddressPtr(); | |
| 49 return mojo::TypeConverter<NetAddressPtr, net::IPEndPoint>::Convert( | |
|
yzshen1
2014/10/07 20:14:34
You could do
return NetAddress::From(resolved_loca
| |
| 50 resolved_local_address); | |
| 51 } | |
| 52 | |
| 53 void TCPBoundSocketImpl::StartListening( | |
| 54 InterfaceRequest<TCPServerSocket> server, | |
| 55 const Callback<void(NetworkErrorPtr)>& callback) { | |
| 56 if (!socket_) { | |
| 57 // A bound socket will only be returned to the caller after binding | |
| 58 // succeeds, so if the socket doesn't exist, that means ownership was | |
| 59 // already passed to a server socket or client socket. | |
| 60 callback.Run(MakeNetworkError(net::ERR_SOCKET_IS_CONNECTED)); | |
|
yzshen1
2014/10/07 20:14:34
Not quite sure whether this is the appropriate cod
| |
| 61 return; | |
| 62 } | |
| 63 | |
| 64 // TODO(brettw) set the backlog properly. | |
| 65 int result = socket_->Listen(4); | |
| 66 if (result != net::ERR_IO_PENDING) { | |
|
yzshen1
2014/10/07 20:14:34
I thought it should be net::OK?
| |
| 67 callback.Run(MakeNetworkError(result)); | |
| 68 return; | |
| 69 } | |
| 70 | |
| 71 // The server socket object takes ownership of the socket. | |
| 72 BindToRequest(new TCPServerSocketImpl(socket_.Pass()), &server); | |
| 73 callback.Run(MakeNetworkError(net::OK)); | |
| 74 } | |
| 75 | |
| 76 void TCPBoundSocketImpl::Connect( | |
| 77 NetAddressPtr remote_address, | |
| 78 ScopedDataPipeConsumerHandle send_stream, | |
| 79 ScopedDataPipeProducerHandle receive_stream, | |
| 80 InterfaceRequest<TCPConnectedSocket> client_socket, | |
| 81 const Callback<void(NetworkErrorPtr)>& callback) { | |
| 82 // TODO(brettw) write this. | |
| 83 } | |
| 84 | |
| 85 } // namespace mojo | |
| OLD | NEW |