summaryrefslogtreecommitdiffstats
path: root/mojo/services/network/tcp_bound_socket_impl.cc
blob: d8a60b7ea5a861a9a99ccc2324da6ab74088e870 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// 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 <utility>

#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(
    scoped_ptr<mojo::AppRefCount> app_refcount,
    InterfaceRequest<TCPBoundSocket> request)
    : app_refcount_(std::move(app_refcount)),
      binding_(this, std::move(request)) {}

TCPBoundSocketImpl::~TCPBoundSocketImpl() {
}

int TCPBoundSocketImpl::Bind(NetAddressPtr local_address) {
  net::IPEndPoint 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;

  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 NetAddress::From(resolved_local_address);
}

void TCPBoundSocketImpl::StartListening(
    InterfaceRequest<TCPServerSocket> server,
    const Callback<void(NetworkErrorPtr)>& callback) {
  if (!socket_ || pending_connect_socket_.is_pending()) {
    // 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_FAILED));
    return;
  }

  // TODO(brettw) set the backlog properly.
  int result = socket_->Listen(4);
  if (result != net::OK) {
    callback.Run(MakeNetworkError(result));
    return;
  }

  // The server socket object takes ownership of the socket.
  new TCPServerSocketImpl(std::move(socket_), app_refcount_->Clone(),
                          std::move(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) {
  if (!socket_ || pending_connect_socket_.is_pending()) {
    // 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_FAILED));
    return;
  }

  net::IPEndPoint end_point = remote_address.To<net::IPEndPoint>();

  pending_connect_send_stream_ = std::move(send_stream);
  pending_connect_receive_stream_ = std::move(receive_stream);
  pending_connect_socket_ = std::move(client_socket);
  pending_connect_callback_ = callback;
  int result = socket_->Connect(
      end_point,
      base::Bind(&TCPBoundSocketImpl::OnConnected, base::Unretained(this)));
  if (result == net::OK) {
    OnConnected(result);
  } else if (result != net::ERR_IO_PENDING) {
    // Error occurred.
    pending_connect_send_stream_.reset();
    pending_connect_receive_stream_.reset();
    pending_connect_socket_ = InterfaceRequest<TCPConnectedSocket>();
    pending_connect_callback_ = Callback<void(NetworkErrorPtr)>();
    callback.Run(MakeNetworkError(result));
  }
}

void TCPBoundSocketImpl::OnConnected(int result) {
  if (result == net::OK) {
    new TCPConnectedSocketImpl(
        std::move(socket_), std::move(pending_connect_send_stream_),
        std::move(pending_connect_receive_stream_),
        std::move(pending_connect_socket_), app_refcount_->Clone());
  } else {
    pending_connect_send_stream_.reset();
    pending_connect_receive_stream_.reset();
    pending_connect_socket_ = InterfaceRequest<TCPConnectedSocket>();
  }
  pending_connect_callback_.Run(MakeNetworkError(result));
  pending_connect_callback_ = Callback<void(NetworkErrorPtr)>();
}

}  // namespace mojo