1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Type frame |
4
|
|
|
* User: moyo |
5
|
|
|
* Date: 27/02/2018 |
6
|
|
|
* Time: 2:32 PM |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Carno\NSQ\Protocol; |
10
|
|
|
|
11
|
|
|
use Carno\NSQ\Types\Message; |
12
|
|
|
|
13
|
|
|
class Frame |
14
|
|
|
{ |
15
|
|
|
// types |
16
|
|
|
private const TYPE_RESPONSE = 0; |
17
|
|
|
private const TYPE_ERROR = 1; |
18
|
|
|
private const TYPE_MESSAGE = 2; |
19
|
|
|
|
20
|
|
|
// responses |
21
|
|
|
private const RESP_OK = 'OK'; |
22
|
|
|
private const RESP_HEARTBEAT = '_heartbeat_'; |
23
|
|
|
private const RESP_CLOSE_WAIT = 'CLOSE_WAIT'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $response = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $error = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var Message |
37
|
|
|
*/ |
38
|
|
|
private $message = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Frame constructor. |
42
|
|
|
* @param int $sized |
43
|
|
|
* @param Buffer $buffer |
44
|
|
|
*/ |
45
|
|
|
public function __construct(int $sized, Buffer $buffer) |
46
|
|
|
{ |
47
|
|
|
switch (Binary::int($buffer)) { |
48
|
|
|
case self::TYPE_RESPONSE: |
49
|
|
|
$this->response = Binary::string($buffer, $sized - 4); |
50
|
|
|
break; |
51
|
|
|
case self::TYPE_ERROR: |
52
|
|
|
$this->error = Binary::string($buffer, $sized - 4); |
53
|
|
|
break; |
54
|
|
|
case self::TYPE_MESSAGE: |
55
|
|
|
$timestamp = Binary::long($buffer); |
56
|
|
|
$attempts = Binary::short($buffer); |
57
|
|
|
$identify = Binary::string($buffer, 16); |
58
|
|
|
$payload = Binary::string($buffer, $sized - 30); |
59
|
|
|
$this->message = (new Message($payload))->meta($identify, $attempts, $timestamp); |
|
|
|
|
60
|
|
|
break; |
61
|
|
|
default: |
62
|
|
|
$this->error = Binary::string($buffer, $sized - 4); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function isResponse() : bool |
70
|
|
|
{ |
71
|
|
|
return $this->response !== null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public function isOK() : bool |
78
|
|
|
{ |
79
|
|
|
return $this->response === self::RESP_OK; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
public function isHeartbeat() : bool |
86
|
|
|
{ |
87
|
|
|
return $this->response === self::RESP_HEARTBEAT; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
public function isCloseWait() : bool |
94
|
|
|
{ |
95
|
|
|
return $this->response === self::RESP_CLOSE_WAIT; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function isError() : bool |
102
|
|
|
{ |
103
|
|
|
return $this->error !== null; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function getError() : string |
110
|
|
|
{ |
111
|
|
|
return $this->error; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
public function isMessage() : bool |
118
|
|
|
{ |
119
|
|
|
return $this->message !== null; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return Message |
124
|
|
|
*/ |
125
|
|
|
public function getMessage() : Message |
126
|
|
|
{ |
127
|
|
|
return $this->message; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|