Frame.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. */
  25. public static function input($buffer ,TcpConnection $connection)
  26. {
  27. if(strlen($buffer)<4)
  28. {
  29. return 0;
  30. }
  31. $unpack_data = unpack('Ntotal_length', $buffer);
  32. return $unpack_data['total_length'];
  33. }
  34. /**
  35. * Encode.
  36. * @param string $buffer
  37. * @return string
  38. */
  39. public static function decode($buffer)
  40. {
  41. return substr($buffer, 4);
  42. }
  43. /**
  44. * Decode.
  45. * @param string $buffer
  46. * @return string
  47. */
  48. public static function encode($buffer)
  49. {
  50. $total_length = 4 + strlen($buffer);
  51. return pack('N',$total_length) . $buffer;
  52. }
  53. }