blob: 216d8a2930e85c60b1e687e335354ff478df3d28 [file] [log] [blame]
Paul Stewartf65320c2011-10-13 14:34:52 -07001// Copyright (c) 2011 The Chromium OS 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
Ben Chanc45688b2014-07-02 23:50:45 -07005#ifndef SHILL_ASYNC_CONNECTION_H_
6#define SHILL_ASYNC_CONNECTION_H_
Paul Stewartf65320c2011-10-13 14:34:52 -07007
8#include <string>
9
Eric Shienbrood3e20a232012-02-16 11:35:56 -050010#include <base/callback.h>
Paul Stewartf65320c2011-10-13 14:34:52 -070011#include <base/memory/scoped_ptr.h>
12
13#include "shill/refptr_types.h"
14
15namespace shill {
16
17class EventDispatcher;
18class IOHandler;
19class IPAddress;
20class Sockets;
21
22// The AsyncConnection class implements an asynchronous
23// outgoing TCP connection. When passed an IPAddress and
24// port, and it will notify the caller when the connection
25// is made. It can also be passed an interface name to
26// bind the local side of the connection.
27class AsyncConnection {
28 public:
29 // If non-empty |interface_name| specifies an local interface from which
30 // to originate the connection.
31 AsyncConnection(const std::string &interface_name,
32 EventDispatcher *dispatcher,
33 Sockets *sockets,
Eric Shienbrood3e20a232012-02-16 11:35:56 -050034 const base::Callback<void(bool, int)> &callback);
Paul Stewartf65320c2011-10-13 14:34:52 -070035 virtual ~AsyncConnection();
36
37 // Open a connection given an IP address and port (in host order).
38 // When the connection completes, |callback| will be called with the
39 // a boolean (indicating success if true) and an fd of the opened socket
40 // (in the success case). If successful, ownership of this open fd is
41 // passed to the caller on execution of the callback.
42 //
43 // This function (Start) returns true if the connection is in progress,
44 // or if the connection has immediately succeeded (the callback will be
45 // called in this case). On success the callback may be called before
46 // Start() returns to its caller. On failure to start the connection,
47 // this function returns false, but does not execute the callback.
48 //
49 // Calling Start() on an AsyncConnection that is already Start()ed is
50 // an error.
51 virtual bool Start(const IPAddress &address, int port);
52
53 // Stop the open connection, closing any fds that are still owned.
54 // Calling Stop() on an unstarted or Stop()ped AsyncConnection is
55 // a no-op.
56 virtual void Stop();
57
58 std::string error() const { return error_; }
59
60 private:
61 friend class AsyncConnectionTest;
62
63 void OnConnectCompletion(int fd);
64
65 std::string interface_name_;
66 EventDispatcher *dispatcher_;
67 Sockets *sockets_;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050068 base::Callback<void(bool, int)> callback_;
Paul Stewartf65320c2011-10-13 14:34:52 -070069 std::string error_;
70 int fd_;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050071 base::Callback<void(int)> connect_completion_callback_;
Paul Stewartf65320c2011-10-13 14:34:52 -070072 scoped_ptr<IOHandler> connect_completion_handler_;
73
74 DISALLOW_COPY_AND_ASSIGN(AsyncConnection);
75};
76
77} // namespace shill
78
Ben Chanc45688b2014-07-02 23:50:45 -070079#endif // SHILL_ASYNC_CONNECTION_H_