]> git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/UdsOp.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / ipc / UdsOp.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 /* DEBUG: section 54 Interprocess Communication */
10
11 #ifndef SQUID_IPC_ASYNCUDSOP_H
12 #define SQUID_IPC_ASYNCUDSOP_H
13
14 #include "base/AsyncJob.h"
15 #include "cbdata.h"
16 #include "comm/forward.h"
17 #include "ipc/FdNotes.h"
18 #include "ipc/TypedMsgHdr.h"
19 #include "SquidString.h"
20
21 class CommTimeoutCbParams;
22 class CommIoCbParams;
23
24 namespace Ipc
25 {
26
27 /// code shared by unix-domain socket senders (e.g., UdsSender or Coordinator)
28 /// and receivers (e.g. Port or Coordinator)
29 class UdsOp: public AsyncJob
30 {
31 public:
32 UdsOp(const String &pathAddr);
33 virtual ~UdsOp();
34
35 public:
36 struct sockaddr_un address; ///< UDS address from path; treat as read-only
37
38 protected:
39 virtual void timedout() {} ///< called after setTimeout() if timed out
40
41 Comm::ConnectionPointer &conn(); ///< creates if needed and returns raw UDS socket descriptor
42
43 /// call timedout() if no UDS messages in a given number of seconds
44 void setTimeout(int seconds, const char *handlerName);
45 void clearTimeout(); ///< remove previously set timeout, if any
46
47 void setOptions(int newOptions); ///< changes socket options
48
49 private:
50 /// Comm timeout callback; calls timedout()
51 void noteTimeout(const CommTimeoutCbParams &p);
52
53 private:
54 int options; ///< UDS options
55 Comm::ConnectionPointer conn_; ///< UDS descriptor
56
57 private:
58 UdsOp(const UdsOp &); // not implemented
59 UdsOp &operator= (const UdsOp &); // not implemented
60 };
61
62 /// converts human-readable filename path into UDS address
63 struct sockaddr_un PathToAddress(const String &pathAddr);
64
65 // XXX: move UdsSender code to UdsSender.{cc,h}
66 /// attempts to send an IPC message a few times, with a timeout
67 class UdsSender: public UdsOp
68 {
69 CBDATA_CLASS(UdsSender);
70
71 public:
72 UdsSender(const String& pathAddr, const TypedMsgHdr& aMessage);
73
74 protected:
75 virtual void swanSong(); // UdsOp (AsyncJob) API
76 virtual void start(); // UdsOp (AsyncJob) API
77 virtual bool doneAll() const; // UdsOp (AsyncJob) API
78 virtual void timedout(); // UdsOp API
79
80 private:
81 void startSleep();
82 void cancelSleep();
83 static void DelayedRetry(void *data);
84 void delayedRetry();
85
86 void write(); ///< schedule writing
87 void wrote(const CommIoCbParams& params); ///< done writing or error
88
89 private:
90 TypedMsgHdr message; ///< what to send
91 int retries; ///< how many times to try after a write error
92 int timeout; ///< total time to send the message
93 bool sleeping; ///< whether we are waiting to retry a failed write
94 bool writing; ///< whether Comm started and did not finish writing
95
96 private:
97 UdsSender(const UdsSender&); // not implemented
98 UdsSender& operator= (const UdsSender&); // not implemented
99 };
100
101 void SendMessage(const String& toAddress, const TypedMsgHdr& message);
102 /// import socket fd from another strand into our Comm state
103 const Comm::ConnectionPointer & ImportFdIntoComm(const Comm::ConnectionPointer &conn, int socktype, int protocol, FdNoteId noteId);
104
105 }
106
107 #endif /* SQUID_IPC_ASYNCUDSOP_H */
108