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

Side by Side Diff: mojo/services/network/tcp_bound_socket_impl.cc

Issue 1873463003: Remove mojo network service. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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 unified diff | Download patch
OLDNEW
(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 <utility>
8
9 #include "mojo/services/network/net_adapters.h"
10 #include "mojo/services/network/net_address_type_converters.h"
11 #include "mojo/services/network/tcp_connected_socket_impl.h"
12 #include "mojo/services/network/tcp_server_socket_impl.h"
13 #include "net/base/net_errors.h"
14
15 namespace mojo {
16
17 TCPBoundSocketImpl::TCPBoundSocketImpl(
18 scoped_ptr<mojo::MessageLoopRef> app_refcount,
19 InterfaceRequest<TCPBoundSocket> request)
20 : app_refcount_(std::move(app_refcount)),
21 binding_(this, std::move(request)) {}
22
23 TCPBoundSocketImpl::~TCPBoundSocketImpl() {
24 }
25
26 int TCPBoundSocketImpl::Bind(NetAddressPtr local_address) {
27 net::IPEndPoint end_point = local_address.To<net::IPEndPoint>();
28
29 socket_.reset(new net::TCPSocket(NULL, net::NetLog::Source()));
30 int result = socket_->Open(end_point.GetFamily());
31 if (result != net::OK)
32 return result;
33
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 NetAddress::From(resolved_local_address);
50 }
51
52 void TCPBoundSocketImpl::StartListening(
53 InterfaceRequest<TCPServerSocket> server,
54 const Callback<void(NetworkErrorPtr)>& callback) {
55 if (!socket_ || pending_connect_socket_.is_pending()) {
56 // A bound socket will only be returned to the caller after binding
57 // succeeds, so if the socket doesn't exist, that means ownership was
58 // already passed to a server socket or client socket.
59 callback.Run(MakeNetworkError(net::ERR_FAILED));
60 return;
61 }
62
63 // TODO(brettw) set the backlog properly.
64 int result = socket_->Listen(4);
65 if (result != net::OK) {
66 callback.Run(MakeNetworkError(result));
67 return;
68 }
69
70 // The server socket object takes ownership of the socket.
71 new TCPServerSocketImpl(std::move(socket_), app_refcount_->Clone(),
72 std::move(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 if (!socket_ || pending_connect_socket_.is_pending()) {
83 // A bound socket will only be returned to the caller after binding
84 // succeeds, so if the socket doesn't exist, that means ownership was
85 // already passed to a server socket or client socket.
86 callback.Run(MakeNetworkError(net::ERR_FAILED));
87 return;
88 }
89
90 net::IPEndPoint end_point = remote_address.To<net::IPEndPoint>();
91
92 pending_connect_send_stream_ = std::move(send_stream);
93 pending_connect_receive_stream_ = std::move(receive_stream);
94 pending_connect_socket_ = std::move(client_socket);
95 pending_connect_callback_ = callback;
96 int result = socket_->Connect(
97 end_point,
98 base::Bind(&TCPBoundSocketImpl::OnConnected, base::Unretained(this)));
99 if (result == net::OK) {
100 OnConnected(result);
101 } else if (result != net::ERR_IO_PENDING) {
102 // Error occurred.
103 pending_connect_send_stream_.reset();
104 pending_connect_receive_stream_.reset();
105 pending_connect_socket_ = InterfaceRequest<TCPConnectedSocket>();
106 pending_connect_callback_ = Callback<void(NetworkErrorPtr)>();
107 callback.Run(MakeNetworkError(result));
108 }
109 }
110
111 void TCPBoundSocketImpl::OnConnected(int result) {
112 if (result == net::OK) {
113 new TCPConnectedSocketImpl(
114 std::move(socket_), std::move(pending_connect_send_stream_),
115 std::move(pending_connect_receive_stream_),
116 std::move(pending_connect_socket_), app_refcount_->Clone());
117 } else {
118 pending_connect_send_stream_.reset();
119 pending_connect_receive_stream_.reset();
120 pending_connect_socket_ = InterfaceRequest<TCPConnectedSocket>();
121 }
122 pending_connect_callback_.Run(MakeNetworkError(result));
123 pending_connect_callback_ = Callback<void(NetworkErrorPtr)>();
124 }
125
126 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/services/network/tcp_bound_socket_impl.h ('k') | mojo/services/network/tcp_connected_socket_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698