udp_tcp_timeout.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * @Author: winterswang
  4. * @Date: 2015-06-18 16:45:09
  5. * @Last Modified by: winterswang
  6. * @Last Modified time: 2016-09-18 17:36:18
  7. */
  8. class TestHttpServer {
  9. public $http;
  10. public $queue;
  11. public $setting = array();
  12. /**
  13. * [__construct description]
  14. * @param array $setting [description]
  15. */
  16. public function __construct(){
  17. }
  18. public function set($setting){
  19. $this ->setting = $setting;
  20. }
  21. /**
  22. * [init description]
  23. * @return [type] [description]
  24. */
  25. public function init(){
  26. if (!isset($this ->setting['host'])) {
  27. $this ->setting['host'] = '0.0.0.0';
  28. }
  29. if (!isset($this ->setting['port'])) {
  30. $this ->setting['port'] = '9999';
  31. }
  32. $this ->http = new Swoole\Http\Server($this ->setting['host'], $this ->setting['port']);
  33. $this ->http ->set($this ->setting);
  34. $this ->http ->on('request', array($this, 'onRequest'));
  35. $this ->http ->on('close', array($this, 'onClose'));
  36. }
  37. /**
  38. * [onRequest description]
  39. * @param [type] $request [description]
  40. * @param [type] $response [description]
  41. * @return [type] [description]
  42. */
  43. public function onRequest($request, $response){
  44. //$udp_cli = new Swoole\Coroutine\Client(SWOOLE_SOCK_UDP);
  45. $tcp_cli = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
  46. // $ret = $udp_cli ->connect('10.100.65.222', 9906);
  47. // $ret = $udp_cli ->send('test for the coro');
  48. // $ret = $udp_cli ->recv(100);
  49. // $udp_cli->close();
  50. // if ($ret) {
  51. // //error_log(" udp cli get rsp == " . print_r($ret, true),3, '/data/log/udp_timeout.log');
  52. // }
  53. // else{
  54. // error_log(" udp cli timeout \n",3, '/data/log/udp_timeout.log');
  55. // }
  56. $ret = $tcp_cli ->connect("10.100.64.151", 9805);
  57. $ret = $tcp_cli ->send('test for the coro');
  58. $ret = $tcp_cli ->recv(100);
  59. $tcp_cli->close();
  60. if ($ret) {
  61. //error_log(" tcp cli get rsp == " . print_r($ret, true) . PHP_EOL, 3, '/data/log/udp_timeout.log');
  62. }
  63. else{
  64. error_log(" tcp cli timeout \n",3, '/data/log/udp_timeout.log');
  65. }
  66. $response ->end(" swoole response is ok");
  67. }
  68. /**
  69. * [onClose description]
  70. * @param [type] $server [description]
  71. * @param [type] $fd [description]
  72. * @param [type] $reactor_id [description]
  73. * @return [type] [description]
  74. */
  75. public function onClose($server, $fd, $reactor_id){
  76. //echo " on close fd = $fd reactor_id = $reactor_id \n";
  77. }
  78. /**
  79. * [start description]
  80. * @return [type] [description]
  81. */
  82. public function start(){
  83. $this ->init();
  84. $this ->http ->start();
  85. }
  86. }
  87. $setting = array(
  88. 'host' => '0.0.0.0',
  89. 'port' => 10006,
  90. 'worker_num' => 4,
  91. 'dispatch_mode' => 3, //固定分配请求到worker
  92. 'reactor_num' => 4, //亲核
  93. 'daemonize' => 1, //守护进程
  94. 'backlog' => 128,
  95. 'log_file' => '/data/log/test_http_server.log',
  96. );
  97. $th = new TestHttpServer();
  98. $th ->set($setting);
  99. $th ->start();