Peter Qiu | c0beca5 | 2015-09-03 11:25:46 -0700 | [diff] [blame] | 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 | // |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 16 | |
Ben Chan | c45688b | 2014-07-02 23:50:45 -0700 | [diff] [blame] | 17 | #ifndef SHILL_ASYNC_CONNECTION_H_ |
| 18 | #define SHILL_ASYNC_CONNECTION_H_ |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 19 | |
Ben Chan | 22f1fbc | 2014-10-17 14:18:07 -0700 | [diff] [blame] | 20 | #include <memory> |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 23 | #include <base/callback.h> |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 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. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 43 | AsyncConnection(const std::string& interface_name, |
| 44 | EventDispatcher* dispatcher, |
| 45 | Sockets* sockets, |
| 46 | const base::Callback<void(bool, int)>& callback); |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 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. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 63 | virtual bool Start(const IPAddress& address, int port); |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 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 | |
| 70 | std::string error() const { return error_; } |
| 71 | |
| 72 | private: |
| 73 | friend class AsyncConnectionTest; |
| 74 | |
| 75 | void OnConnectCompletion(int fd); |
| 76 | |
Peter Qiu | f3a8f90 | 2014-08-20 10:05:42 -0700 | [diff] [blame] | 77 | // Initiate a socket connection to given IP address and port (in host order). |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 78 | int ConnectTo(const IPAddress& address, int port); |
Peter Qiu | f3a8f90 | 2014-08-20 10:05:42 -0700 | [diff] [blame] | 79 | |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 80 | std::string interface_name_; |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 81 | EventDispatcher* dispatcher_; |
| 82 | Sockets* sockets_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 83 | base::Callback<void(bool, int)> callback_; |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 84 | std::string error_; |
| 85 | int fd_; |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 86 | base::Callback<void(int)> connect_completion_callback_; |
Ben Chan | 22f1fbc | 2014-10-17 14:18:07 -0700 | [diff] [blame] | 87 | std::unique_ptr<IOHandler> connect_completion_handler_; |
Paul Stewart | f65320c | 2011-10-13 14:34:52 -0700 | [diff] [blame] | 88 | |
| 89 | DISALLOW_COPY_AND_ASSIGN(AsyncConnection); |
| 90 | }; |
| 91 | |
| 92 | } // namespace shill |
| 93 | |
Ben Chan | c45688b | 2014-07-02 23:50:45 -0700 | [diff] [blame] | 94 | #endif // SHILL_ASYNC_CONNECTION_H_ |