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