proxy_sync.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. class ProxyServer
  3. {
  4. protected $clients;
  5. protected $backends;
  6. protected $serv;
  7. function run()
  8. {
  9. $serv = new swoole_server("127.0.0.1", 9509);
  10. $serv->set(array(
  11. 'worker_num' => 32, //reactor thread num
  12. 'backlog' => 128, //listen backlog
  13. 'max_conn' => 10000,
  14. 'dispatch_mode' => 2,
  15. //'open_tcp_keepalive' => 1,
  16. //'log_file' => '/tmp/swoole.log', //swoole error log
  17. ));
  18. $serv->on('WorkerStart', array($this, 'onStart'));
  19. $serv->on('Connect', array($this, 'onConnect'));
  20. $serv->on('Receive', array($this, 'onReceive'));
  21. $serv->on('Close', array($this, 'onClose'));
  22. $serv->on('WorkerStop', array($this, 'onShutdown'));
  23. $serv->start();
  24. }
  25. function onStart($serv)
  26. {
  27. $this->serv = $serv;
  28. echo "Server: start.Swoole version is [" . SWOOLE_VERSION . "]\n";
  29. }
  30. function onShutdown($serv)
  31. {
  32. echo "Server: onShutdown\n";
  33. }
  34. function onClose($serv, $fd, $reactor_id)
  35. {
  36. }
  37. function onConnect($serv, $fd, $reactor_id)
  38. {
  39. }
  40. function onReceive($serv, $fd, $reactor_id, $data)
  41. {
  42. $socket = new swoole_client(SWOOLE_SOCK_TCP);
  43. if($socket->connect('127.0.0.1', 80, 0.5))
  44. {
  45. $socket->send($data);
  46. $serv->send($fd, $socket->recv(8192, 0));
  47. }
  48. unset($socket);
  49. $serv->close($fd);
  50. }
  51. }
  52. $serv = new ProxyServer();
  53. $serv->run();