• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2011 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef SHILL_ASYNC_CONNECTION_H_
18 #define SHILL_ASYNC_CONNECTION_H_
19 
20 #include <memory>
21 #include <string>
22 
23 #include <base/callback.h>
24 
25 #include "shill/refptr_types.h"
26 
27 namespace shill {
28 
29 class EventDispatcher;
30 class IOHandler;
31 class IPAddress;
32 class Sockets;
33 
34 // The AsyncConnection class implements an asynchronous
35 // outgoing TCP connection.  When passed an IPAddress and
36 // port, and it will notify the caller when the connection
37 // is made.  It can also be passed an interface name to
38 // bind the local side of the connection.
39 class AsyncConnection {
40  public:
41   // If non-empty |interface_name| specifies an local interface from which
42   // to originate the connection.
43   AsyncConnection(const std::string& interface_name,
44                   EventDispatcher* dispatcher,
45                   Sockets* sockets,
46                   const base::Callback<void(bool, int)>& callback);
47   virtual ~AsyncConnection();
48 
49   // Open a connection given an IP address and port (in host order).
50   // When the connection completes, |callback| will be called with the
51   // a boolean (indicating success if true) and an fd of the opened socket
52   // (in the success case).  If successful, ownership of this open fd is
53   // passed to the caller on execution of the callback.
54   //
55   // This function (Start) returns true if the connection is in progress,
56   // or if the connection has immediately succeeded (the callback will be
57   // called in this case).  On success the callback may be called before
58   // Start() returns to its caller.  On failure to start the connection,
59   // this function returns false, but does not execute the callback.
60   //
61   // Calling Start() on an AsyncConnection that is already Start()ed is
62   // an error.
63   virtual bool Start(const IPAddress& address, int port);
64 
65   // Stop the open connection, closing any fds that are still owned.
66   // Calling Stop() on an unstarted or Stop()ped AsyncConnection is
67   // a no-op.
68   virtual void Stop();
69 
error()70   std::string error() const { return error_; }
71 
72  private:
73   friend class AsyncConnectionTest;
74 
75   void OnConnectCompletion(int fd);
76 
77   // Initiate a socket connection to given IP address and port (in host order).
78   int ConnectTo(const IPAddress& address, int port);
79 
80   std::string interface_name_;
81   EventDispatcher* dispatcher_;
82   Sockets* sockets_;
83   base::Callback<void(bool, int)> callback_;
84   std::string error_;
85   int fd_;
86   base::Callback<void(int)> connect_completion_callback_;
87   std::unique_ptr<IOHandler> connect_completion_handler_;
88 
89   DISALLOW_COPY_AND_ASSIGN(AsyncConnection);
90 };
91 
92 }  // namespace shill
93 
94 #endif  // SHILL_ASYNC_CONNECTION_H_
95