codemod 2010-2016 to 2010-present
[hiphop-php.git] / hphp / util / afdt-util.h
blob407e42e7752b0c8cd775315abdc119d7bed14cb0
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_AFDT_UTIL_H_
18 #define incl_HPHP_AFDT_UTIL_H_
20 #ifdef _MSC_VER
21 # error AFDT is not currently supported for MSVC!
22 #endif
24 #include <sys/socket.h>
25 #include <vector>
26 #include <string>
27 #include <stdexcept>
28 #include <cstring>
30 namespace HPHP {
31 namespace afdt {
32 ///////////////////////////////////////////////////////////////////////////////
34 namespace detail {
36 typedef void (*SrFunc)(int afdt_fd, std::vector<iovec>& iov);
38 void sappend(int afdt_fd,
39 std::vector<iovec>& iov, const void* addr, size_t size);
40 void rappend(int afdt_fd,
41 std::vector<iovec>& iov, void* addr, size_t size);
43 template<class T>
44 typename std::enable_if<std::is_fundamental<T>::value, void>::type
45 sappend(int afdt_fd, std::vector<iovec>& iov, const T& elm) {
46 sappend(afdt_fd, iov, &elm, sizeof elm);
49 template<class T>
50 typename std::enable_if<std::is_fundamental<T>::value, void>::type
51 rappend(int afdt_fd, std::vector<iovec>& iov, T* elm) {
52 rappend(afdt_fd, iov, elm, sizeof *elm);
55 void send(int afdt_fd, std::vector<iovec>& iov);
57 template<class... Tail>
58 void send(int afdt_fd, std::vector<iovec>& iov,
59 const std::vector<std::string>& h, Tail&&... args);
61 template<class... Tail>
62 void send(int afdt_fd, std::vector<iovec>& iov,
63 const std::string& h, Tail&&... args);
65 template<class... Tail>
66 void send(int afdt_fd, std::vector<iovec>& iov,
67 const char* h, Tail&&... args);
69 template<class T, class... Tail>
70 typename std::enable_if<std::is_fundamental<T>::value, void>::type
71 send(int afdt_fd, std::vector<iovec>& iov,
72 const std::vector<T>& h, Tail&&... args);
74 template<class... Tail>
75 void send(int afdt_fd, std::vector<iovec>& iov,
76 const std::vector<std::string>& h, Tail&&... args);
79 template<class Head, class... Tail>
80 void send(int afdt_fd, std::vector<iovec>& iov,
81 const Head& h, Tail&&... args) {
82 sappend(afdt_fd, iov, h);
83 send(afdt_fd, iov, std::forward<Tail>(args)...);
86 template<class... Tail>
87 void send(int afdt_fd, std::vector<iovec>& iov,
88 const std::string& h, Tail&&... args) {
89 size_t s = h.size();
90 sappend(afdt_fd, iov, s);
91 sappend(afdt_fd, iov, &h[0], s);
92 send(afdt_fd, iov, std::forward<Tail>(args)...);
95 template<class... Tail>
96 void send(int afdt_fd, std::vector<iovec>& iov,
97 const char* h, Tail&&... args) {
98 size_t s = std::strlen(h);
99 sappend(afdt_fd, iov, s);
100 sappend(afdt_fd, iov, &h[0], s);
101 send(afdt_fd, iov, std::forward<Tail>(args)...);
104 template<class T, class... Tail>
105 typename std::enable_if<std::is_fundamental<T>::value, void>::type
106 send(int afdt_fd, std::vector<iovec>& iov,
107 const std::vector<T>& h, Tail&&... args) {
108 size_t s = h.size();
109 sappend(afdt_fd, iov, s);
110 sappend(afdt_fd, iov, &h[0], s * sizeof(T));
111 send(afdt_fd, iov, std::forward<Tail>(args)...);
114 template<class... Tail>
115 void send(int afdt_fd, std::vector<iovec>& iov,
116 const std::vector<std::string>& h, Tail&&... args) {
117 size_t s = h.size();
118 sappend(afdt_fd, iov, s);
119 std::vector<size_t> sizes;
120 sizes.reserve(s);
121 for (auto& e : h) {
122 auto strsz = e.size();
123 sizes.push_back(strsz);
124 sappend(afdt_fd, iov, sizes.back());
125 sappend(afdt_fd, iov, &e[0], strsz);
127 send(afdt_fd, iov, std::forward<Tail>(args)...);
130 void recv(int afdt_fd, std::vector<iovec>& iov);
132 template<class... Tail>
133 void recv(int afdt_fd, std::vector<iovec>& iov,
134 std::string& h, Tail&... args);
136 template<class T, class... Tail>
137 typename std::enable_if<std::is_fundamental<T>::value, void>::type
138 recv(int afdt_fd, std::vector<iovec>& iov,
139 std::vector<T>& h, Tail&... args);
141 template<class... Tail>
142 void recv(int afdt_fd, std::vector<iovec>& iov,
143 std::vector<std::string>& h, Tail&... args);
146 template<class Head, class... Tail>
147 void recv(int afdt_fd, std::vector<iovec>& iov,
148 Head& h, Tail&... args) {
149 rappend(afdt_fd, iov, &h);
150 recv(afdt_fd, iov, args...);
153 template<class... Tail>
154 void recv(int afdt_fd, std::vector<iovec>& iov,
155 std::string& h, Tail&... args) {
156 size_t sz;
157 rappend(afdt_fd, iov, &sz);
158 recv(afdt_fd, iov);
159 iov.clear();
160 h.resize(sz);
161 rappend(afdt_fd, iov, &h[0], sz);
162 recv(afdt_fd, iov, args...);
165 template<class T, class... Tail>
166 typename std::enable_if<std::is_fundamental<T>::value, void>::type
167 recv(int afdt_fd, std::vector<iovec>& iov,
168 std::vector<T>& h, Tail&... args) {
169 size_t sz;
170 rappend(afdt_fd, iov, &sz);
171 recv(afdt_fd, iov);
172 iov.clear();
173 h.resize(sz);
174 rappend(afdt_fd, iov, &h[0], sz * sizeof(h[0]));
175 recv(afdt_fd, iov, args...);
178 template<class... Tail>
179 void recv(int afdt_fd, std::vector<iovec>& iov,
180 std::vector<std::string>& h, Tail&... args) {
181 size_t sz;
182 rappend(afdt_fd, iov, &sz);
183 recv(afdt_fd, iov);
184 iov.clear();
185 h.resize(sz);
186 for (auto& e : h) {
187 size_t strsz;
188 rappend(afdt_fd, iov, &strsz);
189 recv(afdt_fd, iov);
190 iov.clear();
191 e.resize(strsz);
192 rappend(afdt_fd, iov, &e[0], strsz);
194 recv(afdt_fd, iov, args...);
199 template<class Head, class... Tail>
200 int sendRaw(int afdt_fd, const Head& h, Tail&&... args) {
201 std::vector<iovec> iov;
202 try {
203 detail::send(afdt_fd, iov, h, std::forward<Tail>(args)...);
204 return 0;
205 } catch (const std::runtime_error& e) {
206 return -1;
210 template<class Head, class... Tail>
211 int recvRaw(int afdt_fd, Head& h, Tail&... args) {
212 std::vector<iovec> iov;
213 try {
214 detail::recv(afdt_fd, iov, h, args...);
215 return 0;
216 } catch (const std::runtime_error& e) {
217 return -1;
221 bool send_fd(int afdt_fd, int fd);
222 int recv_fd(int afdt_fd);
224 ///////////////////////////////////////////////////////////////////////////////
227 #endif