<?php
/**
 * This file is part of workerman.
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the MIT-LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @author    walkor<walkor@workerman.net>
 * @copyright walkor<walkor@workerman.net>
 * @link      http://www.workerman.net/
 * @license   http://www.opensource.org/licenses/mit-license.php MIT License
 */
namespace Workerman\Protocols;

use Workerman\Connection\ConnectionInterface;

/**
 * 协议接口
 */
interface ProtocolInterface
{
    /**
     * 检测数据包的完整性
     * 返回数据包的长度
     * 如果长度未知,请返回0表示需要更多数据。
     * 如果数据包有问题请返回false,连接将被关闭
     *
     * @param ConnectionInterface $connection
     * @param string              $recv_buffer
     * @return int|false
     */
    public static function input($recv_buffer, ConnectionInterface $connection);

    /**
     * 解码数据包并发出onMessage($ message)回调,
     * $message是返回解码的结果。
     *
     * @param ConnectionInterface $connection
     * @param string              $recv_buffer
     * @return mixed
     */
    public static function decode($recv_buffer, ConnectionInterface $connection);

    /**
     * 在发送给客户端之前对数据包进行编码
     *
     * @param ConnectionInterface $connection
     * @param mixed               $data
     * @return string
     */
    public static function encode($data, ConnectionInterface $connection);
}