blob: 6c3673041ca6f1ed5a281f93f16fd915b3d55bdf [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
Ben Chan22f1fbc2014-10-17 14:18:07 -07008#include <memory>
Paul Stewartf65320c2011-10-13 14:34:52 -07009#include <string>
10
Eric Shienbrood3e20a232012-02-16 11:35:56 -050011#include <base/callback.h>
Paul Stewartf65320c2011-10-13 14:34:52 -070012
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
Peter Qiuf3a8f902014-08-20 10:05:42 -070065 // Initiate a socket connection to given IP address and port (in host order).
66 int ConnectTo(const IPAddress &address, int port);
67
Paul Stewartf65320c2011-10-13 14:34:52 -070068 std::string interface_name_;
69 EventDispatcher *dispatcher_;
70 Sockets *sockets_;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050071 base::Callback<void(bool, int)> callback_;
Paul Stewartf65320c2011-10-13 14:34:52 -070072 std::string error_;
73 int fd_;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050074 base::Callback<void(int)> connect_completion_callback_;
Ben Chan22f1fbc2014-10-17 14:18:07 -070075 std::unique_ptr<IOHandler> connect_completion_handler_;
Paul Stewartf65320c2011-10-13 14:34:52 -070076
77 DISALLOW_COPY_AND_ASSIGN(AsyncConnection);
78};
79
80} // namespace shill
81
Ben Chanc45688b2014-07-02 23:50:45 -070082#endif // SHILL_ASYNC_CONNECTION_H_