diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-09-22 15:16:37 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-09-22 15:16:37 +0000 |
commit | 3add309d9812e9f964c1434ab34ce90f6f8919e1 (patch) | |
tree | 8864e626bd6d8e2540fe02e676ca7972e6f4e45a /src/microhttpd/mhd_itc.h | |
parent | 9c1254792ca4971b36d06706e9d51c253d96106d (diff) | |
download | libmicrohttpd-3add309d9812e9f964c1434ab34ce90f6f8919e1.tar.gz libmicrohttpd-3add309d9812e9f964c1434ab34ce90f6f8919e1.zip |
-reduce #ifdef mess in mhd_itc.h
Diffstat (limited to 'src/microhttpd/mhd_itc.h')
-rw-r--r-- | src/microhttpd/mhd_itc.h | 185 |
1 files changed, 111 insertions, 74 deletions
diff --git a/src/microhttpd/mhd_itc.h b/src/microhttpd/mhd_itc.h index 7a14dd52..3c2de54c 100644 --- a/src/microhttpd/mhd_itc.h +++ b/src/microhttpd/mhd_itc.h | |||
@@ -39,102 +39,70 @@ | |||
39 | #define MHD_DONT_USE_PIPES 1 | 39 | #define MHD_DONT_USE_PIPES 1 |
40 | #endif /* defined(_WIN32) && !defined(MHD_DONT_USE_PIPES) */ | 40 | #endif /* defined(_WIN32) && !defined(MHD_DONT_USE_PIPES) */ |
41 | 41 | ||
42 | |||
42 | #ifndef MHD_DONT_USE_PIPES | 43 | #ifndef MHD_DONT_USE_PIPES |
44 | |||
45 | /* **************** STANDARD UNIX PIPE implementation ********** */ | ||
46 | |||
43 | # ifdef HAVE_STRING_H | 47 | # ifdef HAVE_STRING_H |
44 | # include <string.h> /* for strerror() */ | 48 | # include <string.h> /* for strerror() */ |
45 | # endif | 49 | # endif |
46 | #else | ||
47 | # include "mhd_sockets.h" | ||
48 | #endif /* MHD_DONT_USE_PIPES */ | ||
49 | 50 | ||
50 | /** | 51 | /** |
51 | * Data type for a MHD pipe. | 52 | * Data type for a MHD pipe. |
52 | */ | 53 | */ |
53 | struct MHD_Pipe | 54 | struct MHD_Pipe |
54 | { | 55 | { |
55 | #ifndef MHD_DONT_USE_PIPES | ||
56 | int fd[2]; | 56 | int fd[2]; |
57 | #else /* ! MHD_DONT_USE_PIPES */ | ||
58 | MHD_socket fd[2]; | ||
59 | #endif /* ! MHD_DONT_USE_PIPES */ | ||
60 | }; | 57 | }; |
61 | 58 | ||
59 | /** | ||
60 | * create pipe | ||
61 | */ | ||
62 | #define MHD_pipe_(pip) (!pipe((pip.fd))) | ||
62 | 63 | ||
63 | /* MHD_pipe_ create pipe (!MHD_DONT_USE_PIPES) / | 64 | /*** |
64 | * create two connected sockets (MHD_DONT_USE_PIPES) */ | 65 | * Get description string of last errno for pipe operations. |
65 | #ifndef MHD_DONT_USE_PIPES | 66 | */ |
66 | # define MHD_pipe_(pip) (!pipe((pip.fd))) | 67 | #define MHD_pipe_last_strerror_() strerror(errno) |
67 | #else /* MHD_DONT_USE_PIPES */ | ||
68 | # define MHD_pipe_(pip) MHD_socket_pair_((pip.fd)) | ||
69 | #endif /* MHD_DONT_USE_PIPES */ | ||
70 | |||
71 | /* MHD_pipe_last_strerror_ is description string of last errno (!MHD_DONT_USE_PIPES) / | ||
72 | * description string of last pipe error (MHD_DONT_USE_PIPES) */ | ||
73 | #ifndef MHD_DONT_USE_PIPES | ||
74 | # define MHD_pipe_last_strerror_() strerror(errno) | ||
75 | #else | ||
76 | # define MHD_pipe_last_strerror_() MHD_socket_last_strerr_() | ||
77 | #endif | ||
78 | |||
79 | /* MHD_pipe_write_ write data to real pipe (!MHD_DONT_USE_PIPES) / | ||
80 | * write data to emulated pipe (MHD_DONT_USE_PIPES) */ | ||
81 | #ifndef MHD_DONT_USE_PIPES | ||
82 | # define MHD_pipe_write_(pip, ptr, sz) write((pip.fd[1]), (const void*)(ptr), (sz)) | ||
83 | #else | ||
84 | # define MHD_pipe_write_(pip, ptr, sz) send((pip.fd[1]), (const char*)(ptr), (sz), 0) | ||
85 | #endif | ||
86 | |||
87 | |||
88 | #ifndef MHD_DONT_USE_PIPES | ||
89 | # define MHD_pipe_get_read_fd_(pip) (pip.fd[0]) | ||
90 | #else | ||
91 | # define MHD_pipe_get_read_fd_(pip) (pip.fd[0]) | ||
92 | #endif | ||
93 | |||
94 | 68 | ||
95 | #ifndef MHD_DONT_USE_PIPES | 69 | /** |
96 | # define MHD_pipe_get_write_fd_(pip) (pip.fd[1]) | 70 | * write data to real pipe |
97 | #else | 71 | */ |
98 | # define MHD_pipe_get_write_fd_(pip) (pip.fd[1]) | 72 | #define MHD_pipe_write_(pip, ptr, sz) write((pip.fd[1]), (const void*)(ptr), (sz)) |
99 | #endif | ||
100 | 73 | ||
74 | #define MHD_pipe_get_read_fd_(pip) (pip.fd[0]) | ||
101 | 75 | ||
76 | #define MHD_pipe_get_write_fd_(pip) (pip.fd[1]) | ||
102 | 77 | ||
103 | /* MHD_pipe_drain_ drain data from real pipe (!MHD_DONT_USE_PIPES) / | 78 | /** |
104 | * drain data from emulated pipe (MHD_DONT_USE_PIPES) */ | 79 | * drain data from real pipe |
105 | #ifndef MHD_DONT_USE_PIPES | 80 | */ |
106 | # define MHD_pipe_drain_(pip) do { long tmp; while (0 < read((pip.fd[0]), (void*)&tmp, sizeof (tmp))) ; } while (0) | 81 | #define MHD_pipe_drain_(pip) do { \ |
107 | #else | 82 | long tmp; \ |
108 | # define MHD_pipe_drain_(pip) do { long tmp; while (0 < recv((pip.fd[0]), (void*)&tmp, sizeof (tmp), 0)) ; } while (0) | 83 | while (0 < read((pip.fd[0]), (void*)&tmp, sizeof (tmp))) ; \ |
109 | #endif | 84 | } while (0) |
110 | 85 | ||
111 | /* MHD_pipe_close_(fd) close any FDs (non-W32) / | 86 | /** |
112 | * close emulated pipe FDs (W32) */ | 87 | * Close any FDs of the pipe (non-W32) |
113 | #ifndef MHD_DONT_USE_PIPES | 88 | */ |
114 | # define MHD_pipe_close_(pip) do { close(pip.fd[0]); close(pip.fd[1]); } while (0) | 89 | #define MHD_pipe_close_(pip) do { \ |
115 | #else | 90 | close (pip.fd[0]); \ |
116 | # define MHD_pipe_close_(fd) do { MHD_socket_close_(pip.fd[0]); MHD_socket_close_(pip.fd[1]); } while (0) | 91 | close (pip.fd[1]); \ |
117 | #endif | 92 | } while (0) |
118 | 93 | ||
119 | /* MHD_INVALID_PIPE_ is a value of bad pipe FD */ | 94 | /** |
120 | #ifndef MHD_DONT_USE_PIPES | 95 | * Check if we have an uninitialized pipe |
121 | # define MHD_INVALID_PIPE_(pip) (-1 == pip.fd[0]) | 96 | */ |
122 | #else | 97 | #define MHD_INVALID_PIPE_(pip) (-1 == pip.fd[0]) |
123 | # define MHD_INVALID_PIPE_(pip) (MHD_INVALID_SOCKET == pip.fd[0]) | ||
124 | #endif | ||
125 | 98 | ||
126 | #ifndef MHD_DONT_USE_PIPES | 99 | /** |
100 | * Setup uninitialized @a pip data structure. | ||
101 | */ | ||
127 | #define MHD_make_invalid_pipe_(pip) do { \ | 102 | #define MHD_make_invalid_pipe_(pip) do { \ |
128 | pip.fd[0] = pip.fd[1] = -1; \ | 103 | pip.fd[0] = pip.fd[1] = -1; \ |
129 | } while (0) | 104 | } while (0) |
130 | #else | ||
131 | #define MHD_make_invalid_pipe_(pip) do { \ | ||
132 | pip.fd[0] = pip.fd[1] = MHD_INVALID_SOCKET; \ | ||
133 | } while (0) | ||
134 | #endif | ||
135 | |||
136 | 105 | ||
137 | #ifndef MHD_DONT_USE_PIPES | ||
138 | /** | 106 | /** |
139 | * Change itc FD options to be non-blocking. | 107 | * Change itc FD options to be non-blocking. |
140 | * | 108 | * |
@@ -143,8 +111,77 @@ struct MHD_Pipe | |||
143 | */ | 111 | */ |
144 | int | 112 | int |
145 | MHD_itc_nonblocking_ (struct MHD_Pipe fd); | 113 | MHD_itc_nonblocking_ (struct MHD_Pipe fd); |
146 | #else | 114 | |
147 | # define MHD_itc_nonblocking_(pip) (MHD_socket_nonblocking_((pip.fd[0])) && MHD_socket_nonblocking_((pip.fd[1]))) | 115 | |
148 | #endif | 116 | /* **************** END OF STANDARD UNIX PIPE implementation ********** */ |
117 | |||
118 | #else /* MHD_DONT_USE_PIPES */ | ||
119 | |||
120 | /* **************** PIPE EMULATION by socket pairs ********** */ | ||
121 | |||
122 | #include "mhd_sockets.h" | ||
123 | |||
124 | /** | ||
125 | * Data type for a MHD pipe. | ||
126 | */ | ||
127 | struct MHD_Pipe | ||
128 | { | ||
129 | MHD_socket fd[2]; | ||
130 | }; | ||
131 | |||
132 | |||
133 | /** | ||
134 | * Create two connected sockets to emulate a pipe. | ||
135 | */ | ||
136 | #define MHD_pipe_(pip) MHD_socket_pair_((pip.fd)) | ||
137 | |||
138 | /** | ||
139 | * Get description string of last pipe error | ||
140 | */ | ||
141 | #define MHD_pipe_last_strerror_() MHD_socket_last_strerr_() | ||
142 | |||
143 | /** | ||
144 | * Write data to emulated pipe | ||
145 | */ | ||
146 | #define MHD_pipe_write_(pip, ptr, sz) send((pip.fd[1]), (const char*)(ptr), (sz), 0) | ||
147 | |||
148 | #define MHD_pipe_get_read_fd_(pip) (pip.fd[0]) | ||
149 | |||
150 | #define MHD_pipe_get_write_fd_(pip) (pip.fd[1]) | ||
151 | |||
152 | /** | ||
153 | * Drain data from emulated pipe | ||
154 | */ | ||
155 | #define MHD_pipe_drain_(pip) do { long tmp; while (0 < recv((pip.fd[0]), (void*)&tmp, sizeof (tmp), 0)) ; } while (0) | ||
156 | |||
157 | |||
158 | /** | ||
159 | * Close emulated pipe FDs | ||
160 | */ | ||
161 | #define MHD_pipe_close_(fd) do { \ | ||
162 | MHD_socket_close_(pip.fd[0]); \ | ||
163 | MHD_socket_close_(pip.fd[1]); \ | ||
164 | } while (0) | ||
165 | |||
166 | /** | ||
167 | * Check for uninitialized pipe @a pip | ||
168 | */ | ||
169 | #define MHD_INVALID_PIPE_(pip) (MHD_INVALID_SOCKET == pip.fd[0]) | ||
170 | |||
171 | /** | ||
172 | * Setup uninitialized @a pip data structure. | ||
173 | */ | ||
174 | #define MHD_make_invalid_pipe_(pip) do { \ | ||
175 | pip.fd[0] = pip.fd[1] = MHD_INVALID_SOCKET; \ | ||
176 | } while (0) | ||
177 | |||
178 | |||
179 | #define MHD_itc_nonblocking_(pip) (MHD_socket_nonblocking_((pip.fd[0])) && MHD_socket_nonblocking_((pip.fd[1]))) | ||
180 | |||
181 | /* **************** END OF PIPE EMULATION by socket pairs ********** */ | ||
182 | |||
183 | #endif /* MHD_DONT_USE_PIPES */ | ||
184 | |||
185 | |||
149 | 186 | ||
150 | #endif /* MHD_ITC_H */ | 187 | #endif /* MHD_ITC_H */ |