FILENAME 3/4 - delete the old get_fun_path etc.
[hiphop-php.git] / hphp / util / afdt-util.h
blobe4616ac5ca7a8114f7d484b7c46d7b3aa098a529
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 #pragma once
19 #ifdef _MSC_VER
20 # error AFDT is not currently supported for MSVC!
21 #endif
23 #include <sys/socket.h>
24 #include <vector>
25 #include <string>
26 #include <stdexcept>
27 #include <cstring>
29 namespace HPHP {
30 namespace afdt {
31 ///////////////////////////////////////////////////////////////////////////////
33 namespace detail {
35 typedef void (*SrFunc)(int afdt_fd, std::vector<iovec>& iov);
37 void sappend(int afdt_fd,
38 std::vector<iovec>& iov, const void* addr, size_t size);
39 void rappend(int afdt_fd,
40 std::vector<iovec>& iov, void* addr, size_t size);
42 template<class T>
43 typename std::enable_if<
44 std::is_fundamental<T>::value || std::is_pod<T>::value,
45 void
46 >::type sappend(int afdt_fd, std::vector<iovec>& iov, const T& elm) {
47 sappend(afdt_fd, iov, &elm, sizeof elm);
50 template<class T>
51 typename std::enable_if<
52 std::is_fundamental<T>::value || std::is_pod<T>::value,
53 void
54 >::type rappend(int afdt_fd, std::vector<iovec>& iov, T* elm) {
55 rappend(afdt_fd, iov, elm, sizeof *elm);
58 void send(int afdt_fd, std::vector<iovec>& iov);
60 template<class... Tail>
61 void send(int afdt_fd, std::vector<iovec>& iov,
62 const std::vector<std::string>& h, Tail&&... args);
64 template<class... Tail>
65 void send(int afdt_fd, std::vector<iovec>& iov,
66 const std::string& h, Tail&&... args);
68 template<class... Tail>
69 void send(int afdt_fd, std::vector<iovec>& iov,
70 const char* h, Tail&&... args);
72 template<class T, class... Tail>
73 typename std::enable_if<std::is_fundamental<T>::value, void>::type
74 send(int afdt_fd, std::vector<iovec>& iov,
75 const std::vector<T>& h, Tail&&... args);
77 template<class... Tail>
78 void send(int afdt_fd, std::vector<iovec>& iov,
79 const std::vector<std::string>& h, Tail&&... args);
82 template<class Head, class... Tail>
83 void send(int afdt_fd, std::vector<iovec>& iov,
84 const Head& h, Tail&&... args) {
85 sappend(afdt_fd, iov, h);
86 send(afdt_fd, iov, std::forward<Tail>(args)...);
89 template<class... Tail>
90 void send(int afdt_fd, std::vector<iovec>& iov,
91 const std::string& h, Tail&&... args) {
92 size_t s = h.size();
93 sappend(afdt_fd, iov, s);
94 sappend(afdt_fd, iov, &h[0], s);
95 send(afdt_fd, iov, std::forward<Tail>(args)...);
98 template<class... Tail>
99 void send(int afdt_fd, std::vector<iovec>& iov,
100 const char* h, Tail&&... args) {
101 size_t s = std::strlen(h);
102 sappend(afdt_fd, iov, s);
103 sappend(afdt_fd, iov, &h[0], s);
104 send(afdt_fd, iov, std::forward<Tail>(args)...);
107 template<class T, class... Tail>
108 typename std::enable_if<std::is_fundamental<T>::value, void>::type
109 send(int afdt_fd, std::vector<iovec>& iov,
110 const std::vector<T>& h, Tail&&... args) {
111 size_t s = h.size();
112 sappend(afdt_fd, iov, s);
113 sappend(afdt_fd, iov, &h[0], s * sizeof(T));
114 send(afdt_fd, iov, std::forward<Tail>(args)...);
117 template<class... Tail>
118 void send(int afdt_fd, std::vector<iovec>& iov,
119 const std::vector<std::string>& h, Tail&&... args) {
120 size_t s = h.size();
121 sappend(afdt_fd, iov, s);
122 std::vector<size_t> sizes;
123 sizes.reserve(s);
124 for (auto& e : h) {
125 auto strsz = e.size();
126 sizes.push_back(strsz);
127 sappend(afdt_fd, iov, sizes.back());
128 sappend(afdt_fd, iov, &e[0], strsz);
130 send(afdt_fd, iov, std::forward<Tail>(args)...);
133 void recv(int afdt_fd, std::vector<iovec>& iov);
135 template<class... Tail>
136 void recv(int afdt_fd, std::vector<iovec>& iov,
137 std::string& h, Tail&... args);
139 template<class T, class... Tail>
140 typename std::enable_if<std::is_fundamental<T>::value, void>::type
141 recv(int afdt_fd, std::vector<iovec>& iov,
142 std::vector<T>& h, Tail&... args);
144 template<class... Tail>
145 void recv(int afdt_fd, std::vector<iovec>& iov,
146 std::vector<std::string>& h, Tail&... args);
149 template<class Head, class... Tail>
150 void recv(int afdt_fd, std::vector<iovec>& iov,
151 Head& h, Tail&... args) {
152 rappend(afdt_fd, iov, &h);
153 recv(afdt_fd, iov, args...);
156 template<class... Tail>
157 void recv(int afdt_fd, std::vector<iovec>& iov,
158 std::string& h, Tail&... args) {
159 size_t sz;
160 rappend(afdt_fd, iov, &sz);
161 recv(afdt_fd, iov);
162 iov.clear();
163 h.resize(sz);
164 rappend(afdt_fd, iov, &h[0], sz);
165 recv(afdt_fd, iov, args...);
168 template<class T, class... Tail>
169 typename std::enable_if<std::is_fundamental<T>::value, void>::type
170 recv(int afdt_fd, std::vector<iovec>& iov,
171 std::vector<T>& h, Tail&... args) {
172 size_t sz;
173 rappend(afdt_fd, iov, &sz);
174 recv(afdt_fd, iov);
175 iov.clear();
176 h.resize(sz);
177 rappend(afdt_fd, iov, &h[0], sz * sizeof(h[0]));
178 recv(afdt_fd, iov, args...);
181 template<class... Tail>
182 void recv(int afdt_fd, std::vector<iovec>& iov,
183 std::vector<std::string>& h, Tail&... args) {
184 size_t sz;
185 rappend(afdt_fd, iov, &sz);
186 recv(afdt_fd, iov);
187 iov.clear();
188 h.resize(sz);
189 for (auto& e : h) {
190 size_t strsz;
191 rappend(afdt_fd, iov, &strsz);
192 recv(afdt_fd, iov);
193 iov.clear();
194 e.resize(strsz);
195 rappend(afdt_fd, iov, &e[0], strsz);
197 recv(afdt_fd, iov, args...);
202 template<class Head, class... Tail>
203 void sendx(int afdt_fd, const Head& h, Tail&&... args) {
204 std::vector<iovec> iov;
205 detail::send(afdt_fd, iov, h, std::forward<Tail>(args)...);
208 template<class Head, class... Tail>
209 void recvx(int afdt_fd, Head& h, Tail&... args) {
210 std::vector<iovec> iov;
211 detail::recv(afdt_fd, iov, h, args...);
214 template<class Head, class... Tail>
215 int sendRaw(int afdt_fd, const Head& h, Tail&&... args) {
216 std::vector<iovec> iov;
217 try {
218 detail::send(afdt_fd, iov, h, std::forward<Tail>(args)...);
219 return 0;
220 } catch (const std::runtime_error& e) {
221 return -1;
225 template<class Head, class... Tail>
226 int recvRaw(int afdt_fd, Head& h, Tail&... args) {
227 std::vector<iovec> iov;
228 try {
229 detail::recv(afdt_fd, iov, h, args...);
230 return 0;
231 } catch (const std::runtime_error& e) {
232 return -1;
236 bool send_fd(int afdt_fd, int fd);
237 int recv_fd(int afdt_fd);
239 ///////////////////////////////////////////////////////////////////////////////