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

Side by Side 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: missing file in gyp 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 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 "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 result = socket_->SetDefaultOptionsForServer();
33 if (result != net::OK)
34 return result;
35
36 result = socket_->Bind(end_point);
37 if (result != net::OK)
38 return result;
39
40 return net::OK;
41 }
42
43 NetAddressPtr TCPBoundSocketImpl::GetLocalAddress() const {
44 net::IPEndPoint resolved_local_address;
45 if (socket_->GetLocalAddress(&resolved_local_address) != net::OK)
46 return NetAddressPtr();
47 return NetAddress::From(resolved_local_address);
48 }
49
50 void TCPBoundSocketImpl::StartListening(
51 InterfaceRequest<TCPServerSocket> server,
52 const Callback<void(NetworkErrorPtr)>& callback) {
53 if (!socket_) {
54 // 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
56 // already passed to a server socket or client socket.
57 callback.Run(MakeNetworkError(net::ERR_FAILED));
58 return;
59 }
60
61 // TODO(brettw) set the backlog properly.
62 int result = socket_->Listen(4);
63 if (result != net::OK) {
64 callback.Run(MakeNetworkError(result));
65 return;
66 }
67
68 // The server socket object takes ownership of the socket.
69 BindToRequest(new TCPServerSocketImpl(socket_.Pass()), &server);
70 callback.Run(MakeNetworkError(net::OK));
71 }
72
73 void TCPBoundSocketImpl::Connect(
74 NetAddressPtr remote_address,
75 ScopedDataPipeConsumerHandle send_stream,
76 ScopedDataPipeProducerHandle receive_stream,
77 InterfaceRequest<TCPConnectedSocket> client_socket,
78 const Callback<void(NetworkErrorPtr)>& callback) {
79 // TODO(brettw) write this.
80 }
81
82 } // 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