summaryrefslogtreecommitdiffstats
path: root/net/base/tcp_connecting_socket.h
blob: 709669287095c26adab9ad96b2fc9dc3d11d5dd0 (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
// Copyright (c) 2006-2008 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.

#ifndef NET_BASE_TCP_CONNECTING_SOCKET_H_
#define NET_BASE_TCP_CONNECTING_SOCKET_H_

#include <deque>
#include <map>
#include <string>

#include "base/scoped_ptr.h"
#include "base/timer.h"
#include "net/base/address_list.h"
#include "net/base/client_socket_pool.h"
#include "net/base/host_resolver.h"

namespace net {

class ClientSocketFactory;
class ClientSocketPoolBase;

// TCPConnectingSocket handles host resolution and socket connection for a TCP
// socket.
class TCPConnectingSocket {
 public:
  TCPConnectingSocket(const std::string& group_name,
                      const std::string& host,
                      int port,
                      const ClientSocketHandle* handle,
                      CompletionCallback* callback,
                      ClientSocketFactory* client_socket_factory,
                      const scoped_refptr<ClientSocketPoolBase>& pool);
  ~TCPConnectingSocket();

  // Begins the host resolution and the TCP connect.  Returns OK on success
  // and ERR_IO_PENDING if it cannot immediately service the request.
  // Otherwise, it returns a net error code.
  int Connect();

  // TODO(willchan): Delete this function once we decouple connecting sockets
  // from requests, since we'll keep around the idle connected socket.
  // Called by the ClientSocketPoolBase to cancel this TCPConnectingSocket.
  // Only necessary if a ClientSocketHandle is reused.
  void Cancel();

 private:
  // Handles asynchronous completion of IO.  |result| represents the result of
  // the IO operation.
  void OnIOComplete(int result);

  // Handles both asynchronous and synchronous completion of IO.  |result|
  // represents the result of the IO operation.  |synchronous| indicates
  // whether or not the previous IO operation completed synchronously or
  // asynchronously.  OnIOCompleteInternal returns the result of the next IO
  // operation that executes, or just the value of |result|.
  int OnIOCompleteInternal(int result, bool synchronous);

  const std::string group_name_;
  const std::string host_;
  const int port_;
  const ClientSocketHandle* const handle_;
  CompletionCallback* const user_callback_;
  ClientSocketFactory* const client_socket_factory_;
  CompletionCallbackImpl<TCPConnectingSocket> callback_;
  scoped_ptr<ClientSocket> socket_;
  const scoped_refptr<ClientSocketPoolBase> pool_;
  HostResolver resolver_;
  AddressList addresses_;
  bool canceled_;

  // The time the Connect() method was called (if it got called).
  base::Time connect_start_time_;

  DISALLOW_COPY_AND_ASSIGN(TCPConnectingSocket);
};

}  // namespace net

#endif  // NET_BASE_TCP_CONNECTING_SOCKET_H_