phpsocks5/Workerman/Protocols/ProtocolInterface.php
2015-04-04 21:46:31 +08:00

45 lines
1.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Workerman\Protocols;
use \Workerman\Connection\ConnectionInterface;
/**
* Protocol interface
* @author walkor <walkor@workerman.net>
*/
interface ProtocolInterface
{
/**
* 用于分包即在接收的buffer中返回当前请求的长度字节
* 如果可以在$recv_buffer中得到请求包的长度则返回长度
* 否则返回0表示需要更多的数据才能得到当前请求包的长度
* 如果返回false或者负数则代表请求不符合协议则连接会断开
* @param ConnectionInterface $connection
* @param string $recv_buffer
* @return int|false
*/
public static function input($recv_buffer, ConnectionInterface $connection);
/**
* 用于请求解包
* input返回值大于0并且WorkerMan收到了足够的数据则自动调用decode
* 然后触发onMessage回调并将decode解码后的数据传递给onMessage回调的第二个参数
* 也就是说当收到完整的客户端请求时会自动调用decode解码无需业务代码中手动调用
* @param ConnectionInterface $connection
* @param string $recv_buffer
* @return mixed
*/
public static function decode($recv_buffer, ConnectionInterface $connection);
/**
* 用于请求打包
* 当需要向客户端发送数据即调用$connection->send($data);时
* 会自动把$data用encode打包一次变成符合协议的数据格式然后再发送给客户端
* 也就是说发送给客户端的数据会自动encode打包无需业务代码中手动调用
* @param ConnectionInterface $connection
* @param mixed $data
* @return string
*/
public static function encode($data, ConnectionInterface $connection);
}