|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Async sockets |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2015-2016, Efimov Evgenij <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This source file is subject to the MIT license that is bundled |
|
8
|
|
|
* with this source code in the file LICENSE. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace AsyncSockets\Frame; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class AbstractFramePicker |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class AbstractFramePicker implements FramePickerInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Flag, whether framePicker is finished |
|
19
|
|
|
* |
|
20
|
|
|
* @var bool |
|
21
|
|
|
*/ |
|
22
|
|
|
private $isFinished; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Frame with data for this picker |
|
26
|
|
|
* |
|
27
|
|
|
* @var FrameInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $frame; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Collected data for this frame |
|
33
|
|
|
* |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private $buffer; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Client's address sent this data |
|
40
|
|
|
* |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
private $remoteAddress; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* AbstractFramePicker constructor. |
|
47
|
|
|
*/ |
|
48
|
132 |
|
public function __construct() |
|
49
|
|
|
{ |
|
50
|
132 |
|
$this->isFinished = false; |
|
51
|
132 |
|
$this->buffer = ''; |
|
52
|
132 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** {@inheritdoc} */ |
|
55
|
76 |
|
public function isEof() |
|
56
|
|
|
{ |
|
57
|
76 |
|
return $this->isFinished; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** {@inheritdoc} */ |
|
61
|
75 |
|
public function pickUpData($chunk, $remoteAddress) |
|
62
|
|
|
{ |
|
63
|
75 |
|
if ($this->isFinished) { |
|
64
|
3 |
|
return $chunk; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
75 |
|
if (!$this->remoteAddress && $remoteAddress) { |
|
68
|
70 |
|
$this->remoteAddress = $remoteAddress; |
|
69
|
70 |
|
} |
|
70
|
|
|
|
|
71
|
75 |
|
$this->frame = null; |
|
72
|
75 |
|
return (string) $this->doHandleData($chunk, $remoteAddress, $this->buffer); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Process raw network data. Data should be used to determine end of this concrete framePicker |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $chunk Chunk read from socket |
|
79
|
|
|
* @param string $remoteAddress Client's address sent this data |
|
80
|
|
|
* @param string &$buffer Pointer to internal buffer for collecting data |
|
81
|
|
|
* |
|
82
|
|
|
* @return string Unprocessed data after the end of frame |
|
83
|
|
|
*/ |
|
84
|
|
|
abstract protected function doHandleData($chunk, $remoteAddress, &$buffer); |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Create frame for picked up data |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $buffer Buffer with collected data |
|
90
|
|
|
* @param string $remoteAddress Remote socket address these data from |
|
91
|
|
|
* |
|
92
|
|
|
* @return FrameInterface |
|
93
|
|
|
*/ |
|
94
|
|
|
abstract protected function doCreateFrame($buffer, $remoteAddress); |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Sets finished flag |
|
98
|
|
|
* |
|
99
|
|
|
* @param boolean $isFinished Flag whether framePicker is finished |
|
100
|
|
|
* |
|
101
|
|
|
* @return void |
|
102
|
|
|
*/ |
|
103
|
62 |
|
protected function setFinished($isFinished) |
|
104
|
|
|
{ |
|
105
|
62 |
|
$this->isFinished = $isFinished; |
|
106
|
62 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** {@inheritdoc} */ |
|
109
|
73 |
|
public function createFrame() |
|
110
|
|
|
{ |
|
111
|
73 |
|
if (!$this->frame) { |
|
112
|
73 |
|
$this->frame = $this->doCreateFrame($this->buffer, $this->remoteAddress); |
|
113
|
73 |
|
} |
|
114
|
|
|
|
|
115
|
73 |
|
return $this->frame; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|