ProtocolInterface.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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\ConnectionInterface;
  16. /**
  17. * Protocol interface
  18. */
  19. interface ProtocolInterface
  20. {
  21. /**
  22. * 用于分包,即在接收的buffer中返回当前请求的长度(字节)
  23. * 如果可以在$recv_buffer中得到请求包的长度则返回长度
  24. * 否则返回0,表示需要更多的数据才能得到当前请求包的长度
  25. * 如果返回false或者负数,则代表请求不符合协议,则连接会断开
  26. * @param ConnectionInterface $connection
  27. * @param string $recv_buffer
  28. * @return int|false
  29. */
  30. public static function input($recv_buffer, ConnectionInterface $connection);
  31. /**
  32. * 用于请求解包
  33. * input返回值大于0,并且WorkerMan收到了足够的数据,则自动调用decode
  34. * 然后触发onMessage回调,并将decode解码后的数据传递给onMessage回调的第二个参数
  35. * 也就是说当收到完整的客户端请求时,会自动调用decode解码,无需业务代码中手动调用
  36. * @param ConnectionInterface $connection
  37. * @param string $recv_buffer
  38. * @return mixed
  39. */
  40. public static function decode($recv_buffer, ConnectionInterface $connection);
  41. /**
  42. * 用于请求打包
  43. * 当需要向客户端发送数据即调用$connection->send($data);时
  44. * 会自动把$data用encode打包一次,变成符合协议的数据格式,然后再发送给客户端
  45. * 也就是说发送给客户端的数据会自动encode打包,无需业务代码中手动调用
  46. * @param ConnectionInterface $connection
  47. * @param mixed $data
  48. * @return string
  49. */
  50. public static function encode($data, ConnectionInterface $connection);
  51. }