]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ipc/TypedMsgHdr.h
4 * DEBUG: section 54 Interprocess Communication
8 #ifndef SQUID_IPC_TYPED_MSG_HDR_H
9 #define SQUID_IPC_TYPED_MSG_HDR_H
11 #include "compat/cmsg.h"
13 #include <sys/socket.h>
27 /// struct msghdr with a known type, fixed-size I/O and control buffers
28 class TypedMsgHdr
: public msghdr
31 enum {maxSize
= 4096};
35 TypedMsgHdr(const TypedMsgHdr
&tmh
);
36 TypedMsgHdr
&operator =(const TypedMsgHdr
&tmh
);
38 void address(const struct sockaddr_un
&addr
); ///< sets [dest.] address
40 /* message type manipulation; these must be called before put/get*() */
41 void setType(int aType
); ///< sets message type; use MessageType enum
42 void checkType(int aType
) const; ///< throws if stored type is not aType
43 int type() const; ///< returns stored type or zero if none
45 /* access for Plain Old Data (POD)-based message parts */
47 void getPod(Pod
&pod
) const { getFixed(&pod
, sizeof(pod
)); } ///< load POD
49 void putPod(const Pod
&pod
) { putFixed(&pod
, sizeof(pod
)); } ///< store POD
51 /* access to message parts for selected commonly-used part types */
52 void getString(String
&s
) const; ///< load variable-length string
53 void putString(const String
&s
); ///< store variable-length string
54 int getInt() const; ///< load an integer
55 void putInt(int n
); ///< store an integer
56 void getFixed(void *raw
, size_t size
) const; ///< always load size bytes
57 void putFixed(const void *raw
, size_t size
); ///< always store size bytes
59 /// returns true if there is data to extract; handy for optional parts
60 bool hasMoreData() const { return offset
< data
.size
; }
62 /* access to a "file" descriptor that can be passed between processes */
63 void putFd(int aFd
); ///< stores descriptor
64 int getFd() const; ///< returns descriptor
66 /* raw, type-independent access for I/O */
67 void prepForReading(); ///< reset and provide all buffers
68 char *raw() { return reinterpret_cast<char*>(this); }
69 const char *raw() const { return reinterpret_cast<const char*>(this); }
70 size_t size() const { return sizeof(*this); } ///< not true message size
78 /* raw, type-independent manipulation used by type-specific methods */
79 void getRaw(void *raw
, size_t size
) const;
80 void putRaw(const void *raw
, size_t size
);
83 struct sockaddr_un name
; ///< same as .msg_name
85 struct iovec ios
[1]; ///< same as .msg_iov[]
88 int type_
; ///< Message kind, uses MessageType values
89 size_t size
; ///< actual raw data size (for sanity checks)
90 char raw
[maxSize
]; ///< buffer with type-specific data
91 } data
; ///< same as .msg_iov[0].iov_base
94 /// control buffer space for one fd
95 char raw
[SQUID_CMSG_SPACE(sizeof(int))];
96 } ctrl
; ///< same as .msg_control
98 /// data offset for the next get/put*() to start with
99 mutable unsigned int offset
;
104 #endif /* SQUID_IPC_TYPED_MSG_HDR_H */