socketd_internal.H 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * config.hpp
  3. *
  4. * Created on: 2011-05-20
  5. * Author: xaxaxa
  6. */
  7. /*
  8. This program is free software: you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation, either version 3 of the License, or
  11. (at your option) any later version.
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. GNU General Public License for more details.
  16. You should have received a copy of the GNU General Public License
  17. along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. * */
  19. #ifndef CONFIG_HPP_
  20. #define CONFIG_HPP_
  21. #include <cpoll/cpoll.H>
  22. #include <rgc.H>
  23. #include <string>
  24. #include <map>
  25. #include <vector>
  26. #include "socketd.H"
  27. #include <delegate.H>
  28. #include <unistd.h>
  29. using namespace std;
  30. namespace socketd
  31. {
  32. class SocketDException: public std::exception
  33. {
  34. public:
  35. string message;
  36. int32_t number;
  37. SocketDException();
  38. SocketDException(int32_t number);
  39. SocketDException(string message, int32_t number = 0);
  40. ~SocketDException() throw ();
  41. const char* what() const throw ();
  42. };
  43. struct binding;
  44. struct vhost;
  45. struct listen
  46. {
  47. public:
  48. //user supplied fields
  49. //RGC::Ref<CP::EndPoint> ep;
  50. string host;
  51. string port;
  52. int id;
  53. int backlog;
  54. //internal fields
  55. vector<CP::HANDLE> socks;
  56. int d,t,p;
  57. listen(string host, string port, int id, int backlog = 32) :
  58. host(host), port(port), id(id), backlog(backlog) {
  59. }
  60. listen() :
  61. backlog(32) {
  62. }
  63. };
  64. struct binding
  65. {
  66. public:
  67. //user supplied fields
  68. string httpPath; //path must not have / at the end!!!!!
  69. string httpHost;
  70. string vhostName; //only needs to be filled if put in the "extraBindings" array
  71. int listenID;
  72. //bitmap:
  73. //0: match listenID
  74. //1: match httpPath
  75. //2: match httpHost
  76. int matchLevel;
  77. enum
  78. {
  79. match_listenID = 1, match_httpPath = 2, match_httpHost = 4
  80. };
  81. binding() {
  82. }
  83. binding(int listenID, string httpPath, string httpHost, int matchLevel) :
  84. httpPath(httpPath), httpHost(httpHost), listenID(listenID), matchLevel(matchLevel) {
  85. }
  86. //internal fields
  87. vhost* vh;
  88. };
  89. struct appConnection: public RGC::Object
  90. {
  91. // sock success
  92. typedef Delegate<void(bool)> passConnCB;
  93. appConnection();
  94. virtual void shutDown()=0;
  95. //return values: 0: success; 1: failed; 2: in progress (passConnCB() will be called later)
  96. virtual int passConnection(CP::Socket* s, void* buffer, int buflen, const passConnCB& cb)=0;
  97. virtual ~appConnection();
  98. };
  99. struct vhost: public RGC::Object
  100. {
  101. public:
  102. //user supplied fields
  103. vector<binding> bindings;
  104. string name;
  105. string exepath; //leave blank (length==0) to disable execing and just wait for attachment
  106. /*int requestsPerProcess;
  107. int maxRequestsPerProcess;
  108. int maxProcesses;*/
  109. int processes; //how many processes to spawn (for load balancing)
  110. //at runtime, the # of processes will be rounded up to the nearest multiple
  111. //of the # of socketd threads
  112. int ipcBufSize; //set to 0 to use system default, -1 to use global settings
  113. //for attaching to a running vhost; arbitrary string; leave blank (length==0) to
  114. //disable attachments
  115. string authCookie;
  116. //whether to LD_PRELOAD socketd_proxy.so
  117. bool preload;
  118. bool useShell;
  119. vhost() :
  120. processes(1), ipcBufSize(-1), preload(false), useShell(true) {
  121. }
  122. vhost(const vector<binding>& bindings, string name, string exepath, string authCookie,
  123. bool preload = false, bool shell = true, int processes = 1) :
  124. bindings(bindings), name(name), exepath(exepath), processes(processes),
  125. authCookie(authCookie), preload(preload), useShell(shell) {
  126. }
  127. //internal fields
  128. //vector<int> conns_i; //indexed by thread*processes + curProcess
  129. RGC::Ref<appConnection> attachmentConn; //readonly by threads
  130. //vector<int> curProcess_i;
  131. int _ipcBufSize;
  132. int _processes; //processes per thread
  133. vector<uint8_t*> perCPUData;
  134. bool hasAttachments;
  135. };
  136. static const char* socketd_proxy = "socketd_proxy.so";
  137. class socketd
  138. {
  139. public:
  140. //user supplied fields
  141. vector<listen> listens;
  142. vector<vhost> vhosts;
  143. vector<binding> extraBindings; //vhostName must be filled for these
  144. string unixAddress;
  145. uint8_t** perCPUData;
  146. //format of per-cpu data structures:
  147. //struct {
  148. // appConnection* conns[processes];
  149. // int curProcess;
  150. // int padding;
  151. //} vhostInfo[vhosts];
  152. int ipcBufSize; //<=0 to use system default
  153. int threads;
  154. void run();
  155. //internal fields
  156. vector<binding*> bindings;
  157. socketd() :
  158. ipcBufSize(0), threads((int) sysconf(_SC_NPROCESSORS_CONF)) {
  159. }
  160. };
  161. }
  162. #endif /* CONFIG_HPP_ */