Giao thức frame

(Cần phiên bản Workerman >= 3.3.0)

workerman định nghĩa một giao thức gọi là frame, định dạng giao thức là độ dài tổng gói + nội dung gói, trong đó độ dài gói là một số nguyên 4 byte theo thứ tự byte mạng, nội dung gói có thể là văn bản bình thường hoặc dữ liệu nhị phân.

Dưới đây là triển khai giao thức frame.

class Frame
{

    public static function input($buffer ,TcpConnection $connection)
    {
        if(strlen($buffer)<4)
        {
            return 0;
        }
        $unpack_data = unpack('Ntotal_length', $buffer);
        return $unpack_data['total_length'];
    }

    public static function decode($buffer)
    {
        return substr($buffer, 4);
    }

    public static function encode($buffer)
    {
        $total_length = 4 + strlen($buffer);
        return pack('N',$total_length) . $buffer;
    }
}