blob: 915a3f72a9faa6273dc173ef1c674932d2d55a43 [file] [log] [blame]
Peter Qiuc0beca52015-09-03 11:25:46 -07001//
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 Stewartf65320c2011-10-13 14:34:52 -070016
Ben Chanc45688b2014-07-02 23:50:45 -070017#ifndef SHILL_ASYNC_CONNECTION_H_
18#define SHILL_ASYNC_CONNECTION_H_
Paul Stewartf65320c2011-10-13 14:34:52 -070019
Ben Chan22f1fbc2014-10-17 14:18:07 -070020#include <memory>
Paul Stewartf65320c2011-10-13 14:34:52 -070021#include <string>
22
Eric Shienbrood3e20a232012-02-16 11:35:56 -050023#include <base/callback.h>
Paul Stewartf65320c2011-10-13 14:34:52 -070024
25#include "shill/refptr_types.h"
26
27namespace shill {
28
29class EventDispatcher;
30class IOHandler;
31class IPAddress;
32class 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.
39class AsyncConnection {
40 public:
41 // If non-empty |interface_name| specifies an local interface from which
42 // to originate the connection.
Paul Stewarta794cd62015-06-16 13:13:10 -070043 AsyncConnection(const std::string& interface_name,
44 EventDispatcher* dispatcher,
45 Sockets* sockets,
46 const base::Callback<void(bool, int)>& callback);
Paul Stewartf65320c2011-10-13 14:34:52 -070047 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 Stewarta794cd62015-06-16 13:13:10 -070063 virtual bool Start(const IPAddress& address, int port);
Paul Stewartf65320c2011-10-13 14:34:52 -070064
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 Qiuf3a8f902014-08-20 10:05:42 -070077 // Initiate a socket connection to given IP address and port (in host order).
Paul Stewarta794cd62015-06-16 13:13:10 -070078 int ConnectTo(const IPAddress& address, int port);
Peter Qiuf3a8f902014-08-20 10:05:42 -070079
Paul Stewartf65320c2011-10-13 14:34:52 -070080 std::string interface_name_;
Paul Stewarta794cd62015-06-16 13:13:10 -070081 EventDispatcher* dispatcher_;
82 Sockets* sockets_;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050083 base::Callback<void(bool, int)> callback_;
Paul Stewartf65320c2011-10-13 14:34:52 -070084 std::string error_;
85 int fd_;
Eric Shienbrood3e20a232012-02-16 11:35:56 -050086 base::Callback<void(int)> connect_completion_callback_;
Ben Chan22f1fbc2014-10-17 14:18:07 -070087 std::unique_ptr<IOHandler> connect_completion_handler_;
Paul Stewartf65320c2011-10-13 14:34:52 -070088
89 DISALLOW_COPY_AND_ASSIGN(AsyncConnection);
90};
91
92} // namespace shill
93
Ben Chanc45688b2014-07-02 23:50:45 -070094#endif // SHILL_ASYNC_CONNECTION_H_