]>
Commit | Line | Data |
---|---|---|
98d9bd29 AR |
1 | /* |
2 | * $Id$ | |
3 | * | |
4 | * DEBUG: section 54 Interprocess Communication | |
5 | * | |
6 | */ | |
7 | ||
8 | #ifndef SQUID_IPC_TYPED_MSG_HDR_H | |
9 | #define SQUID_IPC_TYPED_MSG_HDR_H | |
10 | ||
ece5481a | 11 | #include "config.h" |
9614899c AJ |
12 | |
13 | #if HAVE_SYS_SOCKET_H | |
98d9bd29 | 14 | #include <sys/socket.h> |
9614899c AJ |
15 | #endif |
16 | #if HAVE_SYS_UIO_H | |
17 | #include <sys/uio.h> | |
18 | #endif | |
98d9bd29 AR |
19 | #if HAVE_SYS_UN_H |
20 | #include <sys/un.h> | |
21 | #endif | |
22 | ||
8822ebee AR |
23 | class String; |
24 | ||
98d9bd29 AR |
25 | namespace Ipc |
26 | { | |
27 | ||
28 | /// struct msghdr with a known type, fixed-size I/O and control buffers | |
29 | class TypedMsgHdr: public msghdr | |
30 | { | |
8822ebee AR |
31 | public: |
32 | enum {maxSize = 4096}; | |
33 | ||
98d9bd29 AR |
34 | public: |
35 | TypedMsgHdr(); | |
36 | TypedMsgHdr(const TypedMsgHdr &tmh); | |
37 | TypedMsgHdr &operator =(const TypedMsgHdr &tmh); | |
38 | ||
8822ebee AR |
39 | void address(const struct sockaddr_un &addr); ///< sets [dest.] address |
40 | ||
41 | /* message type manipulation; these must be called before put/get*() */ | |
42 | void setType(int aType); ///< sets message type; use MessageType enum | |
43 | void checkType(int aType) const; ///< throws if stored type is not aType | |
98d9bd29 | 44 | int type() const; ///< returns stored type or zero if none |
8822ebee AR |
45 | |
46 | /* access for Plain Old Data (POD)-based message parts */ | |
47 | template <class Pod> | |
48 | void getPod(Pod &pod) const { getFixed(&pod, sizeof(pod)); } ///< load POD | |
49 | template <class Pod> | |
50 | void putPod(const Pod &pod) { putFixed(&pod, sizeof(pod)); } ///< store POD | |
51 | ||
52 | /* access to message parts for selected commonly-used part types */ | |
53 | void getString(String &s) const; ///< load variable-length string | |
54 | void putString(const String &s); ///< store variable-length string | |
55 | int getInt() const; ///< load an integer | |
56 | void putInt(int n); ///< store an integer | |
57 | void getFixed(void *raw, size_t size) const; ///< always load size bytes | |
58 | void putFixed(const void *raw, size_t size); ///< always store size bytes | |
59 | ||
60 | /// returns true if there is data to extract; handy for optional parts | |
61 | bool hasMoreData() const { return offset < data.size; } | |
62 | ||
63 | /* access to a "file" descriptor that can be passed between processes */ | |
5667a628 AR |
64 | void putFd(int aFd); ///< stores descriptor |
65 | int getFd() const; ///< returns descriptor | |
98d9bd29 | 66 | |
8822ebee | 67 | /* raw, type-independent access for I/O */ |
5667a628 AR |
68 | void prepForReading(); ///< reset and provide all buffers |
69 | char *raw() { return reinterpret_cast<char*>(this); } | |
70 | const char *raw() const { return reinterpret_cast<const char*>(this); } | |
98d9bd29 AR |
71 | size_t size() const { return sizeof(*this); } ///< not true message size |
72 | ||
73 | private: | |
5667a628 AR |
74 | void sync(); |
75 | void allocData(); | |
76 | void allocName(); | |
77 | void allocControl(); | |
98d9bd29 | 78 | |
8822ebee AR |
79 | /* raw, type-independent manipulation used by type-specific methods */ |
80 | void getRaw(void *raw, size_t size) const; | |
81 | void putRaw(const void *raw, size_t size); | |
82 | ||
98d9bd29 | 83 | private: |
5667a628 | 84 | struct sockaddr_un name; ///< same as .msg_name |
98d9bd29 | 85 | |
5667a628 | 86 | struct iovec ios[1]; ///< same as .msg_iov[] |
98d9bd29 | 87 | |
5667a628 AR |
88 | struct DataBuffer { |
89 | int type_; ///< Message kind, uses MessageType values | |
90 | size_t size; ///< actual raw data size (for sanity checks) | |
8822ebee | 91 | char raw[maxSize]; ///< buffer with type-specific data |
5667a628 | 92 | } data; ///< same as .msg_iov[0].iov_base |
98d9bd29 | 93 | |
5667a628 AR |
94 | struct CtrlBuffer { |
95 | char raw[CMSG_SPACE(sizeof(int))]; ///< control buffer space for one fd | |
96 | } ctrl; ///< same as .msg_control | |
8822ebee AR |
97 | |
98 | /// data offset for the next get/put*() to start with | |
99 | mutable unsigned int offset; | |
98d9bd29 AR |
100 | }; |
101 | ||
102 | } // namespace Ipc | |
103 | ||
104 | #endif /* SQUID_IPC_TYPED_MSG_HDR_H */ |