PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/game_server/include/lib/process.php

http://github.com/ericmuyser/mmo
PHP | 548 lines | 410 code | 130 blank | 8 comment | 34 complexity | bf2bf26885d7b411f156f46a792b65b3 MD5 | raw file
  1. <?php
  2. class process_master
  3. {
  4. public function __construct($file)
  5. {
  6. $php_command = "php5";
  7. $busy = false;
  8. $descriptor = array(0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("pipe", "w"));
  9. $this->proc = proc_open($php_command . " -q " . $file, $descriptor, $this->pipes);
  10. stream_set_blocking($this->pipes[1], false);
  11. }
  12. public function __destruct()
  13. {
  14. $this->quit();
  15. }
  16. public function is_active()
  17. {
  18. if(!$this->proc)
  19. return false;
  20. $f = stream_get_meta_data($this->pipes[1]);
  21. return !$f['eof'];
  22. }
  23. public function send($command, $data = NULL, $callback = NULL)
  24. {
  25. if(!$this->is_active())
  26. return -1;
  27. $msg = $command . "|" . base64_encode(serialize($data)) . "|" . base64_encode(serialize($callback)) . "\n";
  28. fwrite($this->pipes[0], $msg);
  29. return 1;
  30. }
  31. public function get_messages()
  32. {
  33. return $this->process_input($this->receive_input());
  34. }
  35. public function receive_input()
  36. {
  37. $buffer = '';
  38. while($r = fgets($this->pipes[1]))
  39. $buffer .= $r;
  40. return $buffer;
  41. }
  42. public function process_input($input_list)
  43. {
  44. $message_list = array();
  45. if(!$input_list)
  46. return $message_list;
  47. $input_list = explode("\n", $input_list);
  48. foreach($input_list as $input)
  49. {
  50. if(!$input)
  51. continue;
  52. $parts = explode("|", $input);
  53. if(count($parts) == 3)
  54. {
  55. $status = intval($parts[0]);
  56. $data = unserialize(base64_decode($parts[1]));
  57. $callback = unserialize(base64_decode($parts[2]));
  58. }
  59. else
  60. {
  61. $status = 4;
  62. $data = $input;
  63. $callback = NULL;
  64. }
  65. $message_list[] = array("status" => $status, "data" => $data, "callback" => $callback);
  66. }
  67. return $message_list;
  68. }
  69. public function quit()
  70. {
  71. if(!$this->proc)
  72. return;
  73. $this->send(2);
  74. proc_close($this->proc);
  75. $this->proc = NULL;
  76. posix_kill($this->child_pid, 9); // just incase
  77. }
  78. public $child_pid;
  79. private $proc;
  80. private $pipes;
  81. public $busy;
  82. }
  83. class process_slave
  84. {
  85. public function __construct()
  86. {
  87. $this->parent_pid = posix_getpid();
  88. $this->stdin = fopen("php://stdin", "r");
  89. stream_set_blocking($this->stdin, false);
  90. $this->send(1, posix_getpid());
  91. }
  92. public function send($status, $data = NULL, $callback = NULL)
  93. {
  94. if(!$this->is_active())
  95. return -1;
  96. $msg = $status . "|" . base64_encode(serialize($data)) . "|" . base64_encode(serialize($callback)) . "\n";
  97. echo $msg;
  98. return 1;
  99. }
  100. public function get_messages()
  101. {
  102. return $this->process_input($this->receive_input());
  103. }
  104. public function receive_input($wait = true)
  105. {
  106. $buffer = '';
  107. if($wait)
  108. while($r = fgets($this->stdin))
  109. $buffer .= $r;
  110. else
  111. $buffer = fgets($this->stdin);
  112. return $buffer;
  113. }
  114. public function process_input($input_list)
  115. {
  116. $message_list = array();
  117. if(!$input_list)
  118. return $message_list;
  119. $input_list = explode("\n", $input_list);
  120. foreach($input_list as $input)
  121. {
  122. if(!$input)
  123. continue;
  124. $parts = explode("|", $input);
  125. if(count($parts) != 3)
  126. continue;
  127. $status = intval($parts[0]);
  128. $data = unserialize(base64_decode($parts[1]));
  129. $callback = unserialize(base64_decode($parts[2]));
  130. $message_list[] = array("status" => $status, "data" => $data, "callback" => $callback);
  131. }
  132. return $message_list;
  133. }
  134. public function main($message_list)
  135. {
  136. foreach($message_list as $message)
  137. {
  138. $status = $message['status'];
  139. $data = $message['data'];
  140. $callback = $message['callback'];
  141. switch($status)
  142. {
  143. case 1:
  144. $this->parent_pid = $data;
  145. break;
  146. case 2:
  147. exit(0);
  148. break;
  149. case 3:
  150. $result = $data();
  151. $this->send(3, $result, $callback);
  152. break;
  153. }
  154. }
  155. }
  156. public function is_active()
  157. {
  158. // if this isn't linux don't check it
  159. if(!stristr(PHP_OS, "linux"))
  160. return true;
  161. $loads = @exec("uptime");
  162. preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/", $loads, $avgs);
  163. $cpu = intval($avgs[1]);
  164. if($cpu > 80)
  165. return false;
  166. return (posix_kill($this->parent_pid, 0) != false);
  167. }
  168. public function quit()
  169. {
  170. exit;
  171. }
  172. public function debug($data)
  173. {
  174. fwrite($this->stderr, $data);
  175. }
  176. protected $stdin;
  177. protected $stdout;
  178. protected $parent_pid;
  179. protected $last_callback;
  180. }
  181. class resource_service
  182. {
  183. public static function register(&$resource)
  184. {
  185. $id = rand(0, 32768);
  186. while(isset(self::$registry[$id]))
  187. $id = rand(0, 32768);
  188. self::$registry[$id] = &$resource;
  189. return $id;
  190. }
  191. public static function unregister($id)
  192. {
  193. $resource = &self::$registry[$id];
  194. unset(self::$registry[$id]);
  195. return $resource;
  196. }
  197. private static $registry;
  198. }
  199. class process_callback
  200. {
  201. public function __construct(Closure $closure)
  202. {
  203. $this->closure = $closure;
  204. }
  205. public function __invoke()
  206. {
  207. return call_user_func_array($this->closure, func_get_args());
  208. }
  209. public function __sleep()
  210. {
  211. if($this->closure)
  212. $this->resource_id = resource_service::register($this->closure);
  213. return array("resource_id");
  214. }
  215. public function __wakeup()
  216. {
  217. if($closure = resource_service::unregister($this->resource_id))
  218. $this->closure = &$closure;
  219. }
  220. private $closure;
  221. }
  222. class process_request
  223. {
  224. public function __construct($function)
  225. {
  226. if ( ! $function instanceOf Closure)
  227. throw new InvalidArgumentException();
  228. $this->closure = $function;
  229. $this->reflection = new ReflectionFunction($function);
  230. $this->code = $this->_fetchCode();
  231. $this->used_variables = $this->_fetchUsedVariables();
  232. }
  233. public function __invoke()
  234. {
  235. $args = func_get_args();
  236. return $this->reflection->invokeArgs($args);
  237. }
  238. public function getClosure()
  239. {
  240. return $this->closure;
  241. }
  242. protected function _fetchCode()
  243. {
  244. // Open file and seek to the first line of the closure
  245. $file = new SplFileObject($this->reflection->getFileName());
  246. $file->seek($this->reflection->getStartLine()-1);
  247. // Retrieve all of the lines that contain code for the closure
  248. $code = '';
  249. while ($file->key() < $this->reflection->getEndLine())
  250. {
  251. $code .= $file->current();
  252. $file->next();
  253. }
  254. // Only keep the code defining that closure
  255. $begin = strpos($code, 'function');
  256. $end = strrpos($code, '}');
  257. $code = substr($code, $begin, $end - $begin + 1);
  258. return $code;
  259. }
  260. public function getCode()
  261. {
  262. return $this->code;
  263. }
  264. public function getParameters()
  265. {
  266. return $this->reflection->getParameters();
  267. }
  268. protected function _fetchUsedVariables()
  269. {
  270. // Make sure the use construct is actually used
  271. $use_index = stripos($this->code, 'use');
  272. if ( ! $use_index)
  273. return array();
  274. // Get the names of the variables inside the use statement
  275. $begin = strpos($this->code, '(', $use_index) + 1;
  276. $end = strpos($this->code, ')', $begin);
  277. $vars = explode(',', substr($this->code, $begin, $end - $begin));
  278. // Get the static variables of the function via reflection
  279. $static_vars = $this->reflection->getStaticVariables();
  280. // Only keep the variables that appeared in both sets
  281. $used_vars = array();
  282. foreach ($vars as $var)
  283. {
  284. $var = trim($var, ' $&amp;');
  285. if(isset($var) && isset($static_vars[$var]))
  286. $used_vars[$var] = $static_vars[$var];
  287. }
  288. return $used_vars;
  289. }
  290. public function getUsedVariables()
  291. {
  292. return $this->used_variables;
  293. }
  294. public function __sleep()
  295. {
  296. return array('code', 'used_variables');
  297. }
  298. public function __wakeup()
  299. {
  300. extract($this->used_variables);
  301. eval('$_function = '.$this->code.';');
  302. if (isset($_function) AND $_function instanceOf Closure)
  303. {
  304. $this->closure = $_function;
  305. $this->reflection = new ReflectionFunction($_function);
  306. }
  307. else
  308. throw new Exception();
  309. }
  310. protected $closure = NULL;
  311. protected $reflection = NULL;
  312. protected $code = NULL;
  313. protected $used_variables = array();
  314. }
  315. class process_service
  316. {
  317. public function __construct()
  318. {
  319. $this->process_list = array();
  320. }
  321. public function total_active()
  322. {
  323. $total = 0;
  324. foreach($this->process_list as $process)
  325. {
  326. if($process->is_active())
  327. ++$total;
  328. }
  329. return $total;
  330. }
  331. public function run($request, $callback)
  332. {
  333. $process = NULL;
  334. foreach($this->process_list as $p2)
  335. {
  336. if(!$p2->busy)
  337. {
  338. $process = $p2;
  339. break;
  340. }
  341. }
  342. if(!$process)
  343. {
  344. $process = new process_master(__FILE__ . " run_process");
  345. $this->process_list[] = $process;
  346. }
  347. $process->busy = true;
  348. $process->send(3, new process_request($request), new process_callback(function($data) use(&$process, &$callback)
  349. {
  350. $process->busy = false;
  351. if($callback)
  352. return $callback($data);
  353. }));
  354. }
  355. public function remove_process($k)
  356. {
  357. $process = array_splice($this->process_list, $k, 1);
  358. $process = $process[0];
  359. $process->quit();
  360. }
  361. public function update()
  362. {
  363. foreach($this->process_list as $key => $process)
  364. {
  365. if($r1 = $process->get_messages())
  366. {
  367. foreach($r1 as $r2)
  368. {
  369. switch($r2['status'])
  370. {
  371. case 1:
  372. $process->child_pid = $r2['data'];
  373. $process->send(1, posix_getpid());
  374. break;
  375. case 3:
  376. if($r2['callback'])
  377. if($r2['callback']($r2['data']))
  378. $this->remove_process($key);
  379. break;
  380. case 4:
  381. echo "Process encountered errors: " . $r2['data'] . "\n";
  382. if($r2['callback'])
  383. if($r2['callback']($r2['data']))
  384. $this->remove_process($key);
  385. break;
  386. }
  387. }
  388. }
  389. }
  390. }
  391. public $process_list;
  392. }
  393. if(in_array("run_process", $argv))
  394. {
  395. ini_set('display_errors', 1);
  396. error_reporting(E_ALL);
  397. $process = new process_slave();
  398. function errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
  399. {
  400. global $process;
  401. $result = $errfile . " on line " . $errline . ": " . $errstr;
  402. $process->send(4, $result);
  403. }
  404. set_error_handler('errorHandler');
  405. function exc_handler($exception)
  406. {
  407. $result = $exception->getMessage();
  408. $process->send(4, $result);
  409. }
  410. set_exception_handler('exc_handler');
  411. do
  412. {
  413. sleep(1);
  414. $process->main($process->get_messages());
  415. }
  416. while($process->is_active());
  417. }
  418. ?>