Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(752)

Unified Diff: mojo/services/network/tcp_bound_socket_impl.cc

Issue 634713002: Implement parts of the Mojo TCP interfaces. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: mojo/services/network/tcp_bound_socket_impl.cc
diff --git a/mojo/services/network/tcp_bound_socket_impl.cc b/mojo/services/network/tcp_bound_socket_impl.cc
new file mode 100644
index 0000000000000000000000000000000000000000..67bca7149bfea07d1a54db77544507eb7dfe6a73
--- /dev/null
+++ b/mojo/services/network/tcp_bound_socket_impl.cc
@@ -0,0 +1,85 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "mojo/services/network/tcp_bound_socket_impl.h"
+
+#include "mojo/services/network/net_adapters.h"
+#include "mojo/services/network/net_address_type_converters.h"
+#include "mojo/services/network/tcp_connected_socket_impl.h"
+#include "mojo/services/network/tcp_server_socket_impl.h"
+#include "net/base/net_errors.h"
+
+namespace mojo {
+
+TCPBoundSocketImpl::TCPBoundSocketImpl() {
+}
+
+TCPBoundSocketImpl::~TCPBoundSocketImpl() {
+}
+
+int TCPBoundSocketImpl::Bind(NetAddressPtr local_address) {
+ // The local address might be null to match any port.
+ net::IPEndPoint end_point;
+ if (!local_address.is_null())
+ end_point = local_address.To<net::IPEndPoint>();
+
+ socket_.reset(new net::TCPSocket(NULL, net::NetLog::Source()));
+ int result = socket_->Open(end_point.GetFamily());
+ if (result != net::OK)
+ return result;
+
+ // 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
+ // so we knoe whether to do default options for client or server?
yzshen1 2014/10/07 20:14:34 knoe->know
+ result = socket_->SetDefaultOptionsForServer();
+ if (result != net::OK)
+ return result;
+
+ result = socket_->Bind(end_point);
+ if (result != net::OK)
+ return result;
+
+ return net::OK;
+}
+
+NetAddressPtr TCPBoundSocketImpl::GetLocalAddress() const {
+ net::IPEndPoint resolved_local_address;
+ if (socket_->GetLocalAddress(&resolved_local_address) != net::OK)
+ return NetAddressPtr();
+ return mojo::TypeConverter<NetAddressPtr, net::IPEndPoint>::Convert(
yzshen1 2014/10/07 20:14:34 You could do return NetAddress::From(resolved_loca
+ resolved_local_address);
+}
+
+void TCPBoundSocketImpl::StartListening(
+ InterfaceRequest<TCPServerSocket> server,
+ const Callback<void(NetworkErrorPtr)>& callback) {
+ if (!socket_) {
+ // A bound socket will only be returned to the caller after binding
+ // succeeds, so if the socket doesn't exist, that means ownership was
+ // already passed to a server socket or client socket.
+ callback.Run(MakeNetworkError(net::ERR_SOCKET_IS_CONNECTED));
yzshen1 2014/10/07 20:14:34 Not quite sure whether this is the appropriate cod
+ return;
+ }
+
+ // TODO(brettw) set the backlog properly.
+ int result = socket_->Listen(4);
+ if (result != net::ERR_IO_PENDING) {
yzshen1 2014/10/07 20:14:34 I thought it should be net::OK?
+ callback.Run(MakeNetworkError(result));
+ return;
+ }
+
+ // The server socket object takes ownership of the socket.
+ BindToRequest(new TCPServerSocketImpl(socket_.Pass()), &server);
+ callback.Run(MakeNetworkError(net::OK));
+}
+
+void TCPBoundSocketImpl::Connect(
+ NetAddressPtr remote_address,
+ ScopedDataPipeConsumerHandle send_stream,
+ ScopedDataPipeProducerHandle receive_stream,
+ InterfaceRequest<TCPConnectedSocket> client_socket,
+ const Callback<void(NetworkErrorPtr)>& callback) {
+ // TODO(brettw) write this.
+}
+
+} // namespace mojo

Powered by Google App Engine
This is Rietveld 408576698