]> git.ipfire.org Git - thirdparty/squid.git/blob - src/comm/ConnOpener.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / comm / ConnOpener.h
1 /*
2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 #ifndef _SQUID_SRC_COMM_OPENERSTATEDATA_H
10 #define _SQUID_SRC_COMM_OPENERSTATEDATA_H
11
12 #include "base/AsyncCall.h"
13 #include "base/AsyncJob.h"
14 #include "cbdata.h"
15 #include "comm/Flag.h"
16 #include "comm/forward.h"
17 #include "CommCalls.h"
18
19 namespace Comm
20 {
21
22 /**
23 * Async-opener of a Comm connection.
24 */
25 class ConnOpener : public AsyncJob
26 {
27 CBDATA_CLASS(ConnOpener);
28
29 public:
30 void noteAbort() { mustStop("externally aborted"); }
31
32 typedef CbcPointer<ConnOpener> Pointer;
33
34 virtual bool doneAll() const;
35
36 ConnOpener(Comm::ConnectionPointer &, AsyncCall::Pointer &handler, time_t connect_timeout);
37 ~ConnOpener();
38
39 void setHost(const char *); ///< set the hostname note for this connection
40 const char * getHost() const; ///< get the hostname noted for this connection
41
42 protected:
43 virtual void start();
44 virtual void swanSong();
45
46 private:
47 // Undefined because two openers cannot share a connection
48 ConnOpener(const ConnOpener &);
49 ConnOpener & operator =(const ConnOpener &c);
50
51 void earlyAbort(const CommCloseCbParams &);
52 void timeout(const CommTimeoutCbParams &);
53 void sendAnswer(Comm::Flag errFlag, int xerrno, const char *why);
54 static void InProgressConnectRetry(int fd, void *data);
55 static void DelayedConnectRetry(void *data);
56 void doConnect();
57 void connected();
58 void lookupLocalAddress();
59
60 void retrySleep();
61 void restart();
62
63 bool createFd();
64 void closeFd();
65 void keepFd();
66 void cleanFd();
67
68 void cancelSleep();
69
70 private:
71 char *host_; ///< domain name we are trying to connect to.
72 int temporaryFd_; ///< the FD being opened. Do NOT set conn_->fd until it is fully open.
73 Comm::ConnectionPointer conn_; ///< single connection currently to be opened.
74 AsyncCall::Pointer callback_; ///< handler to be called on connection completion.
75
76 int totalTries_; ///< total number of connection attempts over all destinations so far.
77 int failRetries_; ///< number of retries current destination has been tried.
78
79 /// if we are not done by then, we will call back with Comm::TIMEOUT
80 time_t deadline_;
81
82 /// handles to calls which we may need to cancel.
83 struct Calls {
84 AsyncCall::Pointer earlyAbort_;
85 AsyncCall::Pointer timeout_;
86 /// Whether we are idling before retrying to connect; not yet a call
87 /// [that we can cancel], but it will probably become one eventually.
88 bool sleep_;
89 } calls_;
90 };
91
92 }; // namespace Comm
93
94 #endif /* _SQUID_SRC_COMM_CONNOPENER_H */
95