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