]>
Commit | Line | Data |
---|---|---|
4c218615 EB |
1 | /* |
2 | * Copyright (C) 1996-2020 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_IPC_QUESTIONERID_H | |
10 | #define SQUID_SRC_IPC_QUESTIONERID_H | |
11 | ||
12 | #include "ipc/forward.h" | |
13 | ||
14 | #include <iosfwd> | |
15 | ||
16 | namespace Ipc | |
17 | { | |
18 | ||
19 | /// Identifies a kid process sending IPC messages that require an answer. | |
20 | /// Must be unique across all kids with pending questions. | |
21 | class QuestionerId | |
22 | { | |
23 | public: | |
24 | /// to-be-determined ID | |
25 | QuestionerId() = default; | |
26 | ||
27 | /// for sending the ID of the asking process | |
28 | void pack(TypedMsgHdr &) const; | |
29 | ||
30 | /// for receiving the ID of the asking process | |
31 | void unpack(const TypedMsgHdr &); | |
32 | ||
33 | /// does nothing but throws if the questioner was not the current process | |
34 | void rejectAnswerIfStale() const; | |
35 | ||
36 | /// reports the stored opaque ID value (for debugging) | |
37 | void print(std::ostream &) const; | |
38 | ||
39 | private: | |
40 | /// for MyQuestionerId() convenience | |
41 | explicit QuestionerId(const pid_t aPid): pid(aPid) {} | |
42 | friend QuestionerId MyQuestionerId(); | |
43 | ||
44 | /// OS process ID of the asking kid. If the kid restarts, it is assumed | |
45 | /// not to wrap back to the old value until the answer is received. | |
46 | pid_t pid = -1; | |
47 | }; | |
48 | ||
49 | /// the questioner ID of the current/calling process | |
50 | QuestionerId MyQuestionerId(); | |
51 | ||
52 | /// Convenience wrapper for rejecting (freshly parsed) stale answers. | |
53 | /// All answers are assumed to have a "QuestionerId intendedRecepient()" member. | |
54 | template <class Answer> | |
55 | const Answer & | |
56 | Mine(const Answer &answer) | |
57 | { | |
58 | answer.intendedRecepient().rejectAnswerIfStale(); | |
59 | return answer; | |
60 | } | |
61 | ||
62 | inline std::ostream & | |
63 | operator <<(std::ostream &os, const QuestionerId &qid) | |
64 | { | |
65 | qid.print(os); | |
66 | return os; | |
67 | } | |
68 | ||
69 | } // namespace Ipc; | |
70 | ||
71 | #endif /* SQUID_SRC_IPC_QUESTIONERID_H */ | |
72 |