Open Lighting Architecture  Latest Git
TCPConnector.h
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * This library is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10  * Lesser General Public License for more details.
11  *
12  * You should have received a copy of the GNU Lesser General Public
13  * License along with this library; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15  *
16  * TCPConnector.h
17  * Copyright (C) 2012 Simon Newton
18  */
19 
20 #ifndef INCLUDE_OLA_NETWORK_TCPCONNECTOR_H_
21 #define INCLUDE_OLA_NETWORK_TCPCONNECTOR_H_
22 
23 #include <ola/Callback.h>
24 #include <ola/Clock.h>
25 #include <ola/base/Macro.h>
26 #include <ola/io/Descriptor.h>
27 #include <ola/io/SelectServerInterface.h>
30 #include <ola/network/TCPSocket.h>
31 #include <set>
32 #include <vector>
33 
34 namespace ola {
35 namespace network {
36 
40 class TCPConnector {
41  public:
49 
55  typedef const void* TCPConnectionID;
56 
62 
63  ~TCPConnector();
64 
78  TCPConnectionID Connect(const IPV4SocketAddress &endpoint,
79  const ola::TimeInterval &timeout,
80  TCPConnectCallback *callback);
81 
91  bool Cancel(TCPConnectionID id);
92 
96  void CancelAll();
97 
101  unsigned int ConnectionsPending() const { return m_connections.size(); }
102 
107  void SocketWritable(class PendingTCPConnection *connection);
108 
109  private:
110  typedef std::set<class PendingTCPConnection*> ConnectionSet;
111 
113  ConnectionSet m_connections;
114 
115  void FreePendingConnection(class PendingTCPConnection *connection);
116  void Timeout(const ConnectionSet::iterator &iter);
117  void TimeoutEvent(class PendingTCPConnection *connection);
118  void CleanUpOrphans();
119 
120  DISALLOW_COPY_AND_ASSIGN(TCPConnector);
121 };
122 } // namespace network
123 } // namespace ola
124 #endif // INCLUDE_OLA_NETWORK_TCPCONNECTOR_H_
A time interval, with usecond accuracy.
Definition: Clock.h:138
Represents Socket Addresses.
ola::SingleUseCallback2< void, int, int > TCPConnectCallback
The callback run when a TCP connection request completes.
Definition: TCPConnector.h:48
unsigned int ConnectionsPending() const
Return the number of pending connections.
Definition: TCPConnector.h:101
const void * TCPConnectionID
The TCPConnectionID.
Definition: TCPConnector.h:55
bool Cancel(TCPConnectionID id)
Cancel a pending TCP connection.
Definition: TCPConnector.cpp:176
An class which manages non-blocking TCP connects.
Definition: TCPConnector.h:40
The interface for the SelectServer.
Definition: SelectServerInterface.h:42
Represents an IPv4 Address.
Helper macros.
void SocketWritable(class PendingTCPConnection *connection)
Called when the TCP socket connects.
Definition: TCPConnector.cpp:212
The namespace containing all OLA symbols.
Definition: Credentials.cpp:44
Definition: TCPConnector.cpp:40
void CancelAll()
Cancel all pending TCP connections.
Definition: TCPConnector.cpp:194
TCPConnector(ola::io::SelectServerInterface *ss)
Create a new TCPConnector.
Definition: TCPConnector.cpp:101
An IPv4 SocketAddress.
Definition: SocketAddress.h:78
TCPConnectionID Connect(const IPV4SocketAddress &endpoint, const ola::TimeInterval &timeout, TCPConnectCallback *callback)
Perform a non-blocking TCP connect.
Definition: TCPConnector.cpp:110
A 2 argument callback which deletes itself after it&#39;s run.
Definition: Callback.h:1907