Text.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. * Text Protocol.
  18. */
  19. class Text
  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. // Judge whether the package length exceeds the limit.
  30. if(strlen($buffer)>=TcpConnection::$maxPackageSize)
  31. {
  32. $connection->close();
  33. return 0;
  34. }
  35. // Find the position of "\n".
  36. $pos = strpos($buffer, "\n");
  37. // No "\n", packet length is unknown, continue to wait for the data so return 0.
  38. if($pos === false)
  39. {
  40. return 0;
  41. }
  42. // Return the current package length.
  43. return $pos+1;
  44. }
  45. /**
  46. * Encode.
  47. * @param string $buffer
  48. * @return string
  49. */
  50. public static function encode($buffer)
  51. {
  52. // Add "\n"
  53. return $buffer."\n";
  54. }
  55. /**
  56. * Decode.
  57. * @param string $buffer
  58. * @return string
  59. */
  60. public static function decode($buffer)
  61. {
  62. // Remove "\n"
  63. return trim($buffer);
  64. }
  65. }