Frame.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /**
  3. * This file is part of workerman.
  4. *
  5. * Licensed under The MIT License
  6. * For full copyright and license information, please see the MIT-LICENSE.txt
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @author walkor<walkor@workerman.net>
  10. * @copyright walkor<walkor@workerman.net>
  11. * @link http://www.workerman.net/
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Workerman\Protocols;
  15. use Workerman\Connection\TcpConnection;
  16. /**
  17. * Frame Protocol.
  18. */
  19. class Frame
  20. {
  21. /**
  22. * Check the integrity of the package.
  23. * @param string $buffer
  24. * @param TcpConnection $connection
  25. * @return int
  26. */
  27. public static function input($buffer ,TcpConnection $connection)
  28. {
  29. if(strlen($buffer)<4)
  30. {
  31. return 0;
  32. }
  33. $unpack_data = unpack('Ntotal_length', $buffer);
  34. return $unpack_data['total_length'];
  35. }
  36. /**
  37. * Encode.
  38. * @param string $buffer
  39. * @return string
  40. */
  41. public static function decode($buffer)
  42. {
  43. return substr($buffer, 4);
  44. }
  45. /**
  46. * Decode.
  47. * @param string $buffer
  48. * @return string
  49. */
  50. public static function encode($buffer)
  51. {
  52. $total_length = 4 + strlen($buffer);
  53. return pack('N',$total_length) . $buffer;
  54. }
  55. }