Protocol::packing()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
/**
3
 * TCP accel protocol
4
 * User: moyo
5
 * Date: 2018/7/30
6
 * Time: 4:41 PM
7
 */
8
9
namespace Carno\HRPC\Accel\Chips;
10
11
trait Protocol
12
{
13
    /**
14
     * @param int $seq
15
     * @param string $message
16
     * @return string
17
     */
18
    protected function packing(int $seq, string $message) : string
19
    {
20
        return pack('N', $seq) . pack('N', strlen($message)) . $message;
21
    }
22
23
    /**
24
     * @param string $packet
25
     * @return array [int:seq, string:message]
26
     */
27
    protected function unpacking(string $packet) : array
28
    {
29
        return [
30
            unpack('N', substr($packet, 0, 4))[1],
31
            substr($packet, 8, unpack('N', substr($packet, 4, 4))[1])
32
        ];
33
    }
34
}
35