You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. <?PHP
  2. /**
  3. * {
  4. * unisgned char type;
  5. * unsigned int time;
  6. * unsigned int serid;
  7. * unsigned int length;
  8. * char body[length];
  9. * }
  10. */
  11. function remoteCall(string $host, $port, string $data) {
  12. $length = strlen($data);
  13. $pack = pack('CNNNa*', 1, time(), 0, $length, $data);
  14. $client = new Swoole\Client(SWOOLE_SOCK_TCP);
  15. $client->set(array(
  16. 'open_length_check' => true,
  17. 'package_max_length' => 1024 * 1024 * 20,
  18. 'package_length_type' => 'N',
  19. 'package_length_offset' => 9,
  20. 'package_body_offset' => 13
  21. ));
  22. if (!$client->connect($host, $port, 0.5)) {
  23. return false;
  24. }
  25. $client->send($pack);
  26. $pack = @$client->recv();
  27. $client->close();
  28. if ($pack === false) {
  29. //echo $client->errCode . "\n";
  30. //TODO 获取错误码并进行处理
  31. return false;
  32. } else if (empty($pack)) {
  33. echo "close by peer\n";
  34. return false;
  35. }
  36. $ret = unpack('Ctype/Ntime/Nserid/Nlength/a*data', $pack);
  37. return $ret['data'];
  38. }
  39. //$data = '{"controller":"site","methor":"test","params":{"id":1}}';
  40. $data = file_get_contents("/mnt/hgfs/wdev/data.txt");
  41. $ret = remoteCall("127.0.0.1", "5188", $data);
  42. //'{"controller":"site","methor":"test","params":{"id":"111"}}
  43. //'{"controller":"site","methor":"test","params":{"id":1,"status":0}}'
  44. echo "{$ret}\n";
  45. ?>