forked from walkor/workerman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProtocolInterface.php
55 lines (51 loc) · 2.13 KB
/
ProtocolInterface.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?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<[email protected]>
* @copyright walkor<[email protected]>
* @link http://www.workerman.net/
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Workerman\Protocols;
use \Workerman\Connection\ConnectionInterface;
/**
* Protocol interface
*/
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);
}